Now that we have 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 the Terrain tool 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. Using those tools, 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:
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. 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 Terrain, which we will do in this first section of this chapter.
In this section, we will cover the following concepts related to terrains:
Let's start 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 the problem that we will use full detailed models for objects at all possible distances, thus wasting resources on details we won't see when the object is far away. We will see lots of terrain parts from a great distance, such as mountains and rivers, so this is a serious issue.
Unity Terrain tools use 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 figure, you can see a black and white top-down view of Scotland terrain heights, with white being a higher height and black being a lower height:
In the preceding image, you can easily spot the peaks of the mountains while looking for the whitest areas of the image. Everything below sea level is black, while anything in the middle using gradients of gray represents 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 Terrain 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 lower-detail models for faraway areas, making it a performant solution.
In the following figure, 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 the further-away parts:
Take into account that this technology also 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 how a giant plane appears on your scene and that 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 screenshot, you can see what a brand new Terrain looks like:
Before you start editing your 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. Remember that our game will happen in the Player´s Base, 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:
Important Note
The Heightmap resolution is the size of the Heightmap 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 Heightmap. 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 holes in case you need them. In order to do so, do the following:
Now that we have properly configured our Height Map, let's start editing it.
Remember that the Height Map is just an image with the heights, so in order to edit it, we would 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:
Important Note
That checkered pattern you can see near the terrain 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 is useful to prevent creating too big or too small terrain features. 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.
Now, we have decent starter hills around our future base. We can also draw a river basin around our future base area. To do so, follow these steps:
Now, we have a simple but good starter terrain that gives us a basic idea of how it will look from our base's perspective. 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 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 the Terrain tools. Right now, our mountains look very smooth. In real life, they are sharper, so let's improve that:
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:
We can 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 this time, 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 a visual of how the player will feel that area. In this case, we can use any 3D modeling tools for this, such as 3D studio, Maya, or Blender, but those can be difficult to learn and you probably won't need all their power at this stage of 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 we can install it.
Unity is a powerful engine full of features, but having all those tools added to our project if we are not using all of them can make the engine run slower, so we need to manually specify which Unity tools we are using. To do so, we will use the Package Manager, a tool that we can use to see and select which Unity packages we are going to need. As you may recall, earlier, we talked about the Packages folder. This is basically what this Package Manager is modifying.
In order to install ProBuilder with this tool, we need to do the following:
Important Note
Im using ProBuilder version 4.2.3, the newest version available at the moment of writing this book. While you can use a newer version, consider that the steps to use it may differ. You can look at older versions using the arrow at the left of the title.
Now that we have installed ProBuilder in our project, let's use it!
We will start our base by creating a plane for our floor. We will do this by doing the following:
Now that we have created the floor, let's learn how we can manipulate its vertexes 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 segments to 2 (2 cuts). We have done that because we will use the outer cells to create our walls, thus raising them up. 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:
Important Note
If you want the vertexes to have exact positions, I recommend that you install and explore the ProGrids package. It is a position snapping system that works with regular Unity and ProBuilder.
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:
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 just push the walls downward, without moving the floor, so that the walls will be buried in the terrain but our floor will keep a little distance from it. You can see an example of how it would look in the following figure:
In order to do so, we need to do the following:
If you selected undesired edges, just click them again while holding Ctrl (command on Mac) to deselect them, all while keeping the current selection:
Information Box
If you want to use the Wireframe mode in the previous screenshot, click on the Shaded button in the top-left part of the Scene view and select the Wireframe option from the drop-down menu.
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. Follow these steps:
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:
I know we didn't plan the pit in the original level layout, but remember that the define acronym is a document that will constantly change in the middle of game development, so sometimes, we can be bold and change it in order to improve the game. Just take care to not go too far with never ending changes, which is a difficult-to-master art.
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 river basins. Also, we saw how to create our own 3D meshes using ProBuilder, as well as how to manipulate the vertexes, edges, and faces of a model to create a prototype base model for our game. We didn't discuss some performance optimizations we can apply to our meshes or some advanced 3D modeling concepts as that would require entire chapters and that's outside 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 so that it reaches the final look, which we will finish by the end of Part 2.