Tutorial: Build Your First VR App

Important

All Oculus Quest developers MUST PASS the concept review prior to gaining publishing access to the Quest Store and additional resources. Submit a concept document for review as early in your Quest application development cycle as possible. For additional information and context, please see Submitting Your App to the Oculus Quest Store.

This short tutorial walks you through creating and running a simple Unity.

Requirements

Preparation

Before beginning, you will need to set up your development environment.

Follow the instructions in Preparing for Android Development.

Build Your Simple Application

We are going to build a simple game with a play area that consists of a floor and some walls. Then we’ll add a ball and attach a controller script so we can move it around the play area with a keyboard or gamepad. Finally, we’ll add VR support so we can run the game on Rift, Oculus Go, or Gear VR.

So, let’s get started without any further ado.

Part 1: Build a Play Area and Add a Player

In this part of the tutorial, we’ll build a simple play area consisting of a floor and four walls, and add a sphere as a player.

  1. Launch the Unity Editor and, in the initial launch dialog, create a new project by clicking New. Give it a creative name like VRProject, and select a location for the files to live in. Verify that the 3D option is selected, and click Create Project.
  2. Save the Scene. a. Select the File pulldown menu, and click Save Scene as…. b. Give the scene a creative name like VRScene.

  3. Let’s create a floor. a. In the GameObject pulldown menu, select 3D Object > Plane. b. In the Inspector, verify that the Position is set to origin (0,0,0). If not, set it to origin by selecting the gear icon pulldown in the upper-right of the Transform section of the Inspector and clicking Reset. c. Find Plane in the Hierarchy View and right-click it. Select Rename in the context menu, and rename it Floor.

  4. Now we’ll create the first wall. a. In the GameObject pulldown menu, select 3D Object > Cube. b. If the cube isn’t at origin, reset its Transform like we did in the preceding step. c. Longify the cube by setting the X value of Scale to 10 under Transform. d. Move it up slightly by setting the Y value of Position to .5 under Transform. It should now rest upon the floor. e. Find Cube in the Hierarchy View and right-click it. Select Rename in the context menu, and name it Wall1.
  5. Move the wall to the outer edge of the play area. a. Select Wall1 in the Hierarchy View or in the Scene View. b. In the Inspector, set the Z value of Position to 5 under Transform.

  6. Make a second wall and move it to the opposite edge of the play area. a. Right-click Wall1 in the Hierarchy View and select Duplicate in the context menu. You will see a second wall named Wall1 (1). b. Right-click Wall1 (1) in the Hierarchy View. Select Rename in the context menu, and rename it Wall2. c. Select Wall2 in the Hierarchy View or in the Scene View. d. In the Inspector, set the Z value of Position to -5 under Transform.
  7. Make a third wall, rotate it, and move it into place. a. Right-click Wall1 in the Hierarchy View and select Duplicate in the context menu. You will see a wall named Wall1 (1). b. Right-click Wall1 (1) in the Hierarchy View and select Rename in the context menu, and rename it Wall3. c. Select Wall3 in the Hierarchy View or in the Scene View. d. In the Inspector, set the Y value of Rotation to 90 under Transform. e. Select Wall3 in the Hierarchy View or in the Scene View. f. In the Inspector, set the X value to 4.5 and the Z value to 0 in Position, under Transform.

  8. Make a fourth wall and move it into place. a. Right-click Wall3 in the Hierarchy View and select Duplicate in the context menu. You will see a wall named Wall3 (1). b. Right-click Wall3 (1) in the Hierarchy View and select Rename in the context menu. Name it Wall4. c. Select Wall4 in the Hierarchy View or in the Scene View. d. In the Inspector, set the X value of Position to -4.5 under Transform.

  9. Now we have a play area with a floor surrounded by walls. Let’s add a sphere player. a. In the GameObject pulldown menu, select 3D Object > Sphere. b. In the Inspector, set the Position to 0, .5, 0. c. Right-click Sphere in the Hierarchy View and select Rename in the context menu. Name it Player.
  10. Adjust your camera so we can see the play area better. a. Select Main Camera in the Hierarchy View. b. In the Inspector, set Position to 0, 5, 0 and Rotation to 20, 0, 0 under Transform.

Part 2: Add a control script to your Player

In this part of the tutorial, we will prepare the Player so we can control its movement programmatically, based on user input from keyboard or gamepad.

  1. Add a RigidBody component to the Player. This will allow us to move it (for more details, RigidBody in Unity’s manual). a. Select Player in the Hierarchy View or in the Scene View. b. In the Inspector, click Add Component at the bottom. Select the Physics category, then select RigidBody.

  2. Create a new script, which we will use to control the Player. a. Select Player in the Hierarchy View or in the Scene View. b. In the Inspector, click Add Component at the bottom. Select the New Script category. c. Set the name of our new script to PlayerController, and set the language to C Sharp. Click Create and Add. d. Set Speed to 5. e. Right-click Player Controller (Script) in the Inspector, and select Edit Script in the context menu. This will launch the editor Unity associates with editing C# scripts (MonoDevelop by default). You should see the following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class temp : MonoBehaviour {
// Use this for initialization
    void Start () {
    }
// Update is called once per frame
    void Update () {
    }
}

  1. Add a new function to move your Player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public int speed = 0;
// Use this for initialization
   void Start () {
  }
// Update is called once per frame
   void Update () {
// get input data from keyboard or controller
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
// update player position based on input
      Vector3 position = transform.position;
      position.x += moveHorizontal * speed * Time.deltaTime;
      position.z += moveVertical * speed * Time.deltaTime;
      transform.position = position;
      }
  }

At this point if you preview your game in the Game View by pressing the Play button, you’ll find you can control the Player with the arrow keys or W-A-S-D on your keyboard. If you have a Unity-compatible gamepad controller you can control it with a joystick. Try it out!

Part 3: Modify your project for VR

In this step we’ll enable VR support in Player Settings.

  1. In the Edit menu, select Project Settings > Player.
  2. 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.

Android Platform

  1. In Other Settings > Rendering, select the Virtual Reality Supported check box.

That’s it! That’s all you need to do to make your game into a VR application.

If you have a Rift, go ahead and try it out. Press the Play button to launch the application in the Play View. If the Oculus app is not already running, it will launch automatically, as it must be running for a VR application to play.

Go ahead and put on the Rift and accept the Health and Safety Warnings. The game will be viewable in the Rift, and the Unity Game View will show the undistorted left eye buffer image.

Save your scene and project before proceeding.

Play

If you are developing for mobile, follow the Preparing for Android Development instructions to build an APK and load it onto your device, then launch your game.

Once you have run your application, it will then be available in your Oculus Library, and you may re-launch it from there.