Google Cardboard is an excellent way to experience Virtual Reality for the first time as it’s fairly cheap and is compatible with most modern smartphones so you don’t have to buy any additional hardware. Combine it with Unity, the most widely used free game engine, and you’ll have all the necessary tools to create your very own VR experience. In this tutorial I will walk you through a step-by-step process of building your first Virtual Reality room on top of a Google cardboard Unity demo.
The room will have a floor, four walls and a ceiling and you will be able to move around inside of it freely. If you don’t yet have Unity or the necessary SDK’s, please follow this straightforward guide from Google and continue reading my article when you have imported the demo and your working environment in Unity looks like this:
Problem parsing the package error
When you build the .apk file for the first time, load it to your phone and try to open it, you may encounter an error stating that “There is a problem parsing the package“. If this happens you need to open up your AndroidManifest.xml file inside Your Game/Assets/Plugins/Android directory and change the value of android:minSdkVersion to 16:
Now build your game again, load it to your phone and you should be able to install the app. Oh and by the way, your phone’s security settings should be changed to allow unknown sources.
How to build a Virtual Reality room for Cardboard
Alright, let’s get started! We can optionally remove all the things we don’t need in our current game so in the Hierarchy panel, right click on Cube and delete it. Do the same for Floor Canvas, Overlay Canvas and Event System.
- Now click on the GameObject drop down menu, navigate to 3D Object and click on Cube to create a new object in our game.
- Right click on it in the Hierarchy panel and rename it to Character.
- Drag the Head (under CardboardMain) inside the Character object.
- Click Continue to confirm your action.
- Left click on the Character object that now is parent to the Head object to select it.
- In the Inspector panel at the right side Scale it to X: 1; Y:2; Z:1 and Position it to X:0; Y:1; Z:0.
- While the Character is selected, navigate to Component, then Physics and choose Rigidbody.
- In the Inspector panel edit Rigidbody settings by expanding Constraints and freezing the Rotation of X, Y and Z.
- Additionally tick Convex in the Mesh Collider settings panel (for the Character object).
The Rigidbody will prevent us from walking through the walls and by constraining the Rotation of X, Y and Z, we prevent our character from possibly falling over. This was especially the case with the Capsule object set as our Character- you’d simply fall over when you touched a wall. Hilarious, but annoying.
Adding walking functionality
Now that our character is ready, we need to add a way to move around in the world. Luckily JuppOtto has created a script that we can use. You can download it from here.
- Save the file as Autowalk.cs somewhere in your computer.
- From the Assets drop down, navigate to Import New Asset and import the Autowalk.cs file.
- Left click the Character object in the Hierarchy panel.
- In the Inspector panel, click on the Add Component button.
- Navigate to Scripts and choose Autowalk.
- In the newly added Autowalk script component set Speed to 1.
- Tick the Walk when looking down option and set Threshold Angle to 20.
If you play your game now you should be able to move around the empty space simply by looking slightly down. Your character stops when you look back up.
If you’re not satisfied with the angle you can always change it to something else, but I think 20 degrees is perfect because from playing other people’s games, I have found that if the angle is set too high it will be hard to pick up objects that are placed on the floor (you keep moving over them because you’re looking down).
You may have noticed that there is another setting called Walk When Trigger is pressed. It’s another great example of how one can move around and you should definitely try it out! This method was used in the VR Horror game Silent Home and for that particular game, it’s a great fit.
Building walls for our Virtual Reality room
Walking around in an empty space is boring so why don’t we add some walls and a ceiling to our game? We can do this simply by adding 3D objects, namely the Cube, into our game. To make the cubes more wall-like, we simply need to adjust their position and scale properties.
- From the GameObject drop down, navigate to 3D Object and click on Cube.
- In the Hierarchy panel, right click the Cube and rename it to Wall#1.
- Adjust the Position and Scale properties of the Wall#1 in the Inspector panel. I set the Position to X: -3; Y: 1.5; Z:0 and Scale to X: 1; Y: 3; Z: 5.
- Take advantage of the Duplicate option by right clicking Wall#1 to create three more walls.
- In the Transform panel, Rotate the Wall’s Y coordinate by 90 degrees if needed.
- Finally add a ceiling which should be placed on top of everything like a lid on a box.
- Make sure to drag the Point Light object inside of your room once it’s ready.
- You can optionally left click Point Light from the Hierarchy panel and adjust its light properties in the Inspector panel (range, color, that sorta thing).
Note: When you walk around in your room, you might notice that you can see through the walls when in close proximity.
To fix this, expand the Character object, then the Head object and select the Main Camera by left clicking on it. From its Inspector panel look for Camera and adjust its Near Clipping Pane to 0.01 which is the lowest possible value.
Here’s what my virtual room now looks like from outside (yours can look different. Just be creative!):
When you play the game now you should be able to move around in your virtual room, and because we applied Rigidbody to our Character object earlier, it’s working together with the wall’s Box Collider and thus preventing us from traversing through the walls as they weren’t even there.
So far so good. Now it’s time to add some textures!
Adding textures to our Virtual Reality room
Unity comes with its own built in Asset Store which is very similar to Google’s Play Store. In the Asset Store you can look for and download various scripts, textures and other stuff for your virtual reality game or any other game for that matter.
Select the Free Only filter and search for wall texture from the Asset Store. I chose Make It A Brick Wall by Allegorithmic as the suitable texture for my walls.
When you find what you like, simply download the asset, wait for Unity to prepare it for you and when the Import Unity Package popup appears, select all and press the Import button.
Adding the texture to a wall
Select the Wall#1 by left clicking on it in the Hierarchy panel. Expand it’s Mesh Renderer – Materials drop down and click the little circle right next to Default-Material.
A popup should appear in which you should be able to locate your newly imported texture easily. When you double click it, the popup should close and your Wall#1 will now have a texture.
Repeat the process on the rest of the walls (and ceiling if you like). I chose Dungeon Ground Texture by Pixel Indie as my ceiling’s texture.
Adding a texture to the floor
While you can use the same texture on your floor as you did on the walls, it will look a lot better if you add a different texture to it so after you have found the right material from the Asset Store again, left click on the Plane object in the Hierarchy panel to select it.
The Plane object’s default material in the Mesh Renderer should be GroundPlane. Simply click on the little circle next to it as before, browse to the material you want to use as your floor and double click it in the popup to set it as your new floor material.
Adjust tiling if needed
I used the Stone Floor Texture Tile by 3dfancy, but because the default tiling settings didn’t match with my floor quite right, I had to adjust its tiling manually. Simply find your texture folder inside the Assets panel at the bottom of Unity’s UI, open it and click on the texture material (with a blue sphere icon) to edit its tiling settings.
Your room in Virtual Reality is ready!
If you’ve followed this tutorial so far, you should have a single room in your VR game with solid walls and in which you can move around by looking slightly towards to the floor. Here’s what my room looks like:
If you want, you can download this app and try it out yourself.
Where to go from here?
This was just a basic tutorial on how to create a room in Virtual Reality for the Google Cardboard headset and the next step would probably be to create more rooms. Perhaps even a maze?
To be fair with you, I have to come out and admit that I made this tutorial to be a future reference point for myself because I’m a complete noob in Unity and I have only been at it for a week. That being said, I hope my guide has helped you and if you get stuck, feel free to comment here or email me.
If you enjoyed this article, please consider sharing or leaving a like so that I will know it has been useful for people and that you want me to write more similar tutorials in the future.