Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[newbies] How to spawn building like RTS Game

Discussion in 'Scripting' started by Kyson, Dec 10, 2014.

  1. Kyson

    Kyson

    Joined:
    May 31, 2014
    Posts:
    4
    Hi All,

    I am currently learning on how to develop a game which required player to select a building and build it on specific location.

    This is what I trying to do now.
    1) I create a canvas, with a button, which initially plan to act as one of the building type.
    2) when player click on the button, set the selected building to a game object.
    3) then it should wait for player to click on terrain to get the location clicked.
    4) spawn the selected building on the location.

    But I have totally no idea on how to do it, youtube and google doesn't inspired me any idea thus.
    I am trying to write some script but the "Building" (which is just a cube) is spawn on button area.

    Hopefully anyone here can give me some hint about this.

    This is my code, which i put in inside a button :-

    using UnityEngine;
    using System.Collections;

    public class BuildingSpawner : MonoBehaviour {

    public GameObject spawnObject;
    private Vector3 buildingLocation;
    private Quaternion buildingRotation;

    // Mouse Click event
    public void btnBuildingClick()
    {
    buildingRotation = new Quaternion(0, 0, 0, 0);

    Debug.Log(buildingRotation);


    getBuildLocation();
    Debug.Log(buildingLocation);


    if (buildingLocation.x != 0)
    {
    spawnBuilding();
    }
    }

    // Get build location from mouse click
    void getBuildLocation()
    {
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit, 1000))
    {
    // Define position to destination
    buildingLocation = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    Debug.Log(buildingLocation);
    }
    }

    void spawnBuilding()
    {
    Instantiate(spawnObject, buildingLocation, buildingRotation);
    }

    }
     
  2. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Hey there, welcome to the forums! Whenever you post code please use the CODE tags.

    For building instantiation I use something like this (written from memory so it might not compile)


    Code (CSharp):
    1. //The building prefabs, this is just an array of GameObjects that is set through the inspector.
    2. public GameObject[] Buildings;
    3.  
    4. //This is the layer that the terrain is on
    5. public LayerMask acceptableLayer;
    6.  
    7. //Defines wether we're checking for a valid position
    8. private bool currentlyBuilding = false;
    9.  
    10. //The position at which we'll spawn the building
    11. private Vector3 position;
    12.  
    13. //The building we're currently placing
    14. private GameObject currentBuilding;
    15.  
    16. void Update(){
    17.     if(currentlyBuilding){
    18.         //Send a ray from the mouse cursor to the terrain and see if it hits
    19.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    20.         RaycastHit hit = new RaycastHit();
    21.      
    22.         //We hit the terrain, assign the ray hit position
    23.         if(Physics.Raycast(ray, out hit, Mathf.Infinity, acceptableLayer)){
    24.             position = hit.point;
    25.         }
    26.         //We didn't hit the terrain.
    27.         else{
    28.             position = Vector3.zero;
    29.         }
    30.      
    31.         //If the user presses the left mouse button call SpawnBuilding
    32.         if(Input.GetMouseButtonDown(0)){
    33.             SpawnBuilding();
    34.         }
    35.     }
    36. }
    37.  
    38. //The will be called from your UI button.
    39. public void AssignBuilding(int buildingIndex){
    40.     currentBuilding = Buildings[buildingIndex];
    41.     currentlyBuilding = true;
    42. }
    43.  
    44. //Spawn the building if the current position is valid.
    45. public void SpawnBuilding(){
    46.     if(position != Vector3.zero){
    47.         Instantiate(currentBuilding, position, currentBuilding.transform.rotation);
    48.     }
    49. }
    Of course this is very basic, there are a lot of things to look for depending on the environment you're in. Is the building over a river? On a cliff? Do you have enough resources to build it?

    Let me know if you have any questions!
     
  3. Kyson

    Kyson

    Joined:
    May 31, 2014
    Posts:
    4
    Hi Ensiferum,

    I have studies your code and will give it a try. Your code did gave me some idea too.
    Thanks for your feedback ! Let's have some luck blessed on me :)
     
  4. Flowered

    Flowered

    Joined:
    Feb 13, 2015
    Posts:
    6
    Did you get it to work ?
    I am tryting to figure These things out aswell,
    ~ Flowered