SteamVR+HTCVive 交互事件

1,了解HTCVive手柄按钮对应名称

HTCVive手柄介绍

2,获取手柄输入事件

using UnityEngine;
using System.Collections;

//该脚本挂在[CameraRig] 下的Controller(left or right)即手柄 上
public class WandController : MonoBehaviour {

    //1 获取控制器按钮ID
    private Valve.VR.EVRButtonId triggerBtn = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;//获取trigger Id
    private Valve.VR.EVRButtonId touchpadBtn = Valve.VR.EVRButtonId.k_EButton_SteamVR_Touchpad; //获取touchpad Id
    private Valve.VR.EVRButtonId gripBtn = Valve.VR.EVRButtonId.k_EButton_Grip;//获取grip Id
    private Valve.VR.EVRButtonId menuBtn = Valve.VR.EVRButtonId.k_EButton_ApplicationMenu;//获取menu Id
    //2 定义trackedObj (SteamVR_TrackedObject script 组件挂在[CameraRig] 下的Controller(left or right) 上
    private SteamVR_TrackedObject trackedObj;
    //3 获取Device类 ,最后通过调用Device类下的方法GetTouch(Press)Down/GetTouch(Press)/GetTouch(Press)Up来获取按键事件
    private SteamVR_Controller.Device device { get { return SteamVR_Controller.Input((int)trackedObj.index); } }

    // Use this for initialization
    void Start () {
        trackedObj = GetComponent<SteamVR_TrackedObject>();
    }

    // Update is called once per frame
    void Update () {
         if (device == null)
        {
            Debug.Log("device not initialized");
            return;
        }
        /**
         *下面是trigger键按下至松开过程的输入事件 
         * 按下(只会调用一次)-----> 按住(多次调用)-----> 松开(只会调用一次)
         * **/

        if (device.GetPressDown(triggerBtn))
        {
            //Debug.Log("trigger 刚按下"); 
        }

        if (device.GetPress(triggerBtn))
        {
            //Debug.Log("trigger 一直处于按下状态");
        }
        if (device.GetPressUp(triggerBtn))
        {
            //Debug.Log("trigger 刚松开");
        }
        /**
         *下面是touchpad的触摸前后输入事件
         */
        if (device.GetTouchDown(touchpadBtn))
        {
            //Debug.Log("手指刚接触触摸板");
        }
        if (device.GetTouch(touchpadBtn))
        {
            //Debug.Log("一直接触触摸板");
        }
        if (device.GetTouchUp(touchpadBtn))
        {
            //Debug.Log("手指离开触摸板");
        }
        //手指在触摸板上的位置 为Vector2 类型 x,y 介于-1 至1之间
        //Debug.Log("触摸点的位置:"+ device.GetAxis(touchpadBtn));
        //扳机键按下的程度 为Vector2 类型 x介于0至1,y 始终为0
        //Debug.Log("扳机键的位置:" + device.GetAxis(triggerBtn));
        //使用hairTrigger来判断
        //device.hairTriggerDelta = 0.5f;//hairTriggerDelta默认是0.1f;
        //Debug.Log(device.GetHairTriggerDown());



        //手柄震动  参数可以理解为震动强度,默认500(参数超过4000会无效,导致震动不触发)
        //device.TriggerHapticPulse(3000);
    }
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值