在 VRTK 中检测控制器输入

Detect controller input in VRTK

我是新手,如果我写错地方了,请告诉我,我会移动/删除此评论。

我目前在使用 VRTK 时检测控制器输入时遇到问题。

例如,当我在两个物体之间发生碰撞时,我希望能够检测到控制器上按下了哪些按钮,但似乎无法弄清楚如何才能做到这一点。

此外,我已经实现了交互使用功能,但我正在努力弄清楚如何让两个按钮执行不同的操作。

例如:

once I grab an object with the simple pointer I want one button to bring the object closer and another to move it away, but I've only managed to implement one or the other.


有什么建议吗?我在文档、示例和 Google 中到处查看,但似乎找不到任何东西。任何帮助将非常感激!把我的头发扯出来!

您可以在 InteractableObject 上使用 Grabbed 方法:https://vrtoolkit.readme.io/docs/vrtk_interactableobject#section-grabbed-1

或者您可以在 InteractGrab 脚本上使用 ControllerGrabInteractableObject 事件:https://vrtoolkit.readme.io/docs/vrtk_interactgrab#section-class-events

或者您可以有一个更新例程,并检查控制器上的 grabbed 状态,执行 GetGrabbedObject() != null(检查控制器是否抓取了一个对象,如果它为空,则一个不是抓住了)。

然后您可以使用 ControllerEvents 按钮布尔值在按下按钮时执行某些操作。因此,带有此内容的脚本位于交互抓取脚本旁边的控制器脚本别名游戏对象上:

void Update() {
  if (GetComponent<VRTK_InteractGrab>().GetGrabbedObject != null) {
    var controllerEvents = GetComponent<VRTK_ControllerEvents>();
    if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Trigger_Press) {
        //Do something on trigger press
    }

    if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Grip_Press) {
        //Do something on grip press
    }
  }
}