When you start building a Virtual Reality or 3D game, you generally will start with a world or a room. In either case, you need to build a bounding area, including a floor or ground. If you are building a room or a play area, you’ll also want to build bounding walls. In this article, you will learn how to create a play area or room in Unity.
This article uses Unity 2018; however, older versions will be very similar. If you’ve not worked with Unity, you should read the previous article, “Virtual Reality 101: Building and Android VR App,” which not only talks about getting started, but will also show you how to create basic objects and how to compile to create the VR app.
The basic structure of a room is a floor or ground, four walls or boundaries, and a ceiling. If you want to create a play field, you can simply leave off the ceiling. Each of these pieces are built by using GameObjects in Unity.
To create the floor or ground, you can use either a plane or a cube 3D object. Load Unity and create a new project, as shown in the previous article. I’ve named my new Unity project MyDevComRoom, but you can use any name you like. Remember to make sure you select 3D for the project type.
When Unity initially loads, you’ll have a blank scene. As a general practice, it is good to go ahead and save your initial scene. You can do this by selecting the Save Scene option on the File menu or by right-clicking the scene name in the Hierarchy menu and selecting Save Scene from the pop-up. In either case, the first time you save, you will be asked to enter a file name. This file name will be the name of the scene as well. I saved my initial scene as “Level000”. You can use any name.
Once saved, you should have a blank Unity project similar to Figure 1.
Figure 1: New Unity project with the blank scene saved
With your scene named and your project ready to go, the first thing to do is to add the floor or ground. This can be done by adding a plane GameObject to the scene. You were shown how to add objects in the previous article, but to reiterate, you either navigate through the menus by selecting GameObject –> 3D Object –> Plane, or you can click the Create drop-down menu on the Hierarchy tab and, from the pop-up menu, select 3D Object –> Plane. Either way, a plane object will be added to your scene, as shown in Figure 2.
Figure 2: Adding a plane object to your Unity Project
Rename the plane either in the Inspector tab (circled in red in Figure 2) or by right-clicking the plane on the Hierarchy tab and selecting rename. Name your plane “ground“. The default size of the plane is 1 by 1 by 1, as shown in the Scale values in the Inspector tab. You’ll want to increase this to a larger size. For my example, I set the scale to 2 by 2 by 2. You now have the floor of your room!
Coloring the Ground
The default color of the plane is white, which is not very exciting. The color is a result of the default material being used on the ground plane object. You can change the color by adding a new material onto the plane.
Creating a New Material
To create a new material, you can select either the Create drop-down menu on the Projects tab, or you can use the Unity navigation at the top and go to Assets –> Create –> Material. This will create a new Material object, as shown in Figure 3.
Figure 3: A newly created Material in Unity
When the material is created, the name is highlighted in the Project tab’s Assets box so that you can enter a new name. If you don’t enter a new name, it will be defaulted as “New Material”, which isn’t very descriptive. For this project, I renamed the material to “ground” because this will be used to color the ground of our play area.
With the material icon selected in the Assets window, you’ll see the material’s properties on the Inspector tab, as shown in Figure 3. To change the color, you’ll want to modify the Albedo property. By clicking the color box or the dropper icon next to Albedo in the Inspector, a color dialog should be displayed, as shown in Figure 4.
Figure 4: Selecting a material property in Unity
You can use either the color wheel or set values to create your color. I set values of 145, 100, 25, and 255 into the RGBA boxes, which resulted in a version of a brown. Once you hit enter on the dialog, you should notice that the Inspector updates to show your color not only in the Albedo listing, but also on the sphere at the bottom of the Material dialog.
With your new material created in the Assets area, the next step is to apply it to your ground plane. The easiest way to do this is to simply drag the material icon from the Assets tab and drop it onto the GameObject you want colored. In this case, you can drop it onto the plane in the Scene tab. Alternatively, you can drag and drop it onto the ground listing on the Hierarchy tab. Either way, once you’ve done this, your plane should show with the new material applied, as can be seen in Figure 5.
Figure 5: Your colored plane
To color other GameObjects you create, you can follow this same process of creating a material and dragging and dropping it onto them.
Creating the Walls
With your ground now created and colored, the next step is to create walls. This can be done using 3D Cube GameObjects. To create the first wall, add a cube by selecting GameObject –> 3D Objects –> Cube from the navigation menu. This will place a cube in the middle of your ground plane, as shown in Figure 6. If you don’t see the cube, make sure that the position set in the Inspector window is set to X=0, Y=0, and Z=0.
Figure 6: Adding the Cube to the scene
As you can see, the cube doesn’t look like much of a wall. You can change this by stretching and moving the cube.
Stretch the cube either by using the arrows, or by changing the values for the Scale in the Inspector. To make the wall the same width as the plane you created, set the X Scale value to 10 times the number you used for the ground. The value of 2 was used to scale the plane above, so set the X Scale value to 20 (2*10) on the new cube. You should now see a long skinny box going across the middle of your plane, as shown in Figure 7.
Figure 7: The Cube scaled to the width of the plane
You can change the thickness of this first wall by changing the Z Scale value. You can change the height of the wall by adjusting the Y Scale value. I’ve set a value of 4. You should note, however, that the wall goes both above and below the plane. This is not necessary. You can move the wall up by adjusting the Y Position value in the Inspector tab. Simply change the Y value to half of the Y Scale value to move it above the plane. If you set your Scale value to 4, set the Y position value to 2.
Because this is a wall, you will want to move it to the edge of the ground rather than have it going down the middle. If you don’t recall how to move an object, you were shown how in the previous article. Because this is the first wall, you can change the value of the X position to be half of the wall width to move it to the edge. In this case, changing the X Position to 10 will move it over. Alternatively, you can drag the wall to the edge.
As a matter of cleaning up the project, you can rename the cube you created to “wall”. Just like before, this can be done in the Inspector or by right-clicking the Cube in the Hierarchy tab.
To create the second wall, right-click the first wall you created, and select Duplicate. This will create a second wall. You now can simply move the second wall across the scene to the other side of the ground. You can do this by dragging the wall, or by changing the Position values. If you set the position of the first wall’s Z Position to 10, setting the second wall’s Z Position to -10 should result in a scene similar to Figure 8.
Figure 8: Two walls added to the playing field
To create the other two walls, you can duplicate one of the existing walls and then move or change the position again. To position the duplicated wall using the Inspector values, you can simply change the Scale and Position values by swapping the X and Z values. The result is that your third wall will be at position -10, 2, 0 with a Scale of 1, 4, 20 and the fourth wall will be at 10, 2, 0 with the same scale. The final result should look like Figure 9.
Figure 9: A playing field with four walls
The steps for building a VR project were covered in the previous article. If you build this project now for VR, you’ll be able to look around and see the four walls you built. If you have trouble seeing the four walls, then click the Main Camera in the Hierarchy and verify that the X position is 0 (or at least between -9 and 9), the Y position is 1 (or greater than 0), and that the Z position is also between 9 and -9 so that it is in the room. Note that the walls are at position 10 and -10, so the values I just listed are simply within this range to put the camera inside of the room. To be more specific, the walls are 1 unit thick. As such, the area between 9.5 to 10.5 and the area between -9.5 to -10.5 will be inside the walls.
If you want to make the scene more interesting, add a few GameObjects onto the ground. Figure 10 shows the room with a cube added that has been rotated and colored with a material. The camera is at one end of the room. This is the view you see when you press the Play button in Unity. You’ll see a similar view (but in 3D) if you compile this for Android VR and load it onto a VR device. In fact, you’ll be able to look around the room from the camera’s position if you are using a VR device.
Figure 10: The new play area in Unity
Collating the Play Area
Although it is not necessary, it is recommended that you organize the pieces of your playfield by grouping them. Create an empty GameObject by either clicking Create in the Hierarchy menu and selecting “Create Empty”, or by selecting GameObject –> Create Empty from the Unity navigation menus. This will add an empty object to your hierarchy menu within your level that you then can rename to “Playarea” (or any other name). Once created, from within the Hierarchy tab, you can drag your four walls and ground objects into the new object for a result similar to Figure 11. Once you’ve done this, you will be able to manipulate all of the walls and ground at the same time by making changes to the PlayArea parent object.
Figure 11: Organizing the Play area into an empty GameObject
This article built upon the previous one by showing you how to create a basic room or play area for a Unity game. The end result of this article was a very basic room, which can be used as the foundation for building levels that keep your players from moving outside of the play area.
You also learned how to create a basic material and apply it to a game object. Even though you were only shown how to do this with one object, you can create other materials and colors the same way and apply them to any game objects you create.
You can adjust the thickness of the walls as well as the height to change how your room or play area looks. You also can add materials to the walls to give them color. If you want a play area with a ceiling, you can add another cube to the top of the play area that spans the width and breadth of the play area. If you do add the ceiling, you’ll likely need to adjust the lighting as well, which is a topic for a different article!