The first step to making a virtual reality (VR) game is activating VR Mode. In this tutorial, you will learn how to integrate a VR software development kit (SDK). If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!
What is VR Mode?
Create a new Unity project named “VRInteractions”. Save the file to your Project folder. Leave 3D rendering enabled.
In the file that opens, select Main Camera from the Hierarchy. Notice that in the Scene window, there are gray lines protruding from the camera. These lines outline the area that the camera renders.
Create a cube in the Hierarchy. Put the cube in the position 0 0 0. Select Main Camera. In the Scene, you will be able to see that the cube is inside the rendering area of Main Camera. The cube is inside the shape made by the gray lines protruding from Main Camera.
If you move the cube outside the area outlined by the gray lines, the camera will not render the cube. The player will not be able to see the cube.
A VR game contains two cameras to represent two human eyes. The left camera shows the scene from the player’s left eye. The right camera shows the scene from their right eye. With the help of a VR device, dividing the view into two cameras makes feel as though they are in a virtual world.
Delete Cube and Main Camera (1) from the Hierarchy. There are multiple ways to create a VR environment. We will use the SDK of Google Cardboard. Google Cardboard is a VR platform in which a user places a smartphone into a cardboard viewer. When the user looks into the viewer, they see the game on their phone in 3D.
If you do not have Google Cardboard, you can change the SDK settings to disable VR Mode. You will be able to play the game on your phone without VR Mode.
To download Google Cardboard, go to www.developers.google.com/vr/unity. You will reach the page “Google VR SDK for Unity”. This page provides details about what the features of this SDK.
The bottom of the page contains useful links. Click on the “Unity API Reference” link to open a page containing details on how to implement the SDK.
You can look through this page to see details regarding the coding of the SDK, such as the available classes.
To download the SDK, click on the “Google VR SDK for Unity” link at the bottom of www.developers.google.com/vr/unity.
To integrate the SDK into the Unity project, you could clone the entire repository hosted on GitHub. Alternatively, click on the “gvr-unity-sdk” link.
You will be taken to a GitHub webpage. Click on “GoogleVRForUnity.unitypackage”. On the page that loads, press the Download button to download the SDK.
Double-click on the file that appears in your Downloads folder. Once the files decompress, an Import Unity Package window will open.
A Unity Package is like a zip file. It is a compressed file that contains many other files inside it.
You can see that the file contains scripts, materials, images, prefabs, and more. Instead of making our own plug-ins, we can import the ones in this package and adjust them. Press the Import button. Once the assets import, all the necessary files for activating the VR Mode will be in your Assets folder.
Depending on the Unity or SDK version you are using, you may be prompted to perform another import to ensure compatibility. You can press Import Package > Import to import the second package.
Want to learn more? Check out our Unity3D course, where you build 30 virtual reality games!