In virtual reality (VR) games, one of the few interactions a player can make is press a button by looking at it. In this tutorial, we will implement the button we made in our previous Unity tutorial.

If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

There are two ways to interact with the button. Both ways involve looking (the looking logic). We need to understand how to identify the button. To do this, we will perform raycasting.

To draw an image, Main Camera throws rays in multiple directions to find pixels to draw. We will use the ray that shoots forward in the direction the camera is facing. If the ray flying from the camera touches an object, we will identify the object.

Rename Main Camera “Player” because the player will look through the lens of the camera. We can treat the camera as the eyes of the player. Create a C# script in Assets. Name the script “Player”. Drag and drop the Player script to “Player” in the Hierarchy.

Double-click on the Player script to open it. Type the following code in the Update method in Player.cs. This code declares the local variable hit of type RaycastHit.

public class Player : MonoBehaviour {
// Use this for initialization
void Start () {}// Update is called once per frame
void Update () {
Raycast hit;}
}

hit is a local variable because we declared it in one function. You can only call a local variable within its function. If you know you are only going to use a variable in one function, declare it as a local variable.

If you declare a variable in a class outside of a function, you can use the variable throughout the functions in the class. These variables are global.

If you hover over “RaycastHit”, you can read that if your raycast hits an object, the object will be stored in hit, along with other details.

Create the following if block, which calls the Raycast method from the Physics class.

The if block’s condition will return true when the raycast hits something. In this case, we will use the hit variable. The condition will return false when the raycast does not hit anything. hit will not store a value, so we will not use it.

If the condition returns true, use the following code to print the name of the object the raycast hit.

Save the script, and open Unity. Press Play. Press Alt/Option, and move the cursor until the crosshair is on top of Button. The console will print the message “Button” constantly because every time the Update method is called, it sees that we are looking at the button, and it prints the message.

In virtual reality (VR) games, one of the few interactions a player can make is press a button by looking at it. In this tutorial, we will design a button in Unity. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

VR has limited interactions with keys. Other devices like computers or Xbox, which have a lot of keys that players can press to interact with the game. Create a cube in the Hierarchy. We will have the cube work like a button. Give Cube the position 0 -1 -5. Change its X Scale value to 2.

Create a material in the Assets folder. Make sure that you do not create the material in the Google SDK folders. Name the material “ButtonMaterial”. Drag ButtonMaterial to Cube in the Scene. Change the color of ButtonMaterial to green with RGB values such as 0 184 100.

We will have the button be somewhat see-through. From the Rendering Mode menu in the Inspector, select “Transparent”.

In the Color window, set the alpha of ButtonMaterial to 147.

We will have the button contain some text. Right-click on “Cube” in the Hierarchy. Select 3D Object > 3D Text. “New Text” will appear in the Hierarchy. The text “Hello World” will appear in the Scene.

Set the X Scale value of New Text to 0.5. In the Text Mesh component, set Anchor to “Middle center” and Alignment to “Center”.

Set the text to sit just in front of the cube by changing its Z position to -0.7. Change Font Size to 15. In the Text property of Text Mesh, change “Hello World” to “Btn” (a shortened form of “Button”).

The text “Btn” will be of poor quality in the Scene. To correct this, scale down New Text in the Transform component. Then increase its font size in the Text Mesh component.

As such, the button’s text will be of better quality. Rename New Text “Label”. Rename Cube “Button”. Want to learn more? Check out our Unity3D course, where you build 30 virtual reality games!

With Google VR SDK, we need to make a small adjustment to the game to view the scene in split mode. Stop playing the scene. In Assets, open the GoogleVR folder. Then open the Prefabs folder. We will use the GvrViewerMain.prefab file. This file has been saved for later reuse, so we do not have to build an entire game object that will change the scene.

Drag and drop GvrViewerMain.prefab to the Hierarchy. In the Inspector, you will see that the GvrViewerMain game object contains a Gvr Viewer (Script) component. This script does the VR logic.

Press Play. The Game window will show the scene from two cameras.

In the Hierarchy, click on “Main Camera”. You will see that this object contains Main Camera Left and Main Camera Right.

Click on “Main Camera Left”. In the Inspector, you can see that this camera’s X position is -0.032. Main Camera Right’s X position is 0.032. The distance between the cameras represents 6.5 centimeters, which refers to the distance between the human eyes.

A VR game contains two cameras to represent two human eyes. The left camera shows the scene from the player’s left eye. The right camera shows the scene from their right eye. With the help of a VR device, dividing the view into two cameras makes feel as though they are in a virtual world.

Create a cube in the Hierarchy. Move the cube closer to the camera. If you cross your eyes, you may be able to see the illusion that occurs because of the split view: the cube appears to be popping from the screen.

Press and hold the Alt/Option key. As you move the cursor, you will be able to look around the scene. If you hold Control, when you move the cursor, the scene will tilt. This could be used to change the weapon a player is holding when the player tilts their head.

The first step to making a virtual reality (VR) game is activating VR Mode. In this tutorial, you will learn how to integrate a VR software development kit (SDK). If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

What is VR Mode?

In the file that opens, select Main Camera from the Hierarchy. Notice that in the Scene window, there are gray lines protruding from the camera. These lines outline the area that the camera renders.

Create a cube in the Hierarchy. Put the cube in the position 0 0 0. Select Main Camera. In the Scene, you will be able to see that the cube is inside the rendering area of Main Camera. The cube is inside the shape made by the gray lines protruding from Main Camera.

If you move the cube outside the area outlined by the gray lines, the camera will not render the cube. The player will not be able to see the cube.

A VR game contains two cameras to represent two human eyes. The left camera shows the scene from the player’s left eye. The right camera shows the scene from their right eye. With the help of a VR device, dividing the view into two cameras makes feel as though they are in a virtual world.

Downloading SDK

Delete Cube and Main Camera (1) from the Hierarchy. There are multiple ways to create a VR environment. We will use the SDK of Google Cardboard. Google Cardboard is a VR platform in which a user places a smartphone into a cardboard viewer. When the user looks into the viewer, they see the game on their phone in 3D.

If you do not have Google Cardboard, you can change the SDK settings to disable VR Mode. You will be able to play the game on your phone without VR Mode.

To download Google Cardboard, go to www.developers.google.com/vr/unity. You will reach the page “Google VR SDK for Unity”. This page provides details about the features of this SDK.

The bottom of the page contains useful links. Click on the “Unity API Reference” link to open a page containing details on how to implement the SDK.

You can look through this page to see details regarding the coding of the SDK, such as the available classes.

To download the SDK, click on the “Google VR SDK for Unity” link at the bottom of www.developers.google.com/vr/unity.

To integrate the SDK into the Unity project, you could clone the entire repository hosted on GitHub. Alternatively, click on the “gvr-unity-sdk” link.

You will be taken to a GitHub webpage. Click on “GoogleVRForUnity.unitypackage”. On the page that loads, press the Download button to download the SDK.

Double-click on the file that appears in your Downloads folder. Once the files decompress, an Import Unity Package window will open.

A Unity Package is like a zip file. It is a compressed file that contains many other files inside it.

You can see that the file contains scripts, materials, images, prefabs, and more. Instead of making our own plug-ins, we can import the ones in this package and adjust them. Press the Import button. Once the assets import, all the necessary files for activating the VR Mode will be in your Assets folder.

Depending on the Unity or SDK version you are using, you may be prompted to perform another import to ensure compatibility. You can press Import Package > Import to import the second package.