第二个控制器操作 VRTK HTC Vive

Second Controller actions VRTK HTC Vive

我们最初没有使用 VRTK 就开始为 HTC Vive 创建应用程序。最近我们切换到使用 VRTK 和 运行 来解决一个问题,当一个控制器按住触发器而另一个控制器按下另一个按钮时,我们想要执行一些操作。我们如何使用 VRTK 实现这一目标? 我们当前的代码:

controllerMain = SteamVR_Controller.Input((int)trackedObj.index);
controllerSecondary = SteamVR_Controller.Input(SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost));

// In Update()
if (controllerMain.GetPressDown(triggerButton) && controllerSecondary.GetPressDown(triggerButton))
{
    scaleSelected(gameObj); //enlarges selected GameObject based on distance between controllers
}

if (controllerMain.GetPressDown(triggerButton) && controllerSecondary.GetPressDown(gripButton))
{
    deleteObject(gameObj); //delete selected GameObject
}

我在 VRTK 文档中找不到任何使用两个控制器与同一对象交互的示例。在 docs/examples 中,一切都是基于事件的,而我们的代码不是,并且没有两个控制器的动作示例。我们如何实现类似的行为?

编辑- VRTK

简单地用布尔值保持每个触发器的状态:

bool triggerMainPressed;
bool triggerSecondaryPressed;

void Update()
{
    if (controllerMain.GetPressDown(triggerButton))
    {
        triggerMainPressed = true;
    }
    if(controllerSecondary.GetPressDown(triggerButton))
    {
        triggerSecondaryPressed = true;
    }

    if (controllerMain.GetPressUp(triggerButton))
    {
        triggerMainPressed = false;
    }
    if(controllerSecondary.GetPressUp(triggerButton))
    {
        triggerSecondaryPressed = false;
    }


    if(triggerMainPressed && triggerSecondaryPressed)
    {
        scaleSelected(gameObj); //enlarges selected GameObject based on distance between controllers
    }
    else if(triggerMainPressed && controllerSecondary.GetPressDown(gripButton))
    {
        deleteObject(gameObj); //delete selected GameObject
    }   
}

当你与对象交互时(使用抓取控制器)你知道哪个控制器在抓取,因此你可以通过检查现有控制器的手然后得到另一只手来找出另一个控制器,像这样:

GameObject otherController;
if(VRTK_DeviceFinder.IsControllerLeftHand(grabbingObject)
{
  otherController = VRTK_DeviceFinder.GetControllerRightHand();
}
else
{
  otherController = VRTK_DeviceFinder.GetControllerLeftHand();
}

这基本上会检查当前的抓取控制器,如果是左手,那么您需要右手(反之亦然)。

弓箭示例脚本显示了这一点,它们可以在示例目录中找到。