VR Overview

Checked with version: 5.3

-

Difficulty: Beginner

Unity VR Introduction

Unity has introduced built-in support for certain VR devices. This guide will focus on the Oculus family of VR devices, notably the Oculus Rift Development Kit 2 (DK2) and the consumer edition of the Gear VR (a mobile headset which requires a Samsung Galaxy S6, S6 Edge, S6 Edge+, or Note 5 handset). We are not focussing on the Note 4, which was previously supported by the first Innovator Edition of the GearVR, though we expect that the VR samples will run on this device (albeit with lower performance) for those of you that have it.

Other VR Head Mounted Displays (HMDs) will also work with Unity, such as the HTC Vive, and this documentation will be updated in future to cover additional VR platforms.

While most of this content will be relevant for all VR HMDs, please see the manufacturer's documentation for more details.

Enabling VR in Unity projects

Please be sure that you have the Oculus runtime 0.8 installed, along with Unity 5.3 or higher.

VR support is enabled by visiting Edit > Project Settings> Player > Other Settings > Rendering.

Enable VR Menu

Then enabling the “Virtual Reality Supported” checkbox in the Inspector.

Enable VR Settings

During runtime, this can be toggled using the UnityEngine.VR.VRSettings.enabled property in code, as shown below:

Code snippet

using UnityEngine;
using UnityEngine.VR;

public class ToggleVR : MonoBehaviour
{
    //Example of toggling VRSettings
    private void Update ()
    {
        //If V is pressed, toggle VRSettings.enabled
        if (Input.GetKeyDown(KeyCode.V))
        {
            VRSettings.enabled = !VRSettings.enabled;
            Debug.Log("Changed VRSettings.enabled to:"+VRSettings.enabled);
        }
    }
}

Previewing VR in Unity

When VR Support is enabled in the Unity Editor and a DK2 is connected, entering Play mode will display the Game view on the DK2, as well as in the editor. This allows for much quicker testing and iteration - there is no need to build an executable of the project to see your changes in VR. Developers who have previously created VR content will notice the side-by-side view is now replaced with a single image.

Enable VR Cube

Please note that there is no need to create one camera per-eye; all cameras will render in VR, except for those with a Render Texture assigned. For more information on Render Textures, please see the manual. Optimisations are automatically applied to make rendering both cameras less expensive, such as culling and rendering shadows once for both eyes.

Additional documentation on VR topics can be found in the Unity Manual, and Unity Scripting API.

Hardware and Software Recommendations for VR development in Unity

Because VR is a very new medium, at the time of writing there are some hardware and software limitations, which are outlined below.

Hardware

Achieving the required frame rate for the HMD is essential for a good VR experience, and this must match the refresh rate of the panel used in the HMD. On DK2 this must be 75fps, and for the Gear VR, it must be 60fps. If the frame rate drops below this, it is particularly noticeable to the user, and will often lead to nausea.

While achieving a consistent frame rate is essential, the GPU in the attached PC must also be capable of outputting the required resolution at the HMD panel’s refresh rate. In the case of DK2, this is 1920 x 1080 at 75hz. Bear in mind that if you’re intending to develop for the commercially released Oculus Rift (CV1), this has a 2160 x 1200 resolution at 90hz, which will be more demanding than the DK2.

If you are using the DK2, please make sure that your hardware is capable of outputting the required resolution at the required refresh rate. Many laptops use a chipset to switch between the discrete and integrated chipsets, and these chipsets are usually unable to output 1920 x 1080 at 75hz, leading to a sub-optimal VR experience, and nausea. Please refer to the Oculus documentation, and the Oculus Ready PCs guide for recommended hardware.

Software

OS X: At this time, it’s possible to develop on OSX 10.9+ with the Oculus 0.0.5 runtime, but as Oculus have paused development for OS X, we recommend Windows for native VR functionality in Unity.

Windows: Windows 7, 8, 8.1, and Windows 10 are all compatible.

Android: We recommend using Android OS Lollipop 5.1 or higher.

Graphics card drivers: Please ensure your drivers are up to date. Older drivers may not supported. Please check the Oculus Configuration Utility to see if there are issues with your driver.

Oculus Runtime: To use VR support in Unity 5.3, please use Oculus Runtime 0.8 or higher.

VR Samples Project

As part of this tutorial topic we have supplied a sample VR project available for free on the Asset store. The Unity VR Samples project will run on both DK2 and Gear VR, and we will be updating it for future iterations of Oculus hardware as well as other HMD manufacturers - keep an eye on the Unity blog for news of this.

You can download the project from the Asset Store to see the source assets.

Sample Scenes

The main sample scenes are:

  • Intro: An Introduction scene to introduce the user to the basic interactions

  • Menu: A Menu scene to choose a minigame to play

  • Flyer: An into-the-screen flying game

  • Maze: A table-top maze game

  • Target Gallery: A corridor target shooting gallery

  • Target Arena: A 360° shooting arena

There are also a number of example scenes, outlining specific concepts detailed in the documentation:

  • InteractiveItem: Showing a basic way to interact with items in VR

  • RenderScale: An illustration of how RenderScale affects the quality of the image

  • Reticle: A basic example of a reticle in VR

  • Rotation: Showing how an object might react to head movement

  • Touchpad: How we can read input from the touchpad on Gear VR.

The following concepts are covered:

  • Top-down boardgame style gameplay

  • Into-the-screen Starfox-style gameplay

  • Shooting gallery gameplay

  • 360 shooting gallery gameplay

  • Using Unity UI in VR

  • Spatial UI

  • Basic framework for interacting with objects in VR, including handling gaze over and gaze out

  • Reticle movement, scaling, shaders, and reacting to normals

  • Movement based on head rotation

  • Using the Gear VR touchpad, including tapping, double-tapping and swiping, or keyboard and mouse if using DK2

  • Conditional compilation for Gear VR and DK2

  • Avoiding nausea when rotating the camera around a central pivot point

  • Camera fading

  • Camera fade transitions

  • Object pooling

While this project is mainly to be used to illustrate various concepts in VR and to give you a head start in beginning VR development, we have also included some useful examples for any VR developer to re-use:

  • Camera fading

  • Camera fade transitions

  • Framework for interacting with objects in VR

  • Hold to confirm interaction

  • Reticle movement and scaling

  • Platform-dependent text

  • UI arrows to indicate facing a specific direction

  • Moving UI elements so they always face the camera

  • Tinting UI objects when interacted with

To run the project, you’ll need a copy of Unity 5.3.0, to meet the hardware and software criteria outlined above, and have a DK2 or Gear VR with compatible phone setup for development as per the manufacturer's guidelines (DK2 / Gear VR), along with Oculus Runtime 0.8 or higher.

Oculus Unity Utilities

Depending on the functionality required in your project, you may want to download the Oculus Unity Utilities, which contain useful scripts and prefabs for VR projects, such as being able to correctly change the IPD (Interpupillary distance - the distance between your eyes), and visualise the tracking bounds.

Our articles focus on native Unity VR integration, so please see the Oculus website for more information on the utilities and their suitability for your project.

You should now be familiar with the hardware and software requirements for VR development in Unity, and how to enable VR in your project. To get started with the basics, take a look at the next article: Getting Started with VR Development.