Unity HTCVR 设备手柄控制

Posted dgjs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity HTCVR 设备手柄控制相关的知识,希望对你有一定的参考价值。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HTC.UnityPlugin.Vive;
using UnityEngine.SceneManagement;

public class HandFunction : MonoBehaviour
{
    public static HandFunction hand_function;

    public GameObject RightRay;
    public bool Ray_bool;    //射线开关

    public bool Ray_Canvas;  //ui开关
    private void Awake()
    {
        hand_function = this;
    }

    void Start()
    {
        RightRay.SetActive(false);
        Ray_Canvas = false;
    }

    void Update()
    {

        PressMenuKey();
        PressTriggerKey();
        PressGripKey();
    }


    /// <summary>
    /// 按下应用键
    /// </summary>
    void PressMenuKey()
    {
        if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Menu)/*|| ViveInput.GetPressDown(HandRole.LeftHand, ControllerButton.Menu)*/)
        {
            Ray_bool = !Ray_bool;
            RightRay.SetActive(Ray_bool);
        }
        if (ViveInput.GetPressDown(HandRole.LeftHand,ControllerButton.Menu))
        {
            if (SceneVR.scene_vr.UiOpen==true)
            {
                Ray_Canvas = !Ray_Canvas;
                SceneVR.scene_vr.UICanvasVR.SetActive(Ray_Canvas);
            }
        }
    }

    /// <summary>
    /// 按下扳机键
    /// </summary>
    void PressTriggerKey()
    {
        if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger)|| ViveInput.GetPressDown(HandRole.LeftHand, ControllerButton.Trigger))
        {
            SceneVR.scene_vr.IsRayOpenToolVR = true;

        }
        if (ViveInput.GetPressUp(HandRole.RightHand, ControllerButton.Trigger)|| ViveInput.GetPressUp(HandRole.LeftHand, ControllerButton.Trigger))
        {
            SceneVR.scene_vr.IsRayOpenToolVR = false;
        }
    }

    /// <summary>
    /// 按下圆盘键
    /// </summary>
    void PressPadKey()
    {
        if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Pad))
        {

        }
    }

    /// <summary>
    /// 按下侧握键
    /// </summary>
    void PressGripKey()
    {
        if (ViveInput.GetPressDown(HandRole.LeftHand,ControllerButton.Grip))
        {
            SceneVR.scene_vr.Rerouting();
            SceneVR.scene_vr.IsaniOpen();
            SceneVR.scene_vr.IsAniOpen = false;

        }
    }
}

 

以上是关于Unity HTCVR 设备手柄控制的主要内容,如果未能解决你的问题,请参考以下文章

用Unity开发HTC VIVE——手柄控制篇

unity HTC Vive 通过手柄控制3D物体移动、旋转

unity3d 虚拟手柄

unity每次用手柄点击button,都变换物体的材质

UnityPico手柄摇杆控制第一人称移动和旋转

我可以在 Unity3d 中使用多个游戏手柄吗?