Building a real-time strategy game in Unity 4.1 using C# scripting

>

Part 1: Initial Setup

Before we can even begin doing any coding we need to make sure that our environment is set up correctly. The goal for this part is to get things up and running and have a very basic world rendering on screen.

Setup

The first thing to do is download and install Unity 3D, if you have not already done so, which can be found here. Make sure that you grab Unity 4.1, since I know that 4.2 has some subtle differences in the api. I do not believe it matters which platform you use, since I have been doing coding for this in both Windows7 and OSX. If you have any trouble using a particular 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:

It can also be useful to see the console output as well, though we will not be using it in these tutorials. Personally I find when I am developing that it is useful to be able to print out debugging information to the console while play-testing. It is one of the tools that I use to find out what is going on at runtime.

A quick not on some shorthand that I will use when referencing some things. (File -> New Project ...) means open the 'File' menu and select the 'New Project ...' item. (Game Object -> Create Other -> Plane) means open the 'Game Object' menu, then open the 'Create Other' submenu and select the 'Plane' item from it. I will typically only explain how to use a feature in a menu once, so it is worth paying attention when it is mentioned.

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. Make sure that you save the project somewhere sensible that you will remember. 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.

First Objects

The last thing I want to cover this time is actually being able to see something happening, otherwise most of this post is just a boring overview. Let's add 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.

Ground Settings

Ground Settings

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 the y-axis) by half of it's height (the y-value of it's scale).

Cube Settings

Cube Settings

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 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.

Sun Settings

Sun Settings

The last thing to do here 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).

Camera Settings

Camera Settings

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.

Final Scene

Final Scene

Next time we will look at adding some framework details to build our game on and then we will make our camera interactive.

>