unity3d手柄按键事件回调

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 定义一个按键类.设置手柄按键
/// 
public class ButtonTouchAction : MonoBehaviour {
    SteamVR_TrackedObject trackdeObjec;
    void Awake() {  
        //获取手柄上的这个组件  
        trackdeObjec = GetComponent();  
    } 
    // Update is called once per frame
    void FixedUpdate () {
        //获取手柄输入  
        SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackdeObjec.index);
        if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))//按下扳机
        {
            print("真的按下扳机");
        }
        //按下圆盘键
        if (device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
        {
            // 获取触摸板上的坐标
            Vector2 pad = device.GetAxis();
            // 转换角度
            Vector3 cross = Vector3.Cross(new Vector2(1, 0), pad);
            float angle = Vector2.Angle(new Vector2(1, 0), pad);
            float ang = cross.z > 0 ? -angle : angle; 
            //根据角度来判断上下左右四个范围
            //下
            if (ang > 45 && ang < 135)
            {
                print("下");
            }
            //上  
            else if (ang < -45 && ang> -135)
            {
                print("上");
            }
            //左  
            else if ((ang < 180 && ang> 135) || (ang < -135 && ang > -180))
            {
                print("左");
            }
            //右  
            else if ((ang > 0 && ang< 45) || (ang > -45 && ang< 0))
            {
                print("右");
            }
        }
    }
}

你可能感兴趣的:(unity3d,vr入门,手柄)