Unity MR Part 5: Left and Right Controllers
Tags: unity3d, mixedreality, tutorial, beginners
Published: 2024-01-11 08:50:53

👀 Stumbled here on accident? Start with the [introduction](https://dev.to/taikonauten/part-0-introduction-56fl)!

📚 This article will teach you how to set up the left and right controllers for the Meta Quest 3 in Unity. This setup is essential for enabling interaction with various elements within your Unity project, enhancing the user experience on the Meta Quest 3 platform.

---

ℹ️ If you find yourself facing any difficulties, remember that you can always refer to or download the code from our accompanying [GitHub repository](https://github.com/taikonauten/unity-mr-article-series/tree/main/05_LeftAndRightControllers)

---

To keep the process straightforward, we'll use the starter assets from the `XR Interaction Toolkit` plugin. This approach simplifies the setup process, focusing on achieving a practical outcome quickly and efficiently.

---

## Import XR Interaction Toolkit Starter Assets

We will utilize some of the `XR Interaction Toolkit` starter assets in this article. Go to the `Package Manager` via **Window → Package Manager** and import `Starter Assets` which are located in the tab `Samples` as seen in the next screenshot.

You can locate the XR Interaction Toolkit in the `Package Manager` by accessing `Unity Registry` and searching for 'interact'.


![Importing Starter Assets for the XR Interaction Toolkit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6tcoux8w24pndhe0abo.png)
<figcaption>Importing Starter Assets for the XR Interaction Toolkit</figcaption>
---

## Enable the new Input System

ℹ️ The New Input System in Unity is a flexible and extensible system designed to replace the older input methods. It provides a more unified and consistent way to handle different input devices like keyboards, mice, gamepads, and touchscreens. This system allows for more customized and complex input configurations, making it easier to develop games and applications that can be controlled across various platforms and devices.

Go to your `Project Settings` and enable the new input system. You’ll find it under `Active Input Handling` under the section `Player` and the tab `Other Settings`.


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eav59hs34kqv50tlzdk.png)

After selecting the new input system you will be asked to restart the Unity editor.


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dazsmopigejqkutm9rx.png)

After restarting, the new input is now active. No additional steps required for now.

---

## Adding the AR Input Manager

ℹ️ The AR Input Manager in Unity's AR Foundation is a component that manages input in AR applications. It serves as a central point for handling AR-specific input data, such as touch gestures, camera movement, and device orientation. This manager is essential for creating interactive and user-responsive AR experiences, as it efficiently processes and translates real-world user interactions into actions within the AR environment.

Select the `Session` GameObject in your hierarchy and add the `AR Input Manager` via `Add Component`.


![Our Session GameObject with the added AR Input Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/59ocb5vvrcm02w0p6nq7.png)
<figcaption>Our Session GameObject with the added AR Input Manager</figcaption>
---

## The Input Action Manager

The Input Action Manager in Unity is a component that handles input actions within the New Input System. It allows developers to define and manage complex input actions like button presses, gestures, or movement inputs. This manager makes it easier to organize and respond to various user inputs in a structured and efficient manner, crucial for creating responsive and interactive gaming or application experiences.

ℹ️ For those interested in exploring the Input Manager in greater detail, additional information is available in the provided reference. This resource offers a comprehensive look into the functionalities and applications of the Input Manager, enhancing your understanding and proficiency in utilizing this component in your projects. [Input System | Input System | 1.7.0](https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/manual/index.html).

Select the `XR Origin (XR Rig)` GameObject in your hierarchy.


![Add XRI Default Input Actions to the list of Action Assets
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhx1h9i9znot2as9tngk.png)
<figcaption>Add XRI Default Input Actions to the list of Action Assets
</figcaption>
---

In the screenshot provided, you'll notice that we've added `XRI Default Input Actions` to the list of `Action Assets`. These assets were included as part of the `Starter Assets` from the `XR Interaction Toolkit` plugin, highlighting how the toolkit simplifies the integration of default input actions into your project.

If you're curious about the specific Input Actions included in the Action Asset, simply double-click on the selected action asset. This action will reveal the details and configurations of the Input Actions, providing a clear understanding of the functionalities they encompass.


![XRI Default Input Actions in detail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilf3z2nlj6tb5gv44y4c.png)
<figcaption>XRI Default Input Actions in detail</figcaption>
---

## Adding XR Input Modality Manager

The `XR Input Modality Manager` in Unity dynamically switches between hands and controllers during runtime, depending on their tracking status.

*   if hands start being tracked, it switches to hand interactors.
    
*   if motion controllers are activated, it switches to controller interactors once they're tracked.
    

📚 This component is valuable even without hand tracking, as it deactivates controller GameObjects until they are tracked, preventing them from appearing by default.

Select the `XR Origin (XR Rig)` GameObject in your hierarchy and add the `XR Input Modality Manager` via `Add Component`. Select `Left Controller` and `Right Controller` as seen in the screenshot.


![Selecting the left and right controller for the XR Input Modality Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dr6nf750b8b5jcnlxm49.png)
<figcaption>Selecting the left and right controller for the XR Input Modality Manager</figcaption>
---

## Choose a controller preset

For configuring the left and right controllers, we choose a controller preset imported through the starter assets. In your hierarchy select the GameObject `Left Controller`. To select a preset, click on the two sliders as depicted in the accompanying screenshot. This step is essential for customizing the controller settings in your project, ensuring they align with the imported presets for optimal functionality and user experience.


![Open the preset window for the left controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/va3ny8u8nv2jfxdsxq0p.png)
<figcaption>Open the preset window for the left controller</figcaption>
---

![Selecting a preset for the left controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p12qtuox5r41odoqzjzm.png)
<figcaption>Selecting a preset for the left controller</figcaption>
---

Similarly, for the Right Controller GameObject, follow the same process and select the corresponding preset for the right controller. This ensures that both controllers are appropriately configured with their respective presets, maintaining consistency and functionality in your setup.

---

## Choose an Interaction Manager

For the final step in setting up our controllers, we need to assign the Interaction Manager to both the left and right controllers. As shown in the forthcoming screenshot, select `XR Interaction Manager` as the `Interaction Manager`. This action links the controllers to the central system that manages interactions, ensuring they function correctly within the XR environment.

ℹ️ `Interaction Manager`: The XR Interaction Manager that this interactor will communicate with.


![Selecting the XR Interaction Manager as the Interaction Manager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8iqdqz7e3gg5a8443z4.png)
<figcaption>Selecting the XR Interaction Manager as the Interaction Manager</figcaption>
---

Similarly, for the Right Controller GameObject, follow the same process and select `XR Interaction Manager` as the `Interaction Manager`.

---

## Testing the app

Run your Unity project via `Build And Run` as explained in the last article. You should now see the Unity project running in your headset as seen in the next screenshot. Move your Meta Quest 3 controllers to verify if everything is working as expected.


![Testing the app if our controllers work](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38kvujag1jqfdrx9g1c5.png)
<figcaption>Testing the app if our controllers work</figcaption>
---

## Next article

In our next article, we will delve into the aspect of detecting planes in MR environments. Plane detection is a cornerstone of MR, allowing virtual content to be realistically placed and interacted with in the physical world. We will explore how this technology works and the ways you can implement plane detection in your projects using Unity.