Update: This can now also be found at stormtek.geek.nz/rts_tutorial/part1.php, where the entire tutorial is now being hosted.
This forms the beginning of a series of tutorials on building an RTS in Unity 3D. I will be using the free version of Unity 4.0 for Windows with C# scripting. There will be a lot of programming, so it is assumed that you have an understanding of basic programming principles and at least a basic familiarity with either C# or a similar language (e.g. Java, C++). I will be covering why we are doing things a certain way, but I won’t be stopping to discuss any language stuff in detail. It is also assumed that you have a basic knowledge of Unity – as much as is covered in the beginners tutorials anyway. Earlier posts will include a bit more detail on Unity (including some screenshots), but as we progress this will diminish.
All of the source code will be freely available on my github account. I will make a commit for each post, so you will be able to step through the revision history alongside each post. This will include the Unity project as well as the source code.
The goal is to produce a game that includes all of the core mechanics needed to play a modern real-time strategy game. The core features we will be aiming for are:
- Multiple players, one of which will be controlled by the player
- Selection of game objects – whether units, buildings, or resources
- The ability to issue commands to any units / buildings that the player controls
- The ability to create new units from specified buildings
- The ability for specified units to be able to construct new buildings
- The ability to collect resources, thus enabling a simple economy
- An interactive display that shows the player
- What resources they currently have
- What game object they have selected
- What options a selected game object has
- A functional menu system
- The ability to save and load games
Obviously this is no light undertaking. Just looking at that list is daunting … but it is entirely possible. I will attempt to keep things as short as possible, but I do not wish to skimp on any of the necessary details either. As we progress I will also attempt to lay out my reasoning for certain design decisions. So, it is time to begin …
The first thing to do is download and install Unity 3D, if you have not already done so, which can be found here. I do not believe it matters which platform you use, but I am using Windows 7. If you have any trouble using another platform, I apologize in advance. Once you have installed Unity, tweak the layout of the interface to fit your preferences. The areas that are important to be able to see are
- Project view
- Scene view
- Game view
It can also be useful to see the console output as well, though we will not be using it in these tutorials. But when developing it is useful to be able to print out debugging information to the console while you are play-testing.
Now that you have installed Unity, it is time to create a new project for our game. (File -> New Project …) I will call it RTS Tutorial, but you are free to give it any name you choose. By default (on Windows, at least) Unity will put this under MyDocuments, but it is easy enough to change this when you are creating the project. Do not worry about adding any extra packages at this stage.
When we create a new project Unity presents us with an empty scene that contains a camera. The first thing we want to do is to save this scene. (File -> Save Scene) Let’s call this scene Map since we will be using it to play around with a map for most of the project. Once you have hit save you should see the scene file called map now located in the Assets folder for your project.
The last thing I want to cover this time is actually being able to see something happening, otherwise most of this post is just boring overview. Let’s add some ground for things to sit on and a cube to give us a point of reference.
For the ground we will create a plane and rename it Ground. (Game Object -> Create Other -> Plane) Let’s set it’s position to (0,0,0) and it’s scale to (100,1,100). This will make the centre of our ground to be the origin and it will extend 100 units in either direction. Note that we are using the y-axis for height off the ground. This will be the case for everything in our world.
Now create a cube (Game Object -> Create Other -> Cube), position it at (0,2,0), and set the scale to (10,4,10). The centre of all objects is in the middle of the volume, so to sit an object on the ground we must move it up (in they-axis) by half of it’s height (the y-value of it’s scale).
At the moment our scene only contains ambient light. This means that our rendered scene will not be black, but we will also not be able to distinguish things like the edges of objects. We can fix that by adding a point light to the scene. (Game Object -> Create Other -> Point Light) Let’s rename it Sun, since we want it to simulate a sun somewhere at a distance. Position the newly created sun at (100,400,100) so it is off to the side of the centre of our little world and up quite high. Now set the range for the light to be 1000 so that it spreads light across most of the ground.
The last thing to do is to position our camera so that we can see our cube. Select the camera and set it’s position to (20,10,-20) and it’s rotation to (15,-45,0).
And that is all for part 1. We now have ground that can be extended when we want to, a light to simulate the sun, a cube for reference, and a camera to see things. If you hit play now you should see a scene very similar to the one below.
Next time we will look at adding some framework details to build our game on and then we will make our camera interactive.