Tutorial: Build a Simple VR Unity Game
This tutorial describes the steps necessary to build, load, and run a simple Unity 3D application on the Oculus Rift or Samsung Gear VR.
It is intended to serve as a basic introduction for developers who are new to VR development and to Unity. After the necessary tools are set up, this process should take a few minutes to complete. By the end, you will have a working mobile application that you can play and demonstrate on your Oculus Rift or Gear VR device, to the amazement of your friends and loved ones.
We will build and modify the Unity game Roll-a-ball to add VR capability. The game is controllable by keyboard or by gamepad.
- Oculus Rift or Gear VR with compatible Samsung phone
- Compatible gamepad (required for Mobile; optional for Desktop)
- PC running Windows 7, 8 or 10 or a Mac running OS X 10
- Unity 5 (see Compatibility and Requirements for specific version recommendations)
You will also need to refer to the relevant Oculus SDK documentation, available for download here: https://developer.oculus.com/documentation/
Installation and Preparation
- Install the appropriate Oculus SDK and prepare for development.
Desktop: Download and install the Oculus PC SDK and Utilities package from Oculus PC SDK Downloads. Prepare for development as described in the Oculus Rift Getting Started Guide. By the time you have completed this process, you should be able to run the Demo Scene as described in that guide.
Mobile: Download and install the Oculus Mobile SDK from Oculus Mobile SDK Downloads. Prepare for development as described by the Mobile SDK Setup Guide. By the time you have completed this process, you should be able to communicate with your Samsung phone via USB. To verify this, retrieve the device ID from your phone by connecting via USB and sending the command adb devices from a command prompt. If you are communicating successfully, the phone will return its device ID. You may wish to make a note of it - you will need it later to request a Oculus Signature File (see step four in Modify Roll-a-ball for VR for more information).
- Install Unity.
Check which version of the Unity editor you should download an install in our Compatibility and Version Requirements, then download the appropriate version here: http://docs.unity3d.com/Manual/index.html. Unity provides extensive documentation to introduce users to the environment. You may wish to begin by reviewing their documentation to gain a basic familiarity with core concepts such as the Editor, GameObjects, prefabs, projects, and scenes.
- Build the Unity Roll-a-ball application.
Unity provides a number of video tutorials that walk you through the process of creating a simple game. The first in the series provides instructions for creating the Roll-a-ball application, in which you use the keyboard or gamepad to control a ball that rolls around a game board and picks up floating token counters:http://unity3d.com/learn/tutorials/projects/roll-a-ball
The development process is covered in eight short video tutorials which run from around five to fifteen minutes in length. Allow for a few hours to complete the procedure.
The final video in the series, "107. Publishing the game," describes building the Roll-a-ball game for play in a web browser. You may skip this lesson if you wish for the purposes of this exercise, as we will follow a different procedure for building a playable application (PC/Mac) or APK (Android).
- Duplicate your Roll-a-ball project (optional).
Once you have completed building Roll-a-ball, you may wish to create a duplicate Roll-a-ball project specifically for VR development. It can be useful to retain a backup of the original unmodified Roll-a-ball project in case you make mistakes or wish to work with it later without the VR assets.
To duplicate the Roll-a-ball project, simply navigate in your OS to the Unity project folder containing your Roll-a-ball project, copy the folder and all of its contents, and rename it. For this tutorial, we will use the project folder name Roll-a-ball-VR.
- Launch the new project and prepare the game scene.
- Launch Unity.
- Click Open and then select the Roll-a-ball-VR folder. The Unity editor appears.
- In the Project window, expand _Scenes and then select MiniGame.
- Press F2 and rename the scene "VRMiniGame."
- Open the scene "VRMiniGame."
Modify Roll-a-ball for VR
- Enable Virtual Reality Supported in Player Settings.
- On the Edit menu, select Project Settings >Player.
- In the Inspector window, locate the platform selection tabs.
- If developing for the PC, select the PC platform tab. It looks like a download icon.
- If developing for Android, select the Android platform tab. It looks like an Android icon.
- In the Other Settings pane, select the Virtual Reality Supported check box.
- Modify the Main Camera and Player objects for VR. In the original Roll A Ball demo, the camera is an independent object that moves according to transforms in its script. However, VR cameras cannot be moved on their own. Cameras in VR must always have a parent object and they move only when their parent object is moved.
- In the Hierarchy window, drag the Main Camera object to the Player object.
- Set Transform > Position to X: 0 Y: 5 Z: -5.
- Set Transform > Rotation to X: 0 Y: 0 Z: 0. We reset the camera rotation to avoid artificial pitch. Artificially pitching the player's view forward might make someone feel they are about to fall.
- Clear the Camera Controller (Script) check box. We do not need a camera controller script any more because our camera now moves as a child of the Player object.
- Select the Player object.
- Locate the Rigidbody > Constraints > Freeze Rotation check boxes.
- Set the X, Y, and Z check boxes. We want the player sphere to still move, but to not rotate because our camera object is attached and would rotate too. Constraining the rotation stops this.
- Enter Play mode in the Unity editor.
The Unity Game window shows the image corresponding to the left eye buffer. If you are using the PC SDK, the Health and Safety Warning appears before the game is rendered in your headset; press any key to continue past it.
- Save your scene and project before building your application.
- Sign your application (Mobile Only).
To access your Samsung phone's VR capabilities, you will need to sign your application with an Oculus Signature File (osig). If you recorded your device ID earlier, you may use it now to request your osig file. Note that you need only one osig per mobile device.
You may obtain an osig from our self-service portal here: https://dashboard.oculus.com/tools/osig-generator/. Once you have received an osig, copy it to your Unity project folder in /Roll-a-ball-VR/Assets/Plugins/Android/assets/.
More information may be found on application signing in Application Signing in our Mobile guide.
Build and Play
Build and Launch
If you are developing for desktop, you build an executable file that you can launch with your PC or Mac. If you are developing for mobile, you will build an APK and load it onto your phone, and then insert the phone into the Gear VR to launch your game. Follow the build instructions as described in the Configuring for Build section.
Go ahead and try it out! You may use your keyboard or a paired Samsung gamepad to control your ball and collect the game pickup objects.
Launching the Game on Gear VR
If you select Apps from the Samsung home screen, you will see Roll-a-ball-VR listed with the other apps. You may launch the application directly, and when instructed, put the phone in the Gear VR. It will not be visible from Oculus Home.