Activating Virtual Reality | Unity Tutorial

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?

Create a new Unity project named “VRInteractions”. Save the file to your Project folder. Leave 3D rendering enabled.

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 what 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.

Want to learn more? Check out our Unity3D course, where you build 30 virtual reality games!

If Blocks in C# | Unity Tutorial

Suppose we wanted to run code only when a certain condition is met. For instance, a player could only fly in a game if they had a jet pack. A player could only buy a bicycle if they had enough money. To implement this kind of functionality, we can use if blocks. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

An if block runs code when a specified condition passes. In the Start method from our previous tutorial, use the following format to create an if block.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if () {

}
}

The code in between the if block’s parentheses will contain the condition that needs to be met. If the condition passes, the code in the scope definition (between the curly braces) will execute.

Give the if block the following condition. The if block will execute code because 1 plus 1 equals 2.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if (1 + 1 == 2) {

}
}

Note that a single equals sign (=) assigns a value. A double equals sign (==) is a comparison.

Type the following code in the if block. The string “We’re here!” will print in the console if the if block’s condition is true.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if (1 + 1 == 2) {

}
}

Save the script, and open Unity. Press Play. The message “We’re here!” will print in the console. This means that the if block’s condition passed.

Let’s test a different condition in the if block. Use the following code to make the if block test if the string “cats” equals the string “dogs”.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if ("cats" == "dogs") {
Debug.Log ("We're here!");
}
}

Save the script, and open Unity. Press Play. A warning will appear in the console stating that there is unreachable code at Line 14 of Cube.cs.

This warning appears because the compiler already knows that “cats” will never equal “dogs”. Thus the compiler automatically converts the condition in the parentheses to false. The code in the if block will never be executed.

What if we want the if block to execute code only if a condition is false? Create the following if block, which will run code when “cats” is not equal to “dogs”. The exclamation mark means “NOT”.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if ("cats" == "dogs") {
Debug.Log ("We're here!");
}

if ("cats" != "dogs") {
Debug.Log ("We're there!")
}
}

Save the script, and open Unity. Press Play. The message “We’re there!” will print in the console.

Want to learn more about if blocks? Check out our Unity3D course, where you build 30 virtual reality games!

Restaurant Sites That’ll Get You Drooling

Are you making a site for a restaurant? See some UI inspiration below. If you’re a restaurant owner who wants to make a site, check out Mammoth Interactive’s Web Development course!

What’s your favorite restaurant website? Let us know in the comments below! And don’t forget to check out Mammoth Interactive’s Web Development course.

Villager to Millionaire | Uplifting Stories of the Week

We at Mammoth Interactive love sharing good news. Here are 5 uplifting stories from the past week (through March 13, 2017).

1. Teacher, coach, and mentor Maggie MacDonnell is on the shortlist for the $1 Million US Global Teaching Prize.

Photo from CBC News

Photo from CBC News

2. Chinese villagers make millions selling yarn online.

3. This cafe is totally run by adults with Down syndrome.

Photo from Bob Chwedyk

Photo from Bob Chwedykq

4. This iPhone feature improves accessibility for blind people.

5. These retirees are knitting sweaters for chilly chickens.

Photo from WBZ

Photo from WBZ

Have you heard an uplifting story we should talk about? Let us know in the comments below. And don’t forget to check out our courses here.

Changing Properties LIVE | Unity Tutorial

One of the uses of methods is changing the properties of an object live as your game runs. If you are a beginner and want to learn how to build virtual reality games, check out our Unity 3D course!

Suppose you want to change the size of the cube while the game is running. You can use the Update method to achieve this. Cut the line in the Start method. Paste it into the Update method in place of the Log line. Edit the line so that it appears as so:

// Use this for initialization
void Start () {

}

// Called once per frame.
void Update () {
transform.localScale = Vector3.one * sizeModifier;
}

In the preceding code, we set Cube’s local Scale to be the original vector (1 1 1) multiplied by sizeModifier. Save the script, and open Unity. Press Play. Cube will have the dimensions 0.1 0.1 0.1. Click on the “Size Modifier” label (text) in the Cube (Script) property, and drag your mouse left and right. Cube’s size will change according to the value of Size Modifier.

Stop playing the scene. Set Size Modifier to 2.5. Next we will change the name of the cube. Currently, you can see in the Hierarchy that the name of the cube is “Cube”. In Cube.cs, type the following line into the Start method.

// Use this for initialization
void Start () {
transform.name = newName;
}

Save the script, and open Unity. Press Play. “Cube” in the Hierarchy will be renamed “Cubey42”.

What if we wanted to define a new method other than Start and Update? Suppose we want to use a method to add some characters to Cubey42‘s name. Below the Start method, type the following code to declare the method ImproveName.

// Use this for initialization
void Start () {
transform.name = newName;
}

string ImproveName (string originalString) {

}

string ImproveName means the method returns a string. The method has the string originalString as a parameter.

Save the script, and open Unity. The Console will show the following error message.

The message states that the file Cube.cs in Assets, contains an error at Line 20 in Row 16. The error is that not all code paths return a value in ImproveName. This error occurs because ImproveName needs to return a value. Open Cube.cs. Use the following code to return 10.

string ImproveName (string originalString) {
return 10;
}

Save the script, and open Unity. An error will appear in the Console saying that an integer cannot be converted to a string.

ImproveName returns a string, but 10 is an integer. Use the following code to return originalString, the method’s parameter. + appends “-[” and “]-” around originalString.

string ImproveName (string originalString) {
return "-[" + originalString + "]-";
}

Call the ImproveName method in the Start method. The following code makes newName a parameter of ImproveName. ImproveName will edit newName to contain the string that ImproveName returns.

void Start () {
transform.name = ImproveName(newName);
}

Save the script, and open Unity. Press Play. “Cubey42” in the Hierarchy will become “-[Cubey42]-“. Want to learn more about methods? Check out our Unity 3D course, where you build 30 virtual reality games!