Let’s continue working on our RTS and see how to use Unity’s terrain tools to make a nice landscape!
This article is also available on Medium.
Disclaimer: this idea of tutorial came from NeXuS, big thanks to him! 🙂
When you design a game, you always have to work on various things: programming, assets, level design, UI stuff…
So far, most of this series has been about C# programming and UI. There is, however, another nice game feature we can discuss: how to prepare a terrain to put our buildings and characters on!
Creating a simple terrain
Unity terrains are a specific type of 3D object that can be modelled, painted and sculpted with unique tools. Contrary to basic primitives like cubes or planes, terrains are more “evolved” meshes that you can tweak and configure in many ways with intuitive and user-friendly brushes.
To create a new terrain, go to the Game Object > 3D Object > Terrain menu:
The first thing you’ll notice when you create the new game object is that terrains are big. Like – really big. Compared to a cube of size 2, or a plane of size 10, terrains are huge: by default, they are 1000×1000 units!
Note: by the way, terrains are also anchored at a corner rather than in the middle of the mesh like other primitives.
This can be changed in the settings of the terrain: if you select your terrain and click on the gear at the far end of the terrain toolbar, you’ll find the settings panel that has some width/length options at the bottom:
In the rest of the tutorial, I’ll work on a smaller terrain so that we can look at the entire thing more easily – I’ll set up the terrain to a 100×100 size.
Also, notice the resolution settings in this block: these define how “detailed” the terrain is – it determines how many cells there are in the terrain, which in turn determines how many objects you can place on the terrain.
The height of the terrain is the max allowed amplitude for terrain peaks: it’s the difference between the highest and the lowest point on the terrain.
Adding hills and holes
To begin with, let’s update the heightmap of our terrain. The idea here is to change the height of the terrain at various points to create relief.
The great thing with terrains is that almost everything can be done using brushes: this makes for a fun and user-friendly workflow! 🙂
First, navigate to the “paint” tab and choose the “Raise or Lower Terrain” mode:
As you can see, you have a palette of built-in brushes to pick from. Each uses a unique stamp and can be set to a given size and opacity. The more opaque the brush, the faster the effect on the terrain.
Have fun with the brushes to see what they do! Here is a little example of a basic heightmap you can make with a few brush strokes – playing around with more or less “textured” stamps gives you more or less peaks:
If you want to lower the terrain, simply hold the shift key while you paint. Remember however that you can’t lower the heightmap beneath level 0 (the initial height). So if you want to get a flat terrain where you can make holes, you should first set the height to a positive altitude and then lower the altitude by hand:
If you don’t want to draw your heightmap by hand, or if you’d rather generate it procedurally, you can check out a text/video tutorial I made a few weeks ago on how to use mathematical noises to create procedural heightmaps and applying them to Unity terrains via C# 🙂
Now – this terrain is starting to look a bit better, but it’s still covered in an ugly checkmark texture! This is the default terrain texture, and in truth it’s pretty useful as a default display to clearly see what your heightmap looks like.
But since we’ve taken care of the altitude mapping, let’s get rid of it!
Terrains work with a system of paint layers: basically, you define various texture layers that you can then stack on top of each other and blend together to get your final result on every cell of your terrain.
You can of course use your own textures but, as a first step, the easiest way to get terrain texture layers is to get official Unity samples from the free Standard assets (in the “StandardAssets/Environment” category):
Once you’ve imported these, you’ll get a new “Samples” directory in your project as well as a “StandardAssets” folder with various tree models to populate the terrain and water utilities.
To actually use the layers, you need to select your terrain object, switch to the “paint” tab and choose the “Paint Texture” mode. Then, click on the button labeled “Edit Terrain Layers” and pick the “Add layer…” option:
At that point, you’ll be able to pick terrain layers from the samples. Keep on adding more to have more textures to mix and blend! 🙂
Note that the first terrain layer you add will automatically be assigned to the entire terrain – so, in my case, the terrain is now covered in the “dirt” texture.
Now, you can go ahead and click on a layer to set it as active. Then, use the brush to paint this texture on the terrain:
Adding water and details
To wrap this up, we should add some elements on this terrain to make it more lively! In particular, we can add:
- one or more water patches (again, imported from Unity’s Standard assets): these are little game objects that we can place on the terrain and that have nice materials with animated shaders
- doodads like trees or rocks: Unity’s samples come with some trees from the SpeedTree package that we can easily paint on the terrain
Creating a water patch
To add water, you can either go to the “StandardAssets/Water” folder and drag one of the prefabs to your scene, or you can make a water patch yourself. If you take a look at the assets you imported, you’ll see that there are several types of water patches you can use. Feel free to browse and look for the differences 😉
Here, I’ll use the “Pro Daytime” version but I’ll create the game object from scratch.
A water patch is a game object with a specific material and a “Water” C# script. For the geometry, depending on your terrain, you can use a simple plane or more custom shape. I’ll just go for a plane for now:
And this gives me the following result in the scene view! (Of course, we see the water patch “overshoot” the terrain but that would be hidden in a real game 🙂 )
The water material and script combo from the Unity assets gives a really nice reflective effect and a little animation, and you can get great screenshots or movies with your mountains mirroring in a lake…
Painting trees on the terrain
Finally, let’s see how we can populate our terrain with some trees!
Once again, the process is divided into two phases: first, you have to add tree models to your terrain and then you can pick up a brush and paint them on the surface.
First of all, be sure to switch to the “trees” tab, the middle one in the row. Then, click on the “Edit Trees” button:
You can now pick prefabs from your project – either custom tree models that you’ve made yourself or some from the Unity Standard assets package 🙂
In my case, I just loaded up two prefabs from the Unity assets, the “Broadleaf” and the “Conifer” models.
I can now paint trees on my terrain as I would with height or textures! The neat thing is that Unity allows you to tweak parameters about your brush to automatically set the density and get random heights and rotations on your tree instances: this gives a much more organic and life-like result without a sweat!
Something very interesting when you work on adding details to your terrain is the level of detail (LOD) feature that Unity provides us with. This allows us to show more or less-detailed meshes depending on the distance between the objects and the camera; this way, you avoid having too much unnecessary subtle details on your trees if you’re looking at them from far away!
This significantly reduces the required compute power because it directly optimises the display of your objects.
This is configured via an “LOD Group” component that looks like this (that’s an example from the Unity Standard assets that uses this system for its trees):
Note: even if you don’t configure an LOD Group for your game object specifically, there is still a project-wide LOD setting in the “Quality” tab that you can play around with…
We could still improve this landscape a lot! For example, you can go to the “details” tab (the last-but-one in toolbar) and paint additional meshes like rocks or plants. You can also add grass or define wind zones.
I do find, however, that it’s sometimes easier to place trees or details by hand on the terrain. Especially for details like rocks, I think it gives a better control on the placement. But, of course, it’s longer to do, so it depends on how much environment you have to create…
For more info, check out the Unity docs 😉
Today, we’ve seen how to create a basic terrain in Unity. This is how I made the very basic environment you see in my RTS tutorial demos – I’ve imported some custom textures, in particular for the sand, and the buildings are obviously not part of the terrain, but the overall workflow is still pretty much the same!
In the next episodes, we’ll improve our unit navigation system and implement a system of formations to make the units placement follow specific patterns…