Vive开发手册之手柄持续震动

2017-03-08  本文已影响272人  木心Sepith
using UnityEngine;

public class ViveShock : MonoBehaviour 
{    
  SteamVR_TrackedObject Hand;    
  SteamVR_Controller.Device device;    
  bool IsShock = false;  //布尔型变量IsShock   
  
  void Start ()    
  {        
    Hand = GetComponent();  //获得SteamVR_ TrackedObject组件
  }    
   
  void Update ()    
  {      
    //防止Start函数没加载成功,保证SteamVR_ TrackedObject组件获取成功!        
    if (Hand.isValid)        
    {            
      Hand = GetComponent();        
    }       
    
    //根据index,获得手柄            
    device = SteamVR_Controller.Input((int)Hand.index); 
    
 if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
            {            
              IsShock = true;  
              
              StartCoroutine("Shock",0.5f); 
            }    
  } 
  
  IEnumerator Shock(float durationTime)    
  {      
    Invoke("StopShock", durationTime);
        
    while (IsShock)        
    {            
      device.TriggerHapticPulse(500);            
      yield return new WaitForEndOfFrame();        
    }    
  }    
  
  void StopShock()    
  {        
    IsShock = false; 
    //关闭手柄的震动    
  }
上一篇下一篇

猜你喜欢

热点阅读