This page has been translated automatically.
UNIGINE 基础课程
1. 简介
2. 虚拟世界管理
3. 3D模型准备
4. 材质
5. 摄像机和光照系统
6. 实现应用程序逻辑
7. 制作过场动画与动画序列
8. 准备发布项目
9. 物理系统
10. 优化基础
11. 项目2:第一人称射击游戏
12. PROJECT3: Third-Person Cross-Country Arcade Racing Game

Changing Controller Grip Button to Trigger

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.

User input for the controller is defined in the following methods of the VRPlayerVR component (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:

Source code (C++)
// ...

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.

Switch to SDK Browser and launch our application by clicking the Run button on the project's card.

Now you can grab objects by the trigger, and use them by the Grip side button.

The information on this page is valid for UNIGINE 2.20 SDK.

Last update: 2024-11-06
Build: ()