Virtual Reality Application in Unity3D

unity

Have you ever wondered how to make virtual reality application? If you heard about it and do not have any prior experience of developing the virtual reality application, then you are at the right place. In this article, we are going to learn how to make virtual reality application in Unity3D. Before going deep into the development part, first you need to know what are things you need to develop this application.

Tools you need

  • Unity3D
  • Google VR Plugin
  • Some other objects (which will be discussed later)

Unity3D

In this article, we are going to use Unity3D software. It is a software that lets you develop not only games but also the virtual reality applications and many more. In order to use it, you need to first install the Unity3D software. It is free of cost and you can download it from the following link:

https://unity3d.com/get-unity/download

Google VR Plugin (Unity Package)

Once you download the Unity3D software, you now need the Google VR Plugin for Unity3D. All you need to do is to go to the following link and download the Google VR plugin:

https://github.com/googlevr/gvr-unity-sdk/releases

Download the Unity Package of the Google VR plugin.

Now that you have downloaded all the essential tools to need, it is time to dive into the development part of this article.

Create the Unity Project

The very first step you need is to create the Unity3D project. Open the Unity application and fill in the following fields:

  • Project Name
  • Location
  • Type

Give the name to your project; I am going to name it “Virtual Reality”. Location is the field in which you can specify the folder where you want to save all the files of the project. I am going to save all of the file on “Desktop.” The Type field is important. In this field you need to tell the Unity software whether you want to create the 3D project or 2D project. For virtual reality application, it should always be 3D application. Select the 3D radio button.

Change the Unity Mode

After creating the project, change the Unity Mode to either iOS or Android. In this article, I am going to use Android. In order to change the mode, you need to go to the Build Settings and click on the Android icon as shown below:

After selecting the Android option, click on the “Switch Platform” button and wait for Unity to switch from the PC to Android build system.

Import Google VR Plugin

After creating the project, import the Google VR unity package into the assets folder of your Unity project. There are three methods to import the Unity package, which are as follows:

  • Import directly by drag and dropping into the Assets folder of your project
  • Just copy the downloaded Google VR unity package and paste it in the assets folder
  • Right click on the Assets folder inside the Unity3D folder and click on the “Import New Package” and select the Google VR unity package. It will automatically import all of the files inside the Unity Package.

Making the Application

Now that we have imported Google VR unity package, the next step is to layout the scene. For this application, we are going to make the simple VR application in which the user will tilt his or her head left or right and the sphere will move accordingly. By doing this simple example, you will be able to learn how to use the Google VR plugin in your application and also the application of the virtual reality in general.

After importing the Google VR Unity package you would see the following files in your assets folders.

Change the Player Settings

After importing everything, the next step is to go to the “Player’s settings” in the “Build Settings” option and enable the virtual reality option in Unity3D as shown below:

As you can see the “Virtual Reality Supported” option is now enable. Furthermore, “Cardboard” and “Daydream” are added into the project by clicking the + button down below. This step is necessary to run the application in the mobile phone.

In-depth Analysis of GVR Library

In this section, we are going to look at the two core prefabs of Google VR unity package. These are the prefabs about which you must know in order to develop the virtual reality application.

GvrControllerMain

This prefab is essential to be added into your project. There is no specific place where you need to put this prefab at. All you need to do is to drop it in your scene. It is responsible for managing controller state. Includes the GvrControllerInput component, which is the main entry point to the controller API.

GvrEditorEmulator

If you do not want to run the application on your device every time and wants to check into your editor, then use this prefab. It lets you simulate user's head movement with your mouse or trackpad.

Making the simple VR Application

First go to the “GameObject” tab and select the Plane in the 3D objects as shown below. Customize the scene in however way you want. The scene for this application will look as shown below:

Now after creating the ground floor and walls with plane, you need to add the “rigidbody” to the sphere so that it can be affected by the gravity. Once you add the rigidbody to the sphere, you should now move on to the Google VR files.

Adding the GvrEditorEmulator

Find the prefab GvrEditorEmulator inside the Google VR folder. Just drag and drop that prefab into the scene so that you would be able to use virtual reality application.

Make sure to make that prefab the child of the Camera as shown below:

As you can see the hierarchy of the objects that the player is the parent of both the Main Camera and Main Camera is the parent of the GvrEditorEmulator. The main reason to do this in hierarchy is that the user is watching the scene through his or her eyes. It means the camera should be embedded into the position of the eyes. Hence, to make things tidy and easy to understand, the empty object “Player” is added, and the Main Camera has been made a child of the player.

Once you add that prefab and create the scene as stated above, if you run the application, you can use the features of GvrEditorEmulator. If you hold down “alt” and moves the mouse left or right, you would see that the Main Camera will also rotate. It means that when you run this application on the real device with virtual reality feature enables and see this application in Google’s Cardboard, you would see that as you tilt your head towards right the angle of the camera also changes. But in this application, we would work on the Editor, therefore, we would stick to that “alt” + mouse movement.

Adding C# code

In this section, we are going to add the C# code to the floor upon with sphere resides. The application is that as we tilt our head to left or right, the floor should also tilt according and thus it would affect the sphere.

Click on the “Add Component” button in the inspector panel of the plane and add rotation script. Make sure that the language you select is C#. Following is the code, that we need to add to the plane which we added before.

    using  System.Collections; 
    using  System.Collections.Generic;
    using  UnityEngine;
    public  class  Rotation : MonoBehaviour {
    public  Transform camera;
    
    // Use this for initialization
    void  Start () {
    }
    
    // Update is called once per frame
    void  Update () {
    Vector3 rotationVector = transform.rotation.eulerAngles;
    rotationVector.z = camera.rotation.eulerAngles.z;
    this.transform.rotation = Quaternion.Euler(rotationVector);
    }
}

public Transform camera: As you can see in the code that the camera object is a public object. It means we can drag and drop the camera object into this field from the editor. What this code is essentially doing is that it takes the transform of the camera. In that transform, there are three different objects present: Translation, Rotation and Scale. We are interested in the Rotation object as it is stated earlier that the floor (plane) reacts to the camera rotation accordingly.

Vector3 rotationVector = transform.rotation.eulerAngles:

In this temporary variable, we are saving the rotation values of the plane, as this script is attached to the plane object.

rotationVector.z = camera.rotation.eulerAngles.z;

We assign the “z” component of the camera’s rotation to this plane’s rotation. That way the plane would tilt with the change in the angles of the camera.

this.transform.rotation = Quaternion.Euler(rotationVector);

Lastly, we assign the updated rotation transform of the plane’s transform.

Demo

As the head is tilted towards left, the spherical ball slides towards left and hit the wall.

Summary

In this article, we have learned how to integrate Google’s Virtual Reality SDK in Unity3D. We have also discussed some core features of that VR kit.
After that, we have implemented the application that responds to the movement of the head from left to right.
Furthermore, the options to enable the virtual reality in Unity3D android are also explored. That’s it for this tutorial based article. Hope you like it!

AUTHOR

Thank you for writing interesting articles. I am looking forward to sharing your knowledge.

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?