Professional Documents
Culture Documents
3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes
Unity3D Tutorials
HainesFire&Risk
Specialhazardfireprotectionengineering,investigation&safety
Play Demo
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 RTSgames, 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
useda 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.
First of all, we need a Building class which includes health, game object and name. Create a C# script named Building.
1
2
3
4
5
6
7
8
9
10
11
12
13
http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/
1/5
12/06/2015
3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes
1 using System.Collections.Generic;
Now, create a list for game objects and a list for Building class objects.
1
2
3
We are going to add 3D building objects manually. Create buildings according to these objects.
1
2
3
4
5
6
7
void Start(){
Thats 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
This script gets names from Building class and creates buttons.
Open Available script.Add generic library for using lists.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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.
1
2
3
4
5
6
7
8
9
10
11
12
Transform currentBuilding;
Available available;
bool isPlaced;
Available availableX;
Building newBuilding;
Manager clickable;
void Update () {
Vector3 mPos = Input.mousePosition;
http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/
2/5
12/06/2015
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes
mPos = new Vector3 (mPos.x, mPos.y, transform.position.y);
Vector3 pos = camera.ScreenToWorldPoint (mPos);
if (Input.GetMouseButtonDown (0)) {
if (IsAvailable ()) {
isPlaced = true;
clickable = gameObject.GetComponent<Manager> ();
clickable.SetClickable(true);
}
}
}
else {
if (Input.GetMouseButtonDown(0)){
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(new Vector3(pos.x, 10, pos.z), Vector3.down);
if (Physics.Raycast(ray, out hit, Mathf.Infinity, bMask)){
if(availableX != null){
availableX.SetSelected(false);
}
hit.collider.gameObject.GetComponent<Available> ().SetSelected(true);
availableX = hit.collider.gameObject.GetComponent<Available> ();
}
else{
if(availableX != null){
availableX.SetSelected(false);
}
}
}
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.
HainesFire&Risk
Specialhazardfireprotectionengineering,investigation&safety
http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/
Coffee Break Codes 3D Real Time Strategy Game Tutorial Unity3D (C#)
3/5
12/06/2015
3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes
Leave a comment
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
Comment
PostComment
Notify me of follow-up comments by email.
Notify me of new posts by email.
Reply
Mocha
Post author
Reply
Will be:)
http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/
4/5
12/06/2015
3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes
2015 Coffee Break Codes Designed by Press Customizr
Back to top
http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/
5/5