Over the last several months I have had a chance to dive into VR development with Unity and Google SDK, so I thought I would use this opportunity to write several posts about my experience.

In this post, I will describe how to create a simple scene with Unity, add Google VR SDK, attach the input module called “Reticle” and implement a simple on click listener.

I’m planning to add more posts in the future including some of the projects I have developed.

The Result.

By the end of the article, we will have a VR scene with a 3D cube in the middle. We will be able to interact with it using the button on the Google cardboard. Our cube will move up every time we press the button. Here is a demo of the scene.

Required Software.

All of the necessary software and SDKs are free. In fact, it is possible to complete a game and release it on Google Play or Apple Store without spending a single dollar. I’ll be focusing on the education side of this, though.

Unity.

Unity is a cross-platform development IDE focused specifically on game development. It is powerful and free to use application, which can be intimidating at first glance. You can find the download link here. Personal version is more than enough to complete this tutorial.

Google VR SDK.

Google VR SDK is a set of scripts, tools, and models developed to make working with virtual reality scenes simpler. You can find the download link here.

Step 1. The Unity Scene.

Alright, all of the software is downloaded and installed, it’s time to get started. Let’s launch Unity, select the “NEW” button, give a descriptive name to our project, select any convenient location, make sure that the project is set to “3D” and select the “Create project” button.

Alright, we have an empty 3D scene with a camera, a skybox and a directional light. Without going too deep into terminology, a skybox is what is visible beyond the scene, like a horizon line, and a directional light is used to represent the sun. Here is what we will see if we press the play button.

Not particularly impressive, but everybody has to start somewhere.

Let’s add a “Plane,” which we will be using as our floor, and a 3D Cube, which we will program to interact with later in the post. Press the “Play” button again to stop the scene, then select “Create” -> “3D Object” -> “Plane”

And then follow the same steps to “Create” -> “3D Object” -> “Cube”.

Pressing the “Play” button again will reveal a slightly more interesting scene now.

We can re-arrange all of the objects, including the camera, by using the “Move” tool at the top left-hand corner. Select any object, then select the “Move” tool and then re-position the object by dragging the colored arrows as shown below.

I would prefer to move the plane to below the camera as if we were standing on top of it and the cube a little further away so that it doesn’t appear intimidating, like so.

Great start, now we can convert this scene to VR!

Step 2. Google VR SDK.

Adding the Google VR SDK might be one of the simplest and coolest integrations I have seen with software. The SDK has the extension.unitypackage. All we need to do is double click it while our Unity scene is opened and paused.

Unity will prompt us with the “Import” dialog, without changing the default selection we can click the “Import” button. Alternatively, we can uncheck the “Demos” selection, as we will not be using any of them at the moment. Depending on the power of your computer it may take up to a couple of minutes.

Now we can add the SDK to our scene. Under the “Assets” directory locate “Prefabs,” then select the “GVRViewerMain.prefab” file, drag and drop it anywhere in the scene.

Now if we press the play button we will notice that our screen gets separated into two halves, one for the left eye and the other for the right. We can press and hold the “alt” button on the keyboard and move the mouse cursor to look around as shown in the demo below.

That was easy, wasn’t it? The next thing we need to do is adding an element called “Reticle.” A Reticle is an analog for a mouse cursor used in Virtual Reality. It is a little dot which always stays in the middle of the screen, and it is used to point at elements. We need to grab the GvrReticlePointer prefab under “Assets” -> “GoogleVR” -> “Prefabs” -> “UI” directory and drop it on top of the Main Camera instance.

Pressing the play button again will reveal a new white dot which always stays in the middle of the screen.

Almost there, now we need to add an event system to make sure that we can capture all of the events. Using the “Hierarchy” dialog select “Create” -> “UI” -> “Event System”.

Now we need to add the “Gvr Pointer Input Module” as a new component of the Event System. Select the “Event System,” then navigate to the “Inspector” tab, click “Add Component” button, and locate the “Gvr Pointer Input Module.”

We need to make sure that this module has a higher priority than the “Standalone Input Module,” otherwise it would be very hard to select the elements with our Reticle. We can do this by selecting the gear icon next to the pointer module and then selecting the “Move Up” option.

One last step! To make sure that the reticle gets activated when it points at the cube we need to add the Raycast module to our primary camera. Select the “Main Camera” from the Hierarchy, then select the “Add Component” button and locate the Physics Raycaster.

Step 3. Custom code.

We need to create a new script and associate it with our cube. To do this, we need to select the Cube from the hierarchy, open the “Inspector” panel, tap the “Add Component” button, type “MovingCube” into the search bar and hit the “Enter” key when the “New Script” option appears.

Following these steps will create a new C# script called “MovingCube,” associate it with the Cube we have just created and launch a C# development IDE, either Mono or Visual Studio depending on which operating system you have installed.

The script will be pre-populated with the skeleton code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingCube : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
}

We only need to create a single method which will be triggered every time we point at the cube and click the cardboard button. We can call this method MoveCube. The code is below, every time the method is called it moves the “Y” position of the cube by 1 meter.

public void MoveCube() {
    transform.Translate(Vector3.up * 1);
}

Next, we would need to add a Trigger component to our cube in a very similar way to how we added a script. Select the cube, then the “Add Component” button, then locate the “Event Trigger” option.

Inside of the “Event Trigger” component we need to select the “Add New Event Type” button and then select the “Pointer Click” option.

Next step is a little tricky. We need to press the “Plus” button, select the “Moving Cube” component, drag it and drop it into the “None (Object)” field, like shown in the demo below.

Then we need to add the function. Select the “No Function” drop down, then the “MovingCube” component, then the “MoveCube” method.

And we are done! Pressing the play button will launch our VR scene. Now we can move around, point at the cube and move it up by pressing the Cardboard button. On a computer, we can simulate the button with a mouse click.