How to use the Google VR Reticle in Unity
One the most important things in VR is the ability to interact with the virtual world. One great way to do so is by using a reticle, and Google has now provided this to us in a very easy to use way.
So let’s get started!
If you don’t already have the SDK you can get it here https://developers.google.com/vr/unity/download
Create the Project
Starting with a new 3D project in Unity, add the Google VR SDK to your scene. Now drag and drop the GvrViewerMain and GvrControllerMain into the scene. These are located in Assets -> GoogleVR -> Prefabs and in Assets -> GoogleVR -> Prefabs -> Controller.
To make things look better, add a plane to the scene that will act as your floor and scale it out so it gives you plenty of room to work with.
Adding the Reticle
If you look back in Assets -> GoogleVR -> Prefabs -> UI you’ll see the GvrReticle, drag and drop this in the MainCamera that’s already in your scene.
Now select the MainCamera, and click Add Component on the right side, and add a Physics Raycaster (Script). Also make sure the Y position is set to 1 so the camera is at the correct height with the floor.
Next we need to add an EventSystem to the scene. Right click in the Hierarchy section on the left and go to UI -> Event System. With the Event System selected, go to Add Component and add the Gaze Input Module (Script). Then select the settings Cog for this script and select Move Up so it has a higher priority over the existing Standalone Input Module.
Stuff to look at
Now let’s add a cube to the scene, making the Y scale at 3 so it’s tall enough to look at in proportion to us and then place it in front and to the side of the camera. Now copy and paste that cube so we have another one, and move this one off to the side.
Your scene should now look something like this.
Hooking up the Events
Now we need a way to trigger whenever the reticle is actually looking at one of our cubes. To do this, select the cube that’s on the right, click Add Component, and find the Event Trigger (Script). But before we can assign an action to this event system we need to create the functions first.
So create a new C# class, and add these two methods, and in the Start method, call the HideCube2 method. Basically what we are doing is, when the script loads for the first time, we will hide the second cube, and then when ever we look at the first cube, the second one will appear until we look away.
Once the class is created, select the second cube and assign it a tag that matches what’s in the script you just created (mine was “Cube2”). Now that the script is good to go, select the first cube again and in the Event Trigger we created earlier go to Add New Event Type and select Pointer Enter. For the object, just select the cube your working on, and for the function, go to your script and select the ShowCube2 function. Now repeat this for the Pointer Exit event but this time select the HideCube2 function.
Now when the reticle is directly on Cube1, the function ShowCube2 will get called, and as soon as the reticle leaves Cube1, HideCube2 will get called.
Time to Test!
If you hit the Play button you should only see Cube 1 showing, if your on Mac, hold down the option key and move the mouse to look around, you probably won’t see the reticle on desktop but if you run this on your phone it will be there. But move the camera towards the cube and you should then see Cube 2 appear!
Now that you have this concept working, you can start using this functionality in your own app with more exciting things than just cubes 🙂