Virtual Reality : Mastering the game line play
Set up a simple VR environment
To tell Unity that our project is a VR experience, we should import the virtual reality viewer. To do so, go to assets > GoogleVR > Prefabs > GvrEditorEmulator. Select it and drag and drop into the Unity hierarchy panel. Now, if you launch the scene you will be able to use the ALT key and your mouse to simulate the head movement.
Create more objects
Let’s create a 3D cube. In the top menu Game Object > 3D Object > Cube. Make sure to set up the location in the inspector. To do so, just select the cube and go to inspector > transform section. The values are: position (0, 0.65, 2), rotation (0, 0, 0) and scale (1, 1, 1).
We will create a simple Plane object which will serve as a simple terrain. Top menu again Game Object > 3D object > Plane. Please make sure to place it as follows: position (0, 0, 0), rotation (0, 0, 0) and scale (1, 1, 1). You may have guessed that those values are for x, y and z-axis for every 3D object in Unity.
Customize the scene
As you see here, we have two simple objects. We can go further by customizing them, at least by changing the color. Please go to project section > assets and create a new folder (Assets menu > New > Folder) and name it “Materials”. We will create some objects to help us control the visual appearance of game objects. Once the folder is created, go inside and click on the Unity’s top menu Assets > Create > Material. Name it “green” and confirm. In the Shader option, choose Unlit > Color. While selecting the Material created please go and select a green color from the inspector as shown in the screenshot: Then, go and create another material for the cube. Follow the same steps to create a material, name it “brown” and chose a different brown shade of color. Now we should apply them to our objects. Nothing is simpler than a drag and drop. Unity is always hiding the complexity for non-developers. To do so, just select the green material, drag it to the scene and drop it on the plane object. Do the same process for cube with the brown material.
Customize the Camera
For the time being, we have a very simple scene. Let’s style the camera and add some cool components to it. We will add an FPS counter to our VR scene so we can follow the performance later if we clutter the scene with too many components. The FPS counter itself has been developed and included inside the Google VR SDK. Before using it, we should organize our scene. We will encapsulate the viewer in a game object. Don’t worry, it’s not that complicated. Please go to the top menu Game Object > Create Empty. Name it “vrmain” in the inspector panel. Now set the attributes like bellow: position (0, 2, 0), rotation (0, 0, 0), scale (1, 1, 1). Then go to the Main Camera in the hierarchy and drag and drop it inside our vrmain to make it a child of our vrmain empty game object. Make sure that the Main Camera attributes are: position (0, 0, 0), rotation (0, 0, 0), scale (1, 1, 1).
To get https://www.sumocheats.com/vudu-codes-generator-get-free-ultraviolet-codes-and-credits/, please visit : https://www.sumocheats.com/vudu-codes-generator-get-free-ultraviolet-codes-and-credits/
Now you have prepared the project to handle the FPS counter. To use it in our project please go to the project panel and follow this path: Assets > GoogleVR > Prefabs > Utilities > GvrFPSCanevas. Just select it, drag and drop it as a child to our Main Camera. Please update the attributes to match our example, otherwise, the FPS canvas will look far away from our camera. Select the GvrFPSCanevas and set the values as shown here: PosX = 0, PosY = 0, PosZ = 1. Here is the hierarchy so far if you have had some problems following these steps. Well, now we have a simple scene with an FPS counter and we can launch it by clicking on the Play button. Don’t forget to save by going to File > Save Scene. We can use this scene as a starter project for our future more complex examples. If you want also, you can download the starter as indicated in the resources section in the beginning of this book.
In this chapter we have configured the GoogleVR SDK with Unity. Then we have seen how demos work and what we should update to make those projects compatible. Finally, we have started a project from scratch, and made a simple VR starter scene. You can download the project source code from http://projectlifehacks.wajdibr.com/ .
More advanced VR experience
Now that you have experienced a simple VR scene, we will dive more in depth and try to have some interaction inside of our project.
Gaze tracking
Our goal in this chapter is to add some gaze tracking to our camera. To do so, you will be importing the prefab object to our scene. Go to project panel and follow the path: Assets > GoogleVR > Prefabs > UI > GvrReticlePointer. Select it and drag and drop it as a child of our main camera. Make sure that the attributes are like below: position (0, 0, 0), rotation (0, 0, 0) and scale is (1, 1, 1). The reticle object draws a circle in front of any object the user points at. It’s used to fire events in the VR dimension. If you launch your project you have something like this:
Customize the Reticle
Optionally you can go and customize the reticle. Many options are available but for our example, we will just change the reticle color. Make sure that you are not in a play mode (by stopping the execution). Now select the GvrReticlePointer from the hierarchy under vrmain > Main Camera and go to the inspector panel. Open the Shader section and go to Color to select a new one like the screenshot below: Save the modifications and if you launch your project, you will have a reticle with new color pointing where your head is looking towards.
Our first interaction
Until now, we have been playing with some simple attributes, and project components. What if we want to make the cube disappear by just looking at it? To be honest, it’s not that complicated. If you have an idea about programming, you will probably know about events. The main idea is to create an event that will be triggered when the user looks at the Cube. First of all, we need a collider on the cube object. Fortunately, this 3D object comes with a box collider. You can find it by highlighting the cube in the project hierarchy and go to the inspector. A section checked called “box collider” will be visible with some other attributes. Then, you should tell the Main Camera to use some physics to draw a line from its origin to the cube. A little complicated? Not really, it will be done in a couple of seconds. Just select the main camera from the hierarchy, go to the inspector and click on Add Component > GoogleVR and select the Gvr Pointer Physics Raycaster script. Make sure that the Raycast event mask is set to everything like below: