3D Real-Time-Strategy (RTS) Game Tutorial – Unity3D


3D Real-Time-Strategy (RTS) Game Tutorial – Unity3D (C#).

rts2

Play Demo

Part 2 – Camera Movement

Part I: Building Placement

In this tutorial series, we will develop the RTS basics such as building placement, camera controls, unit controls, creating units etc. Lets start with Building placement.

As you know from legends of RTS games, some different buildings can be built.

Open Unity and create a new 3D project. Most of RTS games have a top camera. So, create a terrain, create a directional light and change camera position for looking downward. You can add grass texture to terrain if you want. Also, you can use some building objects or just cubes for now. I used a street package named “Cartoon City” by Razu Ahmed ( http://bit.ly/1cmYnX2 ) that you can find out lots of objects. Add 2 buildings to the scene and create 2 empty game objects as their parents. For each parent, add Collider and Rigidbody. Create prefabs and delete objects in scene.

Screen Shot 2015-05-29 at 13.22.26 Screen Shot 2015-05-29 at 13.22.35

First of all, we need a Building class which includes health, game object and name. Create a C# script named Building.

Add Generic library because we are going to use list type.

Now, create a list for game objects and a list for Building class objects.

We are going to add 3D building objects manually. Create buildings according to these objects.

 

That’s it. Our building class is ready. Now, create 3 C# scripts to check availability, place buildings and create GUI: Available, Placement, Manager. Open Manager script.

 

This script gets names from Building class and creates buttons.

Open Available script. Add generic library for using lists.

 

Collider list keeps count of colliding that provides if it is available to place a building. Count increase if the new building collides any object that has Building tag and count decrease if the new building exits from the collision. Now open Placement script.

 

In this section, we assign the building position when it is instantiated by clicking the button via SetItem() function. Also, we get the function when clicking on a building with ray. It is just like an invisible laser from camera to the downward. It returns something if it hits any object. IsAvailable() checks if the new building collides anything. Open Manager script and edit it as seen below.

Now, attach Manager, Placement and Building to Main Camera and attach Available to each prefab. Do not forget to assign your prefabs. Also you can add grid to your buildings. Create a Grid script and attach it to each prefab. For more information about Grid function, see Grid Tutorial.

You can download the source code here.

©Coffee Break Codes – 3D Real Time Strategy Game Tutorial – Unity3D (C#)


Leave a comment

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

11 thoughts on “3D Real-Time-Strategy (RTS) Game Tutorial – Unity3D