Making a RTS game #33: Using Unity’s terrain tools (Unity/C#)

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!

Compared to other primitives, terrains are very, very large!

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# 🙂

Painting textures

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

Going further

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 😉

Conclusion

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…

6 thoughts on “Making a RTS game #33: Using Unity’s terrain tools (Unity/C#)”

  1. Wohoo!!
    And here the formations are coming. =) This great tutorial is getting even greater with every new part it gets. 😀

    By the way… I have some combat related topics…
    a) We can have melee attacks, but we need physical range attacks (so projectiles don’t follow enemy movement and follow gravity) and magical range attacks (following enemy movement)
    b) Normally you have something like a builder unit that constructs buildings. Do you plan to implement somethinglike this? Currently we’re using buildings like stamps. 😉
    c) This is an edge case, but what about buildings characters can move into? I guess this is just a small addition to the Navigation Tutorial, but it would be nice if you cover this topic and point out what someone has to do to make characters enter his buildings (this is always a topic if you have walls and gates)

    1. Hi, thanks so much! I’m really happy you like the new episodes 😀
      Yup, formations are on, now – a two-fold episode that’s going to talk about messaging and leadership… I won’t spoil you, but they are nearly ready as we speak 😉

      Ha, new ideas, youpi! So:
      a) the “physical” range attacks are a really good idea but here I’m going more for an “auto-aim” thing, like in Warcraft 3 for example: you basically have infinitely fast projectiles (like arrows) that can’t really miss. However, this would be a really good idea for a standalone tutorial…
      b) I haven’t decided yet if I’m going to have builders or not. At the moment, the building part is more in the Industry Giant style – you are an all-mighty player who instantly creates new buildings. But the more common “builder-building time” combo could be a sweet change. I’ll think about it, and I might update this system in the series later on 🙂
      c) this is not on the list of topics for this RTS tutorial because I’m treating units as “solid” here, and a game like this one can’t really have “interior” mechanics… so I guess it would mostly be for visual effects? I believe for an RTS it’s a bit far-fetched and “costly”, compared to other systems that I still have to discuss and that are more important (in my opinion).

      But this is definitely something I’ll keep in the back of my head… especially if I decide to go with my recent idea of an Hack’n’slash Unity tutorial: here the exterior/interior would be very very cool!

      Again, gosh! your ideas are so helpful – I’m sometimes struggling to produce content everyday and thanks to you I now have some backlog of Unity tutorials :p

      1. Always happy to hear that you like my ideas. 😉
        You referenced a lot on RTS like AoE or Warcraft so I never got the hint you were thinking in an Industry Giant style….

        Just to clearify my idea c) from last post… I don’t want Sims-like interior. I just want to spawn soldiers inside a building and let them move out. And perhaps I like to position soldiers on the walls of a tower… So no need for furniture or something else… But I guess I just have to be careful with the colliders and make the doors big enough that the updateNavMeshSurface() can consider the building as moveable. 😉

        Here is another part an RTS should have: Collectable resources – like trees or stone fields a worker unit can harvest. Atm we have Industry Giant style buildings that harvest resources out of thin air.
        Thats okay, though but while keeping in this style there need to be an adjustor, like a sawmill being much more efficient the more trees are around. (idea #2 😛 ).

        1. Yep, I do draw inspiration from various sources, and I like Warcraft 3 a lot so I always talk a lot about this one… but in the building resource production part I did put a few screenshot of Industry Giant to clearly state that this is what I am making here 😉
          Hum but… there is an adjustment of the production depending on the number of rocks/trees around! 🙂 If you take a look back at episode #22, you’ll see that we compute a “production score” that is a mix of the building level and the amount of resources around it.

          For now, I don’t plan on having actual units move around to collect the resources, but that may change in the future – we’ll see…

          For your idea, yup, it seems like it’s “just” about adjusting the collider sizes. This can get tricky but hopefully Unity’s AI navigation system is tweakable enough for you to work this out! By the way, depending on your different zone types, you can/should perhaps take a look at the nav areas and nav off-mesh links. Don’t know if it’s useful for you, but just in case!

          Cheers 🙂

          1. My apologies, #22 indeed slipped out of my mind.

            I’m a big fan of the Settler series, so I like units running around and carry stuff from A to B. =)
            At the moment I’m implementing a task system for workers. So buildings have one or more stacks of a resource. If the stacksize is <8 the building will create a task to have an idle worker transport one good from a supplier stack to the requester stack.
            The worker will move back home (house) and wait there for the next job.
            The building will have auto skills like a sawmill transforming wood into planks.

          2. No worries 🙂

            Ah yay, the Settlers! That’s a really cool series, too, I’ll admit. Truth be told, one of the reasons I didn’t go for the “visual workers moving around” was that it was a bit harder to code and thus to explain in a tutorial. But I guess now that we have the behaviour trees all set up, it would be more straight-forward… arg, too tempting!! 😀

            That sounds really nice! I definitely feel the Settlers vibe 😉
            So would you be able to specifically ask/refuse some goods in some buildings? (A bit like Sierra’s Caesar/Emperor games, for example?)

            And do the buildings need some of the “transporters” to be converted to expert workers to perform their task?
            (Like you would have a “pool” of citizens and some would be transporters, others carpenters, other blacksmiths, etc.)
            It’s a system I’ve seen in various recent management games, too, like Foundation, and I feel like the difficulty with this technique is to clearly show the player how the citizens are assigned, but it’s still a pretty interesting mechanic.

Leave a Reply

Your email address will not be published. Required fields are marked *