Now that we’ve grasped all the necessary concepts to use Unity, let’s start designing our first level. The idea in this chapter is to learn how to use Terrain Tools to create the landscape of our game and then use ProBuilder to create the 3D mesh of the base with greater detail than using cubes. At the end of the chapter, you will be able to create a prototype of any kind of scene and try out your idea before actually implementing it with final graphics.
Specifically, we will examine the following concepts in this chapter:
Let’s start by talking about our game concept, which will help us draft the first level environment.
Before even adding the first cube to our scene, it is good to have an idea of what we are going to create, as we will need a basic concept of our game to start designing the first level. Throughout this book, we will be creating a shooter game, in which the player will be fighting against waves of enemies trying to destroy the player’s base.
This base will be a complex in a (not so) secret location bordered by mountains:
Figure 3.1: Our finished game
We will be defining the mechanics of our game as we progress through the book, but with this basic high-level concept of the game we can start thinking about how to create a mountainous landscape and a placeholder player’s base to start.
With that in mind, in the next section of this chapter we will learn how to use Unity’s Terrain Tools to create our scene’s landscape.
So far, we have used cubes to generate our level prototype, but we also learned that those shapes sometimes cannot represent all possible objects we could need. Imagine something irregular, such as a full terrain with hills, canyons, and rivers. This would be a nightmare to create using cubes given the irregular shapes you find in the terrain. Another option would be to use 3D modeling software, but the problem with that is that the generated model will be so big and so detailed that it won’t perform well, even on high-end PCs. In this scenario, we need to learn how to use Unity’s Terrain system, which we will do in this first section of the chapter.
In this section, we will cover the following concepts related to terrains:
Let’s start by talking about Height Maps, whose textures help us define the heights of our terrain.
If we create a giant area of the game with hills, canyons, craters, valleys, and rivers using regular 3D modeling tools, we will have a problem in that we will use fully detailed models for objects at all possible distances, thus wasting resources on rendering details that we won’t see when the object is far away. We will see lots of parts of the terrain from a great distance, so this is a serious issue.
Unity Terrain Tools uses a technique called Height Maps to generate the terrain in a performant and dynamic way. Instead of generating large 3D models for the whole terrain, it uses an image called a Height Map, which looks like a top-down black and white photo of the terrain.
In the following image, you can see a black and white top-down view of a region of Scotland, with white being higher and black being lower:
Figure 3.2: Scotland’s Height Map
In the preceding image, you can easily spot the peaks of the mountains by looking for the whitest areas of the image. Everything below sea level becomes black, while anything in the middle uses gradients of gray, representing different heights between the minimum and maximum heights. The idea is that each pixel of the image determines the height of that specific area of the terrain.
Unity Terrain Tools can automatically generate a 3D mesh from that image, saving us the hard drive space of having full 3D models of that terrain. Also, Unity will create the terrain as we move, generating high-detail models for nearby areas and low-detail models for faraway areas, making it a performant solution.
In the following image, you can see the mesh that was generated for the terrain. You can appreciate that the nearer parts of the terrain have more polygons than further away parts:
Figure 3.3: Height Map generated mesh
Take into account that this technology has its cons, such as the time it takes for Unity to generate those 3D models while we play and the inability to create caves, but for now, that’s not a problem for us.
Now that we know what a Height Map is, let’s see how we can use Unity Terrain Tools to create our own Height Maps.
If you click on GameObject | 3D Object | Terrain, you will see a giant plane appear on your scene, and a Terrain object appears on your Hierarchy window. That’s our terrain, and it is plain because its Height Map starts all black, so no height whatsoever is in its initial state.
In the following image, you can see what a brand-new Terrain object looks like:
Figure 3.4: Terrain with no heights painted yet
Before you start editing this Terrain, you must configure different settings such as the size and resolution of the Terrain’s Height Map, and that depends on what you are going to do with it. This is not the same as generating a whole world. Our game will feature the player’s base, which they will defend, so the terrain will be small. In this case, an area that’s 200 x 200 meters in size surrounded by mountains will be enough.
In order to configure our terrain for those requirements, we need to do the following:
Figure 3.5: Terrain Settings button
200
in both settings. This will say that the size of our terrain is going to be 200 x 200 meters.500
just to limit the maximum peak of our mountains:Figure 3.6: Terrain resolution settings
Figure 3.7: Height Map resolution settings
Heightmap Resolution is the size of the Height Map image that will hold the heights of the different parts of the terrain. Using a resolution of 257 x 257 in our 200 x 200-meter terrain means that each square meter in the terrain will be covered by a little bit more than 1 pixel of the Height Map. The higher the resolution per square meter, the greater detail you can draw in that area size. Usually, terrain features are big, so having more than 1 pixel per square meter is generally a waste of resources. Find the smallest resolution you can have that allows you to create the details you need.
Another initial setting you will want to set is the initial terrain height. By default, this is 0, so you can start painting heights from the bottom part, but this way, you can’t make holes in the terrain because it’s already at its lowest point. Setting up a little initial height allows you to paint river paths and pits in case you need them.
In order to do so, do the following:
50
. This will state we want all the terrain to start at 50
meters in height, allowing us to make holes with a maximum depth of 50
meters:Figure 3.8: Set Height Terrain tool location
50
meters we specified. This leaves us with 450 more meters to go up, based on the maximum of 500 meters we specified earlier.Now that we have properly configured our Height Map, let’s start editing it.
Remember that the Height Map is just an image of the heights, so in order to edit it, we need to paint the heights in that image. Luckily, Unity has tools that allow us to edit the terrain directly in the editor and see the results of the modified heights directly. In order to do this, we must follow these steps:
Figure 3.9: Raise or Lower Terrain tool location
Figure 3.10: Smooth edges brush
Figure 3.11: Previsualization of the area to raise the terrain
That checked pattern you can see allows you to see the actual size of the objects you are editing. Each cell represents a square meter. Remember that having a reference to see the actual size of the objects you are editing helps to prevent you from creating terrain features that are too big or too small. Maybe you can put in other kinds of references, such as a big cube with accurate sizes representing a building to get a notion of the size of the mountain or lake you are creating. Remember that the cube has a default size of 1 x 1 x 1 meters, so scaling to 10,10,10 will give you a cube of 10 x 10 x 10 meters.
Figure 3.12: Painted mountains around the edges of the terrain
We now have decent starter hills around our future base. We can also draw a moat around our future base. To do so, follow these steps:
50,10,50
in the middle of the terrain. This will act as a placeholder for the base we are going to create:Figure 3.13: Placeholder cube for the base area
10
.Figure 3.14: Moat around our placeholder base
Now, we have a simple but good starter terrain that gives us a basic idea of how our base and its surroundings will look. Before moving on, we will apply some finer details to make our terrain look a little bit better. In the next section, we will discuss how to simulate terrain erosion with different tools.
In the previous section, we created a rough outline of the terrain. If you want to make it look a little bit more realistic, then you need to start painting lots of tiny details here and there. Usually, this is done later in the level design process, but let’s take a look now since we are exploring Terrain Tools. Right now, our mountains look very smooth. In real life, they are generally sharper, so let’s improve that:
50
so that we can cover a greater area:Figure 3.15: Cloud pattern brush for randomness
Figure 3.16: Erosion generated with the aforementioned brush
This has added some irregularity to our hills. Now, let’s imagine we want to have a flat area on the hills to put a decorative observatory or antenna. Follow these steps to do so:
60
.Figure 3.17: Flattened hill
Figure 3.18: Non-smoothed terrain edges
5
and an opacity of 10
:Figure 3.19: Smooth Height brush selected
Figure 3.20: Smoothed terrain edges
We could keep adding details here and there, but we can settle with this for now. The next step is to create our player’s base, but first, let’s explore ProBuilder in order to generate our geometry.
So far, we have created simple scenes using cubes and primitive shapes, and that’s enough for most of the prototypes you will create, but sometimes, you will have tricky areas of the game that would be difficult to model with regular cubes, or maybe you want to have some deeper details in certain parts of your game to get an idea of how the player will experience that area.
In this case, we can use any 3D modeling tool for this, such as 3D Studio Max, Maya, or Blender, but they can be difficult to learn and you probably won’t need all their power at this stage in your development. Luckily, Unity has a simple 3D model creator called ProBuilder, so let’s explore it.
In this section, we will cover the following concepts related to ProBuilder:
ProBuilder is not included by default in our Unity project, so let’s start by learning how to install it.
Unity is a powerful engine full of features, but adding all those tools to our project if we are not using all of them can make the engine run more slowly, so we need to manually specify which Unity tools we are using. To do so, we will use Package Manager, a tool that we can use to select which Unity Packages we are going to need. As you may recall, earlier, we talked about the Packages
folder. This is basically what Package Manager is modifying.
In order to install ProBuilder in our project with this tool, we need to do the following:
Figure 3.21: Package Manager option
Figure 3.22: Showing all packages
Figure 3.23: ProBuilder in the packages list
I’m using ProBuilder version 5.0.6, the newest version available at the time of writing this book. While you can use a newer version, the process of using it may differ. You can look at older versions using the arrow to the left of the title.
Figure 3.24: Install button
2
and Line Size to 1
. This will help you to better visualize the 3D model we are going to create while editing its different parts: Figure 3.25: Configuring ProBuilder
The Vertex Size and Line Size values are big (2
and 1
meters respectively) due to the fact we are not going to edit little details of a model, but big features like walls. Consider you might want to modify it later depending on what you are editing.
Although this is all we need to know about Package Manager to install ProBuilder, if you want to know more about it, you can review its documentation here: https://docs.unity3d.com/Manual/upm-ui.html. Now that we have installed ProBuilder in our project, let’s use it!
We will start the player’s base by creating a plane for our floor. We will do this by doing the following:
Figure 3.26: ProBuilder Window option
Figure 3.27: New Shape option
2
. We will need those subdivisions later.50
.Figure 3.28: New shape created
We needed to move the plane upwards because it was created at exactly the same height as the terrain. That caused an effect called Z-Fighting, where the pixels that are positioned in the same position are fighting to determine which one will be rendered and which won’t.
Now that we have created the floor, let’s learn how we can manipulate its vertices to change its shape.
If you select the plane, you will see that it is subdivided into a 3 x 3 grid because we set up the width and height cuts to 2
. We did that because we will use the outer cells to create our walls, thus raising them. The idea is to modify the size of those cells to outline the wall length and width before creating the walls. In order to do so, we will do the following:
Figure 3.29: Select vertices tool
Figure 3.30: Enabling Select Hidden
Figure 3.31: Vertex selection
Figure 3.32: Move Tool
Figure 3.33: Moved vertices
Figure 3.34: Moved vertices to reduce edges cell width
Now that we have created the outline for our walls, let’s add new faces to our mesh to create them. In order to use the subdivisions, or faces, we have created to make our walls; we must pick and extrude them. Follow these steps to do so:
Figure 3.35: Select Face tool
Figure 3.36: Edge faces being selected
Figure 3.37: Extrude Faces option
5
in the window that appears after we click the plus button.Figure 3.38: Extrude distance option
Figure 3.39: Extruded grid edges
Now, if you pay attention to how the base floor and walls touch the terrain, there’s a little gap. We can try to move the base downward, but the floor will probably disappear because it will be buried under the terrain. A little trick we can do here is to push the walls downward, without moving the floor, so that the walls will be buried in the terrain but our floor will stay a little distance from it. You can see an example of how it would look in the following image:
Figure 3.40: Slice of the expected result
In order to do this, we need to do the following:
Figure 3.41: Select edges tool
Figure 3.42: Selecting floor edges
If you want to use Wireframe mode in the sphere icon, go to the left of the 2D button in the top-right corner of the Scene View and select the Wireframe option from the drop-down menu, as shown in the following image. You can get back to normal by selecting Shaded.
Figure 3.43: Enabling Wireframe mode
Figure 3.44: Object Move tool
Figure 3.45: Overlapping faces
Now that we have a base mesh, we can start adding details to it using several other ProBuilder tools.
Let’s start adding details to the base by applying a little bevel to the walls, a little cut in the corners so they are not so sharp. To do so, follow these steps:
Figure 3.46: Top wall edges being selected
0.5
:Figure 3.47: Bevel distance to generate
Figure 3.48: Result of the bevel process
Figure 3.49: Bevel being applied to floor-wall edges
Another detail to add could be a pit in the middle of the ground as a hazard we need to avoid falling into and to make the enemies avoid it using AI. In order to do that, follow these steps:
Figure 3.50: Subdividing the floor
Figure 3.51: Scale tool
Figure 3.52: Inner cells being scaled down
Figure 3.53: Close Tab option
Figure 3.54: Terrain being lowered for the pit to be visible
In this chapter, we learned how to create large terrain meshes using Height Maps and Unity Terrain Tools such as Paint Height and Set Height to create hills and rivers. Also, we saw how to create our own 3D meshes using ProBuilder, as well as how to manipulate the vertices, edges, and faces of a model to create a prototype base model for our game. We didn’t discuss any performance optimizations we can apply to our meshes or advanced 3D modeling concepts as that would require entire chapters and is beyond the scope of this book. Right now, our main focus is prototyping, so we are fine with our level’s current status.
In the next chapter, we will learn how to download and replace these prototyping models with final art by integrating assets (files) we have created with external tools. This is the first step to improving the graphics quality of our game, which we will finish by the end of Part 3, Improving Graphics.
Read this book alongside other users, Unity game development experts, and the author himself.
Ask questions, provide solutions to other readers, chat with the author via Ask Me Anything sessions, and much more.
Scan the QR code or visit the link to join the community.