我只想在我的 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/