In this tutorial, I will show you how to create a virtual reality game using Unity 3D. Virtual reality games are popular, fun, and can take your game development skills to another level, so let’s dive in!
If you haven’t already, let’s download the latest version of Unity. It is free for personal and hobby use. Unity allows you to create upload your game to many different platforms, including iOS, Android, Xbox, and Linux.
Adding the VR plugins
In order to create a VR game for your chosen platform, we will need to download a couple of plugins. For the purpose of this tutorial, I will show you how to upload to the Android platform. To upload to iOS, you will need to download Xcode.
Now let’s download the Google VR SDK plugin for Unity. As of this tutorial, it is on version 1.110. You can find it on GitHub. Unzip and extract all the files into a new folder. This will help you keep your files organized.
In addition, if you don’t already have it, you may need the Java SE Development Kit. Follow the instructions on the Oracle website.
Note: You may also need to download the Android SDK.
Let’s Create a Project
Open up Unity to create a new project. It will ask if you would like to download an asset. Let’s download the Environment Package.
Once your project is open, go to Assets > Import Package > Custom Package > Import Google VR SDK for Unity > Import.
We need to prepare our project for the VR platform. We will need to tell Unity we are building for Android and make the scene VR enabled. First, go to File > Build and choose Android. It would also be a good time to choose Landscape Left. Then click Switch Platform at the bottom and close the window.
Time to enable the VR settings. If you try to play the game before doing the above, you will receive errors. Go to Edit > Project Settings > Player, and then on the right-hand side under XR Settings, click Virtual Reality Supported.
Don’t forget to choose an SDK. For this tutorial, choose Cardboard (as seen above).
Initially, we will be using the GVR Demo scene (shown above). Open up your assets in the Project tab and go to Scenes > GVRDemo. Once the file opens, we will be changing the environment. We will delete the following objects in the Hierarchy:
- Floor Canvas
For now, we will keep everything else intact. Let’s briefly discuss why, as explained by the developers at GoogleSDK.
- GvrControllerVisual provides visual feedback for the daydream controller.
- GvrEditorEmulator provides mouse-controlled head tracking emulation in the Unity editor.
- GVR Controller gives the player keyboard input.
Let’s start our own environment. Save your scene and go to GameObject to add a terrain. In Projects, let’s choose a grass surface and drag it onto the scene. To add some substance, let’s add some hills. In the Hierarchy under Terrain, let’s choose the button with the hill and the arrow pointing upwards. Now choose a brush. It’s good to alternate to make your environment look real. If your hills are too pointy, use the smooth button as shown below.
Tip: Make sure your Terrain does not have Gravity chosen, or it will fly away!
The Unity asset store is a great resource for developers, and you can find it right in your project under Assets. I’ve downloaded two buildings. You can find them under the Free filter in the Asset Store as “Abandoned Buildings”.
Tip: You can always drag an asset straight into the Hierarchy.
Let’s Get Moving
Since not all VR headsets come with a controller, we will need to add an Autowalk code to have the user walk on its own. A movement will happen when you look at an object.
Once you are happy with your environment, hit Play, and you should be able to look around the world by holding the Alt button. To make our VR character move, we will need to add a script and change some settings on the Player in the Hierarchy.
First things first: choose Player in the Hierarchy. Let’s Add a Component > Capsule Collider and a RigidBody. We also need to freeze the X, Y, and Z Rotation of the Rigidbody (as shown above).
Now it’s time to add our script to the Player. I have attached the script to this tutorial under attached files. Save the file in your Unity folder, and then click and drag it into the Project. Now Add a Component > New Script. Type Autowalk and the script should show up. You may need to save your project and refresh the scene.
Once you add the script, it will give you an option to configure it. We will need to add a Speed of 2 and check Walk When Triggered (see below).
Test Your Game
Time to test your game on your phone. Go to File > Build Settings > Android > Build & Run. You will need to type an identifier (people usually use com.nameofyourgame). As discussed before, you will need to have the Android SDK, which is free to use.
Save your build and transfer to your phone using a USB cord. Once the game starts, you may need to hit Settings > Cardboard if it looks off.
Unity has an active economy. There are many other products that help you build out your project. The nature of the platform also makes it a great option from which you can better your skills. Whatever the case, you can see what we have available in the Envato Market.
Tip: You need USB debugging to build for Android. If you have an Android phone, navigate to Settings and then About Phone, and tap Build Number seven times. You will get a new button for developers.