Wednesday, 6 May, 2020 UTC


Summary

Build your first VR game with Oculus Quest and Unity — Part 2

Source
This is a continuation of my previous article, Build your first VR game with Oculus Quest and Unity — Part 1. There, we set up unity with necessary resources and configurations. In this article let's set the camera and then model our VR controllers. Finally, in Part 3 (which is the final article) let's build the complete game and deploy it into the Oculus Quest.
Alright, let's get started. If you followed Part 1 until the end, your Unity Window should be similar to Figure 1 below.
Figure 1
Let's first create our VR play area. To do that, go to Hierarchy panel and write click and create a New Empty Game Object and name it as VR_Rig and set its position to X:0, Y:0 and Z:0 (I will use 0,0,0 for make it shorter here onwards). Refer to Figure 2 below.
Figure 2
Next, click on Add Component in the Inspector panel while VR_Rig game object is selected to add a new component to our game object. Then type XR Rig and select the script as shown in Figure 3 and 4 below.
Figure 3Figure 4
Next, we need to create two Game objects as you can see that the XR Rig component needs two Game objects named Camera Floor Offset and Camera. To do this, go to Hierarchy panel and click on VR_Rig game object and then right-click on it and create a New Empty Game Object. This way we can create a child object of VR_Rig. See how this is displayed in the Hierarchy panel by referring to Figure 5 below.
Figure 5
Name it as Camera_Floor_Offset. Again make sure the position is set to (0,0,0). Refer to Figure 6.
Figure 6
Next, let's add a camera under Camera_Floor_Offset. To do that right-click on the Camera_Floor_Offset game object and select Camera in the dropdown. Refer to Figure 7 and 8 below.
Figure 7Figure 8
After that, we need to add Tracked Pose Driver to our Camera object. Go to the Hierarchy panel and select Camera object and then click on Add Component in the Inspector panel and then type Tracked Pose Driver and select it. Refer to Figure 9 and 10 below.
Figure 9Figure 10
Next, let's add our Camera_Floor_Offset and Camera objects to the relevant variables in VR_Rig. To do that, go to Hierarchy panel and select VR_Rig Object and then click and drag Camera_Floor_Offset object to Camera Floor Offset Object variable in the Inspector panel. Then the Camera object to the Camera Game Object variable. Refer to Figure 11 below.
Figure 11
Next, set the Tracking Origin Mode to Floor as in Figure 12 below.
Figure 12
Optionally if you have an Oculus link cable to connect your HMD (Head Mount Display) to the computer, you can hit the play button on Unity and test the functionality at this point. However, that is not necessary as we are going to build the whole game and going to deploy it to Quest.
Alright, now we finished setting up the camera. Let's start building the game. First, let's create a pane for our game. Again go to Hierarchy panel and right click and go to 3D Objects and select Pane. Make sure to set its position to (0,0,0) and Scale to (). Refer to Figure 13 and Figure 14.
Figure 13Figure 14
Now let's set some materials for it. To do this, go to Assets folder in the Project panel and right-click and select Create -> Material. Refer to Figure 15 below.
Figure 15
Name the material as Black. While this is selected go to the Inspector panel and select the colour which is located under the Surface Inputs. Refer to Figure 16 and 17 below.
Figure 16Figure 17
Click and drag the Black Material that you created to the Plane Object in the Hierarchy panel. Refer to Figure 18.
Figure 18
Next, we can create VR controller objects and map them to real VR controllers. To do this go back to the Hierarchy panel and then select the Camera_Floor_Offset object and right-click on it and create an Empty Game Object. Name it as Right_Controller. Refer to Figure 19 below.
Figure 19
While Right_Controller is selected go to Inspector panel and click Add Component and select XR Controller script. Refer to Figure 20 below.
Figure 20
In the XR Controller component, select Righ Hand from the dropdown of Controller Node. Refer to Figure 21 below.
Figure 21
Now do the same steps described as in Figure 18 to Figure 20 to create the Left_Controller similar to Right_Controller game object. Refer to Figure 22 below.
Figure 22
Next, we need to add a model to the controllers so that we can see them in the 3D space. For this, let's first create our 3D model. Go to the Hierarchy panel and then right-click and select 'Create empty' to create an empty game object and name it as Controller. You can set the positions as in Figure 23 below.
Figure 23
After that create a child cylinder object under the Controller object. To do that, select the Controller object and right-click on it and select 3D Object and Cylinder. Again position and scale can be set as in Figure 24 below.
Figure 24
As described in Figure 15, 16 and 17 create a new white Material and set it to the cylinder. Refer to Figure 25 below.
Figure 25
Next is to create the Controller prefab and then set it as the model prefab of Right_Controller. To do this, go to Hierarchy panel and click and drag the Controller object to the Assets folder.

Trending AR VR Articles:

1. Ready Player One : How Close Are We?
2. Augmented Reality — with React-Native
3. Five Augmented Reality Uses That Solve Real-Life Problems
4. Virtual Reality Headsets: What are the Options? Which is Right For You?
After that, we can delete Controller object from the Hierarchy panel. After that, select Right_Controller object in the Hierarchy panel. While it is been selected go to the Inspector panel and find Model Prefab variable under the Model section. This is the place where you need to drag and drop our Controller prefab. Refer to Figure 26 below.
Figure 26
Similarly, add the Controller prefab to the Left_Controller objects model prefab. Refer to Figure 27 below.
Figure 27
Now we have finished our controller modelling. Next step is to model the game. Again the article got very long, thus, take a small break and jump to the final part of this series to finish and deploy the game into Oculus Quest.
After your short break, continue reading 'Build your first VR game with Oculus Quest and Unity — Part 3'.
See you in the next article!
If you want to thank me or if you like the article;
Cheers!

Don’t forget to give us your 👏 !

https://medium.com/media/1e1f2ee7654748bb938735cbca6f0fd3/href
Build your first VR game with Oculus Quest and Unity — Part 2 was originally published in AR/VR Journey: Augmented & Virtual Reality Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.