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

Placing buildings

Discussion in 'Scripting' started by Stormy102, Jul 6, 2014.

  1. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    I would like to have a script, where if you have a certain amount of logs and stones, you can create a campfire. I want a raycast from the middle of the screen to hit a colldier, and instantate a prefab. I then want this prefab to follow the raycast's position, but all my efforts so far have failed. I will post the code for both of these scripts, the GUI and the handler.
    Builder_GUI.js

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var WindowLoc : Rect = Rect(Screen.width/4, Screen.height/4, Screen.width /2, Screen.height /2);
    4. private var scrollBarChopGrid : Vector2 = Vector2.zero;
    5. private var ShowBuildingMenu : boolean = false;
    6. private var Building_Handler : Building_Handler;
    7.  
    8. var ToggleBuild : KeyCode = KeyCode.P;
    9.  
    10. function Start()
    11. {
    12.     Building_Handler = GetComponent("Building_Handler");
    13. }
    14.  
    15. function Update()
    16. {
    17.     if (Input.GetKeyDown(ToggleBuild))
    18.         if (ShowBuildingMenu)
    19.         {
    20.             ShowBuildingMenu = false;
    21.            
    22.         }
    23.         else
    24.             ShowBuildingMenu = true;
    25. }
    26.  
    27. function OnGUI () {
    28.     if (ShowBuildingMenu)
    29.         WindowLoc = GUI.Window(0, WindowLoc, Building_Selection, "");
    30. }
    31.  
    32. function Building_Selection(windowID : int)
    33. {
    34.     GUI.Label(Rect(5, 0,100,25), "<b>Item to build:</b>");
    35.     GUI.Label(Rect(110, 0,200,25), "<b>Resources required:</b>");
    36.     scrollBarChopGrid = GUI.BeginScrollView(Rect (0, 25, Screen.width/2,257), scrollBarChopGrid, Rect(0,0,0,500));
    37.        
    38.         GUI.Label(Rect(5, 0,100,25), "Camp Fire");
    39.         GUI.Label(Rect(110,0,200,25), "10xWood, 10xStone");
    40.         if (GUI.Button(Rect(315, 0,75,25), "Build"))
    41.         {
    42.             Building_Handler.Building = true;
    43.             Building_Handler.Once = true;
    44.             ShowBuildingMenu = false;
    45.         }
    46.     GUI.EndScrollView();
    47. }
    Builder_Handler.js
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. @HideInInspector
    4. var Building : boolean = false;
    5. var BuildCampFire : GameObject;
    6.  
    7. @HideInInspector
    8. var Once : boolean = false;
    9.  
    10. private var hitPointX : int = 0;
    11. private var hitPointY : int = 0;
    12. private var hitPointZ : int = 0;
    13.  
    14. function Update () {
    15.     if (Building)
    16.     {
    17.         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    18.         var hit : RaycastHit;
    19.         if(Physics.Raycast (ray, hit, 50))
    20.         {
    21.             var clone : GameObject;
    22.             hitPointX = hit.point.x;
    23.             hitPointY = hit.point.y;
    24.             hitPointZ = hit.point.z;
    25.             if (Once)
    26.             {  
    27.                 clone = Instantiate(BuildCampFire, Vector3(hitPointX,hitPointY + 0.5,hitPointZ), Quaternion.Euler(0,0,0));
    28.                 Once = false;
    29.             }
    30.             clone.position = Vector3(hitPointX,hitPointY,hitPointZ);
    31.            
    32.             if (Input.GetMouseButtonDown(0))
    33.             {
    34.                 Building = false;
    35.             }
    36.         }
    37.     }
    38. }
     
    Last edited: Jul 6, 2014
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
  3. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
  4. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    Anybody??
     
  5. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    You have some very glaring errors in your build handler.
    • You have Once as a boolean inside that Raycast. Since your other script makes it true automatically. This script will always set the building immediately when you click the GUI button to create a building.
    • You should have the Input handle the Instanciation and set the boolean of Building to false.
    • No need to create/reference the Clone gameObject until you plan on creating anything.
    • Not sure why you created three variables of floats to capture the hitPoint[axis]. If you really want to make it nicer just declare a new Vector3(0, 0.5f, 0) and add it to your result. This is the exact thing your already doing.

    Code (JavaScript):
    1.  
    2. #pragma strict
    3. @HideInInspector
    4. var Building : boolean = false;
    5. var BuildCampFire : GameObject;
    6. var BuildPositionModifier: Vector3 = Vector3(0, 0.5f, 0)
    7.  
    8. var hit : RaycastHit;
    9. var ray : Ray;
    10.  
    11. function Update ()
    12. {
    13.     if (Building)
    14.     {
    15.         ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    16.        
    17.         if(Physics.Raycast (ray, hit, 50))
    18.         {      
    19.             if (Input.GetMouseButtonDown(0))
    20.             {
    21.                 Instantiate(BuildCampFire, hit.point + BuildPositionModifier, Quaternion.Euler(0,0,0));
    22.                 Building = false;
    23.             }
    24.         }
    25.     }
    26. }
     
  6. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I just make my GUI instantiate a copy of the prefab I want and then attach this script to the prefab...

    Code (JavaScript):
    1. var isBeingPlaced : boolean = true;
    2. private var rayHitInfo : RaycastHit;
    3.  
    4. function Update(){
    5.     if(isBeingPlaced){
    6.         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),rayHitInfo,LayerMask.NameToLayer("Ground"))){
    7.             transform.position = rayHitInfo.point;
    8.         }
    9.         if(Input.GetButtonDown("Fire1")){
    10.             Placed();
    11.         }
    12.         if(Input.GetButtonDown("Fire2")){
    13.             Destroy(this.gameObject);
    14.         }
    15.     }
    16. }
    17.  
    18. function Placed(){
    19.     isBeingPlaced = false;
    20.    
    21. }
    This is in JS but the conversion to C# is stupid simple.
     
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    is there a question in there somewhere?
     
  8. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    No, was given him my code that does what he wants much easier. Why would I need to ask anything lol.
     
  9. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ah well miss read it somehow, sorry.
     
  10. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Notta problem. OP did it get ya where ya needed to be?
     
  11. Stormy102

    Stormy102

    Joined:
    Jan 17, 2014
    Posts:
    495
    You all helped me to get a good result. If you want to see it in action, there is a windows standalone which you can view it in action. Use wasd to move around and E to pickup. To view the crafting menu, hit p. Thanks guys for all your help!!

    P.S: This is an early build. When you have no more use for it, please feel free to discard!

    Link to standalone: https://www.dropbox.com/s/27pn8rfbvrfqaa5/[UNITY 3D] Survival.zip