How to set up Unity for VR

About Unity

Unity is a game development engine used to develop applications across various platforms. Among other things, it is also used to develop virtual reality (VR) applications.

In this answer, we have used the 2021.3.6f1 version of Unity. The configuration and settings described below may vary for later versions of Unity.

Set up Unity for VR

Firstly, we add the "Android Build Support" module to our installed version of the Unity Editor as shown below:

Click on the “Installs” tab and then on the gear icon next to the installed version of Unity
1 of 3

Install XR Plugin Management

Next up, we need to install the "XR Plugin Management" by navigating to "Edit > Project Settings".

1 of 3

Install XR Interaction Toolkit

The next step is to install the XR Interaction Toolkit. This can be done by the following method:

  1. Open "Package Manager" by clicking "Window > Package Manager".

  2. Click the "+" icon in the top left corner.

  3. Now either select "Add package from git URL" or "Add package by name".

  4. We need to enter com.unity.xr.interaction.toolkit in the window that pops up and click "Add".

  5. After the package is installed, we need to select it from the list of installed packages and import the files given under "Samples" as shown below:

1 of 4

Set up the scene

Now we'll set up a basic scene to view in virtual reality. For starters, we will just add a cube to the scene. This can be done by right-clicking in the "Hierarchy" window and then selecting "3D Object > Cube".

We also need to add XR Origin (VR) to the scene. This can be done by right-clicking in the Hierarchy window and then selecting "XR -> XR Origin (VR)".

XR Origin acts as the center of the world space when the scene is viewed in our headset. It has a child object called Camera Offset, which has three more children: Main Camera, LeftHand Controller, and RightHand Controller.

To add interactivity to the XR Origin, we need to use the Input Action Manager. We need to first add it as a component to the XR Origin. After adding it as a component, we will set up the input action assets by adding the sample scripts we imported earlier. Setting up the Input Action Manager deals with basic inputs from the head-mounted device, such as head tracking and controller interaction, etc. This step can be done as shown below:

Click on Add Component while the XR Origin is selected
1 of 4

We also need to add the XR Controller Components that we imported earlier as components to the XR input device so that input from the controllers can be read. This task can be achieved as shown below:

Click on the Preset selector (the slider icon) for the XR Controller component while the LeftHandController is selected inside the Hierarchy window
1 of 2

The same needs to be done for the RightHand Controller. It sets the imported XRI Left and Right Controllers and sets the appropriate references for the controllers. This allows the user to use the controllers and perform essential interactions.

Moreover, we need to make sure that the Preset Manager has the left and right controllers mapped correctly. We can do this by heading to "Edit -> Project Settings -> Preset Manager". Under "ActionBasedController", we need to make sure that the Right and Left controllers are mapped to XRI Default Right and Left Controllers, respectively, as shown below:

widget

To render the scene inside the VR Headset, we need to build and run it. To do so, navigate to "File -> Build Settings".

Inside the window that pops up, we need to switch the Platform to "Android" and make sure that our "Run Device" is configured as shown below:

widget

We switched the platform to "Android" and set the "Run Device" to the version of our HMD (head-mounted device).

Output

Finally, by clicking "Build and Run", we'll be able to render the scene inside our HMD. It will look like the following. Here, we can view the cube we added inside the scene:

Copyright ©2024 Educative, Inc. All rights reserved