3D Real-Time-Strategy (RTS) Game Tutorial - Unity3D _ Coffee Break Codes

5

Click here to load reader

description

game tutorial

Transcript of 3D Real-Time-Strategy (RTS) Game Tutorial - Unity3D _ Coffee Break Codes

  • 12/06/2015 3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes

    http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/ 1/5

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

    5

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

    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 startwith 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 cameraposition 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. Iuseda street package named Cartoon City by Razu Ahmed (http://bit.ly/1cmYnX2 ) that you can find out lots of objects. Add 2 buildings to thescene 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.

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

    HainesFire&RiskSpecialhazardfireprotectionengineering,investigation&safety

    12345678910111213

    public class Buildings{ public int health; public string name; public GameObject avatar; public Buildings(int _health, string _name, GameObject _avatar){ health = _health; name = _name; avatar = _avatar; } }

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

    Unity3D Tutorials

  • 12/06/2015 3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes

    http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/ 2/5

    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.

    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.

    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 thathas Building tag and count decrease if the new building exits from the collision. Now open Placement script.

    1 using System.Collections.Generic;

    123

    public List buildingList = new List (); public Buildings newBuilding; public GameObject[] buildings;

    1234567

    void Start(){ for (int i = 0; i < buildings.Length; i ++) { newBuilding = new Buildings (100, buildings[i].name, buildings[i] ); buildingList.Add(newBuilding); } }

    1234567891011121314151617181920

    private Building buildingClass; private bool isClickable; void Start () { buildingClass = GetComponent (); isClickable = true; } public void SetClickable(bool w){ isClickable = w; } void OnGUI(){ for (int i = 0; i < buildingClass.buildings.Length; i ++) { if (GUI.Button(new Rect(50 * 2 * i, 100 , 75, 30), buildingClass.buildings[i].name) && isClickable){ isClickable = false; } } }

    123456789101112131415161718192021222324252627282930

    public List colliders = new List (); private bool isSelected; private Building buildingClass; void Start () { buildingClass = GetComponent (); } void OnTriggerEnter(Collider c){ if (c.tag == "Building") { colliders.Add(c); } } void OnTriggerExit(Collider c){ if (c.tag == "Building") { colliders.Remove(c); } } public void SetSelected(bool b){ isSelected = b; } void OnGUI(){ if (isSelected) { GUI.Button(new Rect(100, 200, 100, 50), name); } }

    123456789101112

    public LayerMask bMask; private Transform currentBuilding; private Available available; private bool isPlaced; private Available availableX; private Building newBuilding; private Manager clickable; void Update () { Vector3 mPos = Input.mousePosition;

  • 12/06/2015 3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes

    http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/ 3/5

    In this section, we assign the building position when it is instantiated by clicking the button via SetItem() function. Also, we get the functionwhen 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. Alsoyou 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#)

    HainesFire&RiskSpecialhazardfireprotectionengineering,investigation&safety

    1314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

    mPos = new Vector3 (mPos.x, mPos.y, transform.position.y); Vector3 pos = camera.ScreenToWorldPoint (mPos); if (currentBuilding != null && !isPlaced) { currentBuilding.position = new Vector3 (pos.x, 0, pos.z); if (Input.GetMouseButtonDown (0)) { if (IsAvailable ()) { isPlaced = true; clickable = gameObject.GetComponent (); 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 ().SetSelected(true); availableX = hit.collider.gameObject.GetComponent (); } else{ if(availableX != null){ availableX.SetSelected(false); } } } } } public void SetItem(GameObject g){ isPlaced = false; currentBuilding = ((GameObject)Instantiate(g)).transform; available = currentBuilding.GetComponent (); } bool IsAvailable(){ if (available.colliders.Count > 0) { return false; } return true; }

    1234567891011121314151617181920212223242526

    private Building buildingClass; private Placement placement; private bool isClickable; void Start () { placement = GetComponent (); buildingClass = GetComponent (); isClickable = true; } public void SetClickable(bool w){ isClickable = w; } void OnGUI(){ for (int i = 0; i < buildingClass.buildings.Length; i ++) { if (GUI.Button(new Rect(50 * 2 * i, 100 , 75, 30), buildingClass.buildings[i].name) && isClickable){ isClickable = false; placement.SetItem(buildingClass.buildings[i]); } } }

  • 12/06/2015 3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes

    http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/ 4/5

    Leave a commentYour email address will not be published. Required fields are marked *

    Name *

    Email *

    Website

    Comment

    You may use these HTML tags and attributes:

    PostComment

    Notify me of follow-up comments by email.

    Notify me of new posts by email.

    2 thoughts on 3D Real-Time-Strategy (RTS) Game Tutorial Unity3DReply AliquidHacker

    June 8, 2015 at 12:24 pmAny hope for part 2?

    Reply Mocha Post authorJune 8, 2015 at 1:50 pmWill be:)

    Donate and help us to improve our totally FREE service.

  • 12/06/2015 3DRealTimeStrategy(RTS)GameTutorialUnity3D|CoffeeBreakCodes

    http://coffeebreakcodes.com/3drealtimestrategyrtsgametutorialunity3d/ 5/5

    Back to top

    Created by Kagan Kartal

    2015 Coffee Break Codes Designed by Press Customizr