HTC Vive键位获取以及物品的拾取方法

发表于2017-12-26
评论0 2.9k浏览

HTC Vive开发者的数量不少,也就意味着HTC Vive的应用也会逐渐增多,当这些开发者进行VR的开发的时候,首先面临的两个问题就是HTC键位的获取和物品的拾取问题,想知道方法的就往下看。

Vive键位获取方法:

//获取抓取键
private Valve.VR.EVRButtonId gripButton = Valve.VR.EVRButtonId.k_EButton_Grip;
//调用官方的API,声明一个自己的变量名,随后进行初始化,就可以获得键位啦
SteamVR_TrackedObject trackedObj;
private SteamVR_Controller.Device device { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
 void Start()
    {
        trackedObj = GetComponent<SteamVR_TrackedObject>();
    }
    void Update()
    {
       if(device.GetPressDown(gripButton ))
       {
            Debug.Lor("按下抓取键");
       }
    }


物品的拾取方法:

//获取抓取键
    private Valve.VR.EVRButtonId gripButton = Valve.VR.EVRButtonId.k_EButton_Grip;
    //获取扳机键
    private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
    SteamVR_TrackedObject trackedObj;
    private SteamVR_Controller.Device device { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
    //要拾取的物品,我叫它灭火器
    public Transform extingusher;
    //model为手柄,此脚本挂在手柄的父物体上
    Transform model;
    // Use this for initialization
    void Start()
    {
        trackedObj = GetComponent<SteamVR_TrackedObject>();
        model = this.transform.GetChild(0);
    }
    // Update is called once per frame
    void Update()
    {
        if (device.GetPressDown(gripButton) && extingusher != null)
        {
            //设置灭火器的父节点
            extingusher.SetParent(model);
            extingusher.GetComponent<Rigidbody>().isKinematic = true;      
        }
        if (device.GetPressUp(gripButton) && extingusher != null)
        {
            extingusher.SetParent(null);
            extingusher.GetComponent<Rigidbody>().isKinematic = false;
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        extingusher = other.gameObject.transform;
    }
    private void OnTriggerExit(Collider other)
    {
        extingusher = null;
    }

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引