SteamVR手柄震动控制实现

时间:2022-11-28 15:00:11

SteamVR手柄震动控制实现

public class handCtrl : MonoBehaviour

{

         public SteamVR_TrackedObject _TrackedObject; //定义unity中手柄控制器

         private SteamVR_Controller.Device _device;   //定义手柄控制设备

         private bool flag=false;                  //定义bool变量

        void Start()

        {

                   //确定当前手柄

                   _device = SteamVR_Controller.Input ((int)_TrackedObject.index);

        }

         //触发碰撞检测

        void OnTriggerEnter(Collider other)

         {       

                   //判断碰撞物体

                   if(other.tag.Equals("Fruit"))

                   {       

                       flag = true;

                       //调用手柄震动协同

                       StartCoroutine ("start");

                   }

         }

         //手柄震动协同

         IEnumerator start()

         {

                   //0.2秒后调用ToFalse,关闭while

                   Invoke ("ToFalse", 0.2f);

                   while (flag)

                   {

                            //震动脉冲强度

                            _device.TriggerHapticPulse (3500);

                            //等待间隔

                            yield return new WaitForEndOfFrame();

                   }

         }

         //bool转为false

         void ToFalse()

         {

                   flag = false;

         }

}