将控制器的握把键更改为扳机键
You might want to remap actions or swap the controls in the VR application. As an example, let's do this for the controller. We will remap the Use action from the Grip button to Trigger, and the grab action to the Grip button.你可能希望在 VR 应用中重新映射动作或切换控制方式。下面以控制器为例,我们将把 Use 动作从 Grip 键改为 Trigger,而将抓取(grab)动作分配给 Grip 键。
User input for the controller is defined in the following methods of the VRPlayerVR component (Framework\Components\Players\VRPlayerVR.cpp):控制器的用户输入在 VRPlayerVR 组件(Framework\Components\Players\VRPlayerVR.cpp)的以下方法中定义:
- getControllerButtonPressed(int controller_num, VRPlayer::BUTTONS button)
- getControllerButtonDown(int controller_num, VRPlayer::BUTTONS button)
- getControllerButtonUp(int controller_num, VRPlayer::BUTTONS button)
To remap actions, you can simply swap VRPlayer::GRAB and VRPlayer::USE in the switch statements inside the methods listed above:若要重新映射操作,只需在上述方法内的 switch 语句中交换 VRPlayer::GRAB 和 VRPlayer::USE 即可:
// ...
int VRPlayerVR::getControllerButtonPressed(int controller_num, VRPlayer::BUTTONS button)
{
if (!controller_valid[controller_num])
return 0;
InputVRControllerPtr controller = (controller_num == 0 ? left_controller_device : right_controller_device);
if (!controller)
return 0;
switch (button)
{
case VRPlayer::TELEPORT:
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRACKPAD_X);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_JOYSTICK_X);
if (axis_index != -1)
return controller->isButtonPressed(Input::VR_BUTTON(Input::VR_BUTTON_AXIS_0 + axis_index));
}
${#HL}$ case VRPlayer::GRAB: ${HL#}$
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_VALUE);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_FORCE);
if (axis_index != -1)
return controller->getAxis(axis_index) > 0.5f;
}
${#HL}$ case VRPlayer::USE: ${HL#}$
return controller->isButtonPressed(Input::VR_BUTTON_GRIP);
case VRPlayer::MENU:
return controller->isButtonPressed(Input::VR_BUTTON_APPLICATION) || controller->isButtonPressed(Input::VR_BUTTON_Y);
}
return 0;
}
int VRPlayerVR::getControllerButtonDown(int controller_num, VRPlayer::BUTTONS button)
{
if (!controller_valid[controller_num])
return 0;
InputVRControllerPtr controller = (controller_num == 0 ? left_controller_device : right_controller_device);
if (!controller)
return 0;
switch(button)
{
case VRPlayer::TELEPORT:
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRACKPAD_X);
if(axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_JOYSTICK_X);
if(axis_index != -1)
return controller->isButtonDown(Input::VR_BUTTON(Input::VR_BUTTON_AXIS_0 + axis_index));
}
${#HL}$ case VRPlayer::GRAB: ${HL#}$
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_VALUE);
if(axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_FORCE);
if(axis_index != -1)
return controller->getAxis(axis_index) > 0.5f;
}
${#HL}$ case VRPlayer::USE: ${HL#}$
return controller->isButtonDown(Input::VR_BUTTON_GRIP);
case VRPlayer::MENU:
return controller->isButtonDown(Input::VR_BUTTON_APPLICATION) || controller->isButtonDown(Input::VR_BUTTON_Y);
}
return 0;
}
int VRPlayerVR::getControllerButtonUp(int controller_num, VRPlayer::BUTTONS button)
{
if (!controller_valid[controller_num])
return 0;
InputVRControllerPtr controller = (controller_num == 0 ? left_controller_device : right_controller_device);
if (!controller)
return 0;
switch (button)
{
case VRPlayer::TELEPORT:
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRACKPAD_X);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_JOYSTICK_X);
if (axis_index != -1)
return controller->isButtonUp(Input::VR_BUTTON(Input::VR_BUTTON_AXIS_0 + axis_index));
}
${#HL}$ case VRPlayer::GRAB: ${HL#}$
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_VALUE);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_FORCE);
if (axis_index != -1)
return controller->getAxis(axis_index) < 0.5f;
}
${#HL}$ case VRPlayer::USE: ${HL#}$
return controller->isButtonUp(Input::VR_BUTTON_GRIP);
case VRPlayer::MENU:
return controller->isButtonUp(Input::VR_BUTTON_APPLICATION) || controller->isButtonUp(Input::VR_BUTTON_Y);
}
return 0;
}
// ...
Save your changes, then build and run the application by hitting Ctrl + F5 to update component's logic. Close the application after running it and switch to UnigineEditor.保存更改后,按下 Ctrl + F5 构建并运行应用程序,以更新组件逻辑。运行结束后关闭应用程序,并切换回 UnigineEditor。
Switch to SDK Browser and launch our application by clicking the Run button on the project's card.切换到 SDK Browser,在项目卡片上点击 Run 按钮启动我们的应用程序。
Now you can grab objects by the trigger, and use them by the Grip side button.现在你可以通过扳机键抓取物体,并通过 Grip 侧键使用它们了。
本页面上的信息适用于 UNIGINE 2.20 SDK.