c# - 使用 SteamVR 交互系统时如何获取 Controller 输入

标签 c# unity3d steam virtual-reality

我只想在我的 VR 游戏中简单地从用户那里获得 Controller 输入,我还想使用 SteamVR 交互系统,这样我就可以实现简单的 UI 内容。但是,我无法从 Hand 脚本的 Controller 中获取输入。

我所做的只是拖入“Player”预制件,然后编写脚本以继续 Hand 对象以从触发器获取输入。

    private Hand _hand;                     // The hand object
    private SteamVR_Controller.Device controller; // the controller property of the hand


    void Start ()
    {
        // Get the hand componenet
        _hand = GetComponent<Hand>();
        // Set the controller reference
        controller = _hand.controller;
    }

    void Update ()
    {
            // === NULL REFERENCE === //
        if ( controller.GetHairTrigger())
        {
            Debug.Log ("Trigger");
        }

    }

这给了我一个“ Controller ”对象的空引用异常。我还尝试在 OnEnable()Awake() 中获取 Controller 组件,但这也没有用。即使在 Update() 中。因此出于某种原因,SteamVR 的 Hand 类不包含对 Controller 的引用。我做错了什么吗?当我拿到 Controller 时,我是否缺少某种索引规范?

我能够像这样获得 Controller 输入:

   private SteamVR_TrackedObject trackedObj;       // The tracked object that is the controller

    private SteamVR_Controller.Device Controller    // The device property that is the controller, so that we can tell what index we are on
    {
        get { return SteamVR_Controller.Input((int)trackedObj.index); }
    }

    private void Awake()
    {
        // Get the tracked object componenet
        trackedObj = GetComponent<SteamVR_TrackedObject>();
    }

    void Update()
 {
     if(Controller.GetHairTrigger()){
          Debug.Log("hey trigger");
     }
  }

但是我无法使用交互系统。有人知道吗?

最佳答案

我真的建议你添加 Valve 的免费 HTC.UnityPlugin在 SteamVR 集成之上, 因此您可以使用

快速访问 Controller 输入
using UnityEngine;
using HTC.UnityPlugin.Vive;
public class YourClass : MonoBehaviour
{
    private void Update()
    {
        // get trigger
        if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Trigger))
        {
            // ...
        }
    }
}

关于c# - 使用 SteamVR 交互系统时如何获取 Controller 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527301/

相关文章:

steam - Web API 的评论统计数据是否已过时?

c# - 获取对象列表的属性的所有值

c# - 获取谷歌浏览器窗口标题

c# - 如何在保持趋势的同时删除常规间隔中的 n% 的列表?

c# - 从程序集 c# 引用 .dll

c# - 如何在到另一点的路径上找到最近的位置

steam - steam社区市场中的item_type

C#、xunit、fscheck,使用自定义生成器或约束随机字符串编写基于简单属性的测试

C# 错误和头痛 >.< (MonoDevelop version2.8.2 for Unity 3d)

javascript - Nodejs Steam 交易机器人