HTC Vive开发笔记之手柄控制

怎么安装设备,配置环境我就不说了,自行百度,教程很多也很简单。接下来说下Vive手柄的控制。

手柄是HTC Vive的重要交互手段,我们通过第一个图片应该对其有一个直观的了解了,总共是九个按钮:

    • 第一个是菜单按钮;
    • 2,3,4,5分别对应的是Trackpad/Touchpad的上下左右,有时候对应的是XBox手柄的▲OX四个按钮或者摇杆;
    • 6对应的是系统按钮/Steam;
    • 7是Trigger/扳机,对应大多数FPS游戏里面的枪械的Shoot/Fire;
    • 8对应的Grip/紧握在手柄的左右两侧各有一个,有时候我们用它来翻页;
    • 9其实是Trackpad/Touchpad在Z轴的一个延伸,相当于是点击事件Click. 
using UnityEngine;
using System.Collections;
/// <summary>
/// 扳机控制触发事件
/// </summary>
public class ComfirmController : MonoBehaviour {
    //手柄
    SteamVR_TrackedObject trackedObj;
 
    void Awake()
    {
        //获取手柄脚本组件
        trackedObj = GetComponent<SteamVR_TrackedObject> ();
    }
 
    // Use this for initialization
    void Start () {
     
    }
     
    // Update is called once per frame
    void FixedUpdate () {
         
        //获取手柄输入
        var device = SteamVR_Controller.Input ((int)trackedObj.index);
 
        //此处可以换其他的函数触发GetPress/GetTouch /GetPressUp GetTouchDown/GetTouchUp/GetAxis
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))  
        {  
            Debug.Log("按下圆盘");  
        }  
        else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))  
        {  
          
            Debug.Log("按下扳机键");  
        }  
        else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))  
        {  
          
            Debug.Log("按下手柄侧键");  
        }  
        else if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))  
        {  
          
            Debug.Log("按下手柄菜单键");  
        }  
        else if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))  
        {  
  
            Debug.Log("按下手柄菜单键");  
        }  
 
    }
}

 

原文地址:https://www.cnblogs.com/unity3ds/p/5908592.html