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.
Firstly, we add the "Android Build Support" module to our installed version of the Unity Editor as shown below:
Next up, we need to install the "XR Plugin Management" by navigating to "Edit > Project Settings".
The next step is to install the XR Interaction Toolkit. This can be done by the following method:
Open "Package Manager" by clicking "Window > Package Manager".
Click the "+" icon in the top left corner.
Now either select "Add package from git URL" or "Add package by name".
We need to enter com.unity.xr.interaction.toolkit
in the window that pops up and click "Add".
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:
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:
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:
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:
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:
We switched the platform to "Android" and set the "Run Device" to the version of our HMD (head-mounted device).
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:
Free Resources