Unity +HTC VIVE 虚拟现实开发— 手柄控制

<p>Hello 大家好,我是帅气的交换机,这是我人生中第一次写博客,但绝不是最后一次,哈哈,想想都好激动啊。</p><p>言归正传,写这篇文章的原因主要是因为现在虚拟现实非常的火爆但目前主流的虚拟现实设备(HTC VIVE)的教程却少的可怜,这个我深有体会。所以,我想将我平时开发中遇到的问题以及解决方法记录下来,分享给大家,若其中有什么错误或者大家有什么更好的方案也请大家指出,大家互相学习,哈哈。</p><p>好了直接上代码。</p>
</pre><pre name="code" class="html"><pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class shoubingkongzhi : MonoBehaviour {
    /// <summary>
    /// 手柄
    /// </summary>
    SteamVR_TrackedObject tracked;



    void Awake()
    {
        //获取手柄
        tracked = GetComponent<SteamVR_TrackedObject>();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        var device = SteamVR_Controller.Input((int)tracked.index);

        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("按下手柄菜单键");
        }
        
    }
}

Debug.Log("按下手柄菜单键");}}}
 
以上都是HTC VIVE手柄中按键按下的代码。其他还有:
按键松开—device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger);
按键长安—device.GetPress(SteamVR_Controller.ButtonMask.Trigger);
按键按下还有另一种方式,但是我自我感觉用着很别扭,没上述的好。device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu);
其他用法与GetPressDown()类似。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值