Controller
Prerequisites
You’ll need to have compiled Unreal Engine 5 with the plugin source included in the Magic Leap 2 Unreal SDK. We showed you how to build that in Getting Started with Unreal Engine.
You'll need a basic project set up with the Magic Leap OpenXR plugin enabled. Create an app using Unreal Engine 5.
Plugin
A Magic Leap Controller plugin is included in the sdk. To use ML2 controller input, enable the plugin using the Plugins browser accessible from the Edit menu in the Unreal editor.
Controller Pose Tracking
When the plugin is enabled, the MagicLeap 2 controller will be detected as a MotionController by Unreal.
See Unreal documentation on motion controllers for more details-
https://dev.epicgames.com/documentation/en-us/unreal-engine/motion-controller-component-setup-in-unreal-engine
To track the controller pose, open the blueprint for the player pawn and add a "MotionController" component to the component hierarchy. You can optionally render a 3d model of the MagicLeap controller when it is detected simply by adding an XRDeviceVisualization component as a child of the MotionController component in the hierarchy.
The tracker will detect whether the controller is being held in the user's left or right hand, with Left being the default. If you would like to track the controller when the user is holding it in either hand, select the component in the hierarchy and set "AnyHand" as the Motion Source in the details panel.
Input Mapping
You can use the Enhanced Input plugin in UE 5.3 to address all of the other inputs on the MagicLeap controller.
The plugin exposes a set of EKeys for all of the various controller inputs that can be bound to Input Actions using an Input Mapping Context.
The following inputs are available-
Name | Type |
---|---|
MagicLeapController_Left_Menu_Click MagicLeapController_Right_Menu_Click | bool |
MagicLeapController_Left_Trigger_Click MagicLeapController_Right_Trigger_Click | bool |
MagicLeapController_Left_Trigger_Axis MagicLeapController_Right_Trigger_Axis | float (value range [-1, 1]) |
MagicLeapController_Left_Trackpad_X MagicLeapController_Right_Trackpad_X | float (value range [-1, 1]) |
MagicLeapController_Left_Trackpad_Y MagicLeapController_Right_Trackpad_Y | float (value range [-1, 1]) |
MagicLeapController_Left_Trackpad_2D MagicLeapController_Right_Trackpad_2D | Vector2D (component value range [-1, 1]) |
MagicLeapController_Left_Trackpad_Click MagicLeapController_Right_Trackpad_Click | bool |
MagicLeapController_Left_Trackpad_Force MagicLeapController_Right_Trackpad_Force | float (value range [0, 1]) |
MagicLeapController_Left_Shoulder_Click MagicLeapController_Right_Shoulder_Click | bool |
Note that both Left and Right variants of each input are provided. If you would like to create an input binding that works when the user is holding the controller in either hand, then you will need to add both of them when you create the Input Mapping Context.
Setting Up Enhanced Input in Blueprint
Enhanced Input can be set up in the Pawn blueprint using the following steps:
- Open the Pawn Blueprint class
- Inside the Blueprint, connect the Event BeginPlay node to the AddMappingContext node.
- Add a Get Controller node and connect it to a Cast to Player Controller node
- Connect the Cast to Player Controller node to the Enhanced Input Local Player Subsystem node
- Connect the Enhanced Input Local Player Subsystem node into the Add Mapping Context Target pin
- Select the IMC_Default (or your preferred Input Mapping Context asset) as the Mapping Context in the Add Mapping Context node. We will edit the input mapping context asset in the following section.
Use the Blueprint image from the Examples project as a guide:
Editing the input mapping context to use the Magic Leap Controller
The Unreal Engine Input Mapping Context assets use Input Actions that can be configured to work with a variety of controllers. This step will demonstrate how to add the Magic Leap controller to the mapping context.
- Open the Input Mapping Context asset you would like to use the Magic Leap controller with.
- Under the Mappings dropdown, select an input action to add the Magic Leap Controller to.
- Under the input selection dropdown, select Magic Leap > Magic Leap Controller > Magic Leap (L) Trigger.
Now this input action will receive input from the Magic Leap controller trigger.
For more information, visit the Unreal Engine documentation on Enhanced Input or check out the Pawn blueprint class inside the Magic Leap Unreal Examples project.