Immersive Reality Final

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.

  1. 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).

    Note: We refer to the assets, folders, and so forth by the names used in the Unity tutorial, so it is helpful to follow the names they use in their example.
  2. 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.

  3. Launch the new project and prepare the game scene.
    1. Launch Unity.
    2. Click Open and then select the Roll-a-ball-VR folder. The Unity editor appears.
    3. In the Project window, expand _Scenes and then select MiniGame.
    4. Press F2 and rename the scene "VRMiniGame."
    5. Open the scene "VRMiniGame."

Modify Roll-a-ball for VR

  1. Enable Virtual Reality Supported in Player Settings.
    1. On the Edit menu, select Project Settings >Player.
    2. In the Inspector window, locate the platform selection tabs.
    3. If developing for the PC, select the PC platform tab. It looks like a download icon.
    4. If developing for Android, select the Android platform tab. It looks like an Android icon.
    5. In the Other Settings pane, select the Virtual Reality Supported check box.
  2. 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.
    1. In the Hierarchy window, drag the Main Camera object to the Player object.
    2. Set Transform > Position to X:0Y:5Z:-5.
    3. Set Transform > Rotation to X:0Y:0Z: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.
    4. 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.
    5. Select the Player object.
    6. Locate the Rigidbody > Constraints > Freeze Rotation check boxes.
    7. Set the XY, 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.
  3. Enter Play mode in the Unity editor.

    The Unity Game window shows the image corresponding to the left eye buffer.

  4. Save your scene and project before building your application.
  5. Test your game on the VR.