Google Cardboard Interactivity Tutorial
By Ruiqi Mao
1. Let's get rid of our Hello World text. It's ugly, and we can do much better if we want to interact with objects in our scene.
2. Add some cubes and spheres around the camera.
3. Verify in game mode that you can look around and see the objects.
4. We're going to make it so that we can interact with these objects! To do so, we need to be able to tell the camera how to look at objects. To do so, select "Main Camera" and in the Inspector pane on the right, click "Add Component", then search for "GvrPointerPhysicsRaycaster", and select it.
5. Now that we have a raycaster attached to our camera, we have to be able to tell the camera what objects we want to trigger our raycaster. Go through all of your objects and add an "Event Trigger" component.
6. Now test it out! You'll notice that the circle gets bigger whenever you look at one of your objects!
7. Let's have the objects react when we look at them. Right click the Assets pane at the bottom and create a "Scripts" folder next to the existing GoogleVR folder. Enter that folder, right click, and create a new C# script. Call it "ObjectBehavior".
8. Open the script. It should launch an editor in Visual Studio. What we are going to do is write a script that will make objects grow when we look at them. To get started, let's state what components our script requires. We want the object this script is going to be applied to to have an Event Trigger, so first, we'll import Unity's event system, then state that the behavior requires an EventTrigger.
9. We need to add methods that will be triggered whenever an object enters or exits our view. Add two methods to the class and register them with the Event Trigger.
10. Now, we could simply add code to OnPointerEnter and OnPointerExit to immediately change the scale of the objects, but that would look choppy, so we're going to animate the scaling instead. We're going to have two scales: the initial state and the scaled state, which the object will animate back and forth between.
11. We're done with the code! Let's go back to the Unity editor. Drag the script onto each of your objects. You should see an "Object Behavior" component show up on all of them.
12. You should now have a working VR application with interaction! Test it out in Play mode and on your device!