Unity Real Time Strategy (RTS) Game
In this Tutorial we will make a Real Time Strategy game in Unity. As usual, everything will be explained so that a five year old could understand it.
To keep things as simple as possible, we will focus on the RTS (Real Time Strategy) genre fundamentals:
- Building a Castle
- Creating & Controlling units that attack the enemy
- A simple AI for the enemy
The beauty of this tutorial is the unbelievable simplicity, or in other words: only 270 lines of code!
Oh wait, one more thing: we won't need any complicated 3D-modeling tools either!
This tutorial assumes that you already know the Unity basics. If you don't know Unity yet, take a look at our easier Unity Tutorials like Unity 2D Pong Game first.
If you already know your way around Unity, then there is not much to be scared of. As usual we only use the techniques that are completely easy to understand. There will be no scary math involved!
Our game will use Unity 5.1.1f1. Newer versions should work fine as well, older versions may or may not work. The free version of Unity 5 now comes with all the engine features, which makes it the recommended version.
We all love to play those multi-million dollar titles like Age of Empires, but sadly we don't have that much money available. Instead we will go for a very simple, yet unique art style that can be created with Unity's GameObjects and a basic painting tool like Paint.NET. All we need are a few really low resolution textures consisting of just a few pixels, so we can use any painting tool that we are comfortable with.
It's usually a good idea to only use a handful of colors for simple games like ours. We will mostly use orange, white and black for the objects, red for the health bars and a cyan type for the ground. This gives us a pretty neat graphics style:
Project & Scene Setup
Creating the Project
Let's get to it. We will start Unity and select New Project:
We will name it rts, select any location like C:\, select 3D and click Create Project:
Afterwards we save it once via File->Save Scene with the name "scene" (without the "").
Unity automatically adds a Camera to the project. Let's position it in a way that makes it look down onto the world by selecting it in the Hierarchy and then modifying its settings in the Inspector:
Note: we don't see a difference yet because our scene is still empty.
Unity already added a Directional Light to the scene when we created it. Let's select it in the Hierarchy and then assign the following rotation and light settings to it:
Note: we can use any settings that we want, but the ones above will look good later on.
Filling the World
Now that everything was set up, we can fill the scene with all kinds of cool things.
The ground will just be a simple Unity plane. We can create it by selecting GameObject -> 3D Object -> Plane from the top menu. We will name it Ground and give it the following scale in the Inspector:
Note: it's important to give it exactly the name "Ground".
Let's add some color. At first we will create a very simple pixelated 40x40 px texture with our drawing tool of choice (for example, Paint.NET):
Note: right click on the image, select Save As..., navigate to the project's Assets folder and save it in a new Textures folder.
Afterwards we drag it from the Project area right onto the Ground plane:
Which gives us this effect:
It looks really smooth, but we want to go one step further and make it look pixelated to achieve a more unique style. Therefore we have to select the texture in our Project Area and then change the Import Settings in the Inspector to this:
Here is the result:
The smoothing effect is called Interpolation. It's usually a decent effect, but for our pixelated game we turned it off by setting the Filter Mode to Point and the Format to Truecolor.
Note: we will do this for every texture in our game.
Creating the 3D-Models
It's time to create the 3D models for our game. Don't worry, there are no CAD skills needed. We will do everything in Unity!
In Unity's GameObject->3D Object menu we can create basic geometry like Spheres, Cubes, Planes and Cylinders like those:
Then we put them all into an empty game object, name it and save it as a prefab by simply dragging it into the Project area (preferably into a new folder called "Prefabs").
Creating the Arrow
So to create an arrow that can be used to shoot things, we will first create a empty GameObject that holds the whole thing via GameObject->Create Empty and we name it Arrow. Then we create three new Cubes via GameObject->3D Object->Cube and put them into the Arrow GameObject like this:
Note: the Arrow is called the root object, and the Cubes are the child objects.
Now to make the whole thing an arrow, we just move, scale and rotate each cube until it looks like one. We came up with the following settings:
Alright, let's add some color to it. We will use a simple texture:
Note: right click on the image, select Save As... and save it in the project's Assets/Textures folder.
And then drag it from the Project Area onto each cube. Here is the result:
Now we don't want the arrow to be in the Scene all the time. Instead we will save our Arrow GameObject as an Arrow Prefab. More about this technique can be read in our Prefabs Tutorial.
To create the Prefab, we will simply create a new "Prefabs" Folder in our Project Area and then drag the arrow from the Hierarchy into the folder:
Now we can delete it from the Hierarchy.
Castles & Arrow Shooting Things
We will use the previously explained technique to create all the remaining 3D models for our game.
The most important thing to keep in mind is that the child GameObjects should always have a position somewhere close to (0, 0, 0), because being a child, their position is always relative to the parent. Or in other words: if a child has a X-coordinate of 1, it means that it's 'one meter in x-direction away from the parent', no matter where the parent is. Whenever we move the parent, all the child GameObjects move along with it.
Alright so let's create a castle with cubes at the following positions:
And a fighting unit with cubes at:
We can then duplicate the castle and name one of it 'CastlePlayer' and the other one 'CastleNpc'. We will also duplicate the fighting unit and name one of it 'ArrowShootingThingPlayer' and the other one 'ArrowShootingThingNpc'. Feel free to also put some simple colored textures onto them:
Note: we used orange and white for all Player models and black and white for all Npc models.
Here is what our Prefabs folder looks like now: