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

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; 
    //关闭手柄的震动    
  }

你可能感兴趣的:(Vive开发手册之手柄持续震动)