This page has been translated automatically.
Unigine Basics
1. Introduction
2. Managing Virtual Worlds
3. Preparing 3D Models
4. Materials
5. Cameras and Lighting
6. Implementing Application Logic
7. Making Cutscenes and Recording Videos
8. Preparing Your Project for Release
9. Physics
10. Optimization Basics
11. PROJECT2: First-Person Shooter
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.

Last update: 2024-12-13
Build: ()