【问题标题】:How to get controller input when using the SteamVR Interaction System使用 SteamVR 交互系统时如何获取控制器输入
【发布时间】:2017-04-20 18:33:35
【问题描述】:

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

我所做的只是拖入“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");
        }

    }

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

我能够像这样获得控制器输入:

   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");
     }
  }

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

【问题讨论】:

    标签: c# unity3d steam virtual-reality


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在不安装 SteamVR 以外的任何其他依赖项的情况下,将脚本附加到 Hand 组件之一。它是 SteamVR 资产文件夹中 Player 预制件的子代。

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Grab: MonoBehaviour {
          private Valve.VR.InteractionSystem.Hand hand;
      
          void Start () {
              hand = gameObject.GetComponent<Valve.VR.InteractionSystem.Hand>();
          }
      
          void Update () {
              if (hand.controller == null) return;
      
              // Change the ButtonMask to access other inputs
              if (hand.controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
              {
                  Debug.Log("Trigger down")
              }
      
              if (hand.controller.GetPress(SteamVR_Controller.ButtonMask.Trigger))
              {
                  Debug.Log("Trigger still down")
              }
      
              if (hand.controller.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
              {
                  Debug.Log("Trigger released")
              }
          }
      }
      

      【讨论】:

      • 这不再适用于 Unity 当前的 Steam 插件 - 手头没有控制器属性。任何人都可以建议目前的正确方法吗?我很难用新系统读取控制器输入。
      • 我是这样使用的: 第一:使用 Valve.VR.InteractionSystem;那么你可以像这样访问手牌: void Update() { foreach (var hand in Player.instance.hands) {...我希望它有帮助
      【解决方案3】:

      这里是你如何用手工作的方法:

      using UnityEngine;
      using System.Collections;
      using System.Collections.Generic;
      using Valve.VR.InteractionSystem;
      
          public class ShowControllers : MonoBehaviour
          {
              public bool showController = false;
          
          // Update is called once per frame
          void Update()
              {
                  foreach (var hand in Player.instance.hands)
                  {
                      if (showController)
                      {
                          hand.ShowController();
                          hand.SetSkeletonRangeOfMotion(Valve.VR.EVRSkeletalMotionRange.WithController);
                      }
                      else
                      {
                          hand.HideController();
                      hand.SetSkeletonRangeOfMotion(Valve.VR.EVRSkeletalMotionRange.WithoutController);
                  }
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 2014-02-28
        • 2017-12-12
        相关资源
        最近更新 更多