Search Unity

City Building game

Discussion in 'Works In Progress' started by roger0, Feb 11, 2012.

  1. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    Greetings. I am trying to learn about building a RTS game or Sim City like game. I've decided to jump right in and see how far I get. So far the game allows selecting objects from a menu of icons, then once selected, lets them place the objects with the mouse cursor by left clicking. Theres also a cost for each shape, once the money runs out it says out of money.

    One problem im having that I must figure out before going on is, when the icons are clicked, it spawns the object at the same time. Here is my code.

    feedback is helpful. Thanks!
     

    Attached Files:

    Last edited: Feb 11, 2012
  2. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Very good!
     
  3. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    157
    One method to avoid this is to
    • Assign a tooltip to each button (e.g. "Build a box")
    • Only build if the current tooltip is ""
    This would look like this (code not tested) :
    Code (csharp):
    1.  
    2. function OnGUI ()
    3. {
    4.     if (GUI.Button (Rect (10,10, 50, 50), GUIContent( boxIcon, "Build a box") ))
    5.     {
    6.         building = GameObject.Find("Cube");
    7.         buildingCost = cubeCost;
    8.     }
    9.  
    10.     if(GUI.Button (Rect (60,10,50,50), GUIContent( sphereIcon, "Build a sphere") ))
    11.     {
    12.         building = GameObject.Find("Sphere");
    13.         buildingCost = sphereCost;
    14.     }
    15.  
    16.     if(GUI.Button(Rect ( 110,10,50,50), GUIContent( cylinderIcon, "Build a cylinder") ))
    17.     {
    18.         building = GameObject.Find("Cylinder");
    19.         buildingCost = cylinderCost;
    20.     }
    21. }
    22.  
    23. function Update ()
    24. {
    25.     if(Input.GetMouseButtonDown(0)  GUI.tooltip == "" )
    26.     {
    27.         etc...
    28.  
    It's a bit of a hack, but it works.
     
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    Hello.

    I tried the code you posted, although it does not stop the objects from spawning when the buttons pushed. And there is no tooltip that shows.
     
  5. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    can anyone help?
     
  6. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    *bump*
     
  7. BloodSin

    BloodSin

    Joined:
    Nov 27, 2010
    Posts:
    28
    the way i see it its more like, you reference to the object and you have no state function that "stores information about the object into ur click memory to be able to place it". loosely translated my thougth there.
    you should check reference related to that. since thats why it autospawns it.
     
  8. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    Alright I figured out the problem I was having. But now i'm trying to figure out how to make a building reference while placing buildings. An example would be, click on the box icon, a transparent model of the box sticks to the mouse while you go over the terrain. Once you click the mouse button it places the object.

    I am trying to work on the cube object first to make it happen. The other object that will be the object reference for the cube will be the 'cubeGhost'. So far the cubeGhost just spawns once I click the button. It needs to stick to the mouse cursor.

     
  9. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    anyone have any ideas as to how to do it?
     
  10. Norrtec

    Norrtec

    Joined:
    Nov 7, 2011
    Posts:
    10
    I think you're on the right track with:

    The problem is that the code is only run when you click on the "Build a Box" button. So the code probably works, the problem comes from where it's referenced. I'd write the code, but I'm in class right now :p
     
  11. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    I'm not sure how to make the script update every frame so it stays on the cursor. If i put it in the update function than its not related to the button being pressed.
     
  12. Norrtec

    Norrtec

    Joined:
    Nov 7, 2011
    Posts:
    10
    There's got to be an easier way, but my brain can't think of it right now. For lack of a better solution:
    Code (csharp):
    1.  
    2. var cash : int = 2000;
    3. var boxIcon : Texture2D;
    4. var sphereIcon : Texture2D;
    5. var cylinderIcon : Texture2D;
    6. var cubeCost : int = 400;
    7. var sphereCost : int = 200;
    8. var cylinderCost : int = 600;
    9. var cubeGhost : Transform;
    10. var cylinderGhost : Transform;
    11. var sphereGhost : Transform;
    12. private var building;
    13. private var buildingCost : int = 0;
    14. var showGhost : boolean = false;
    15.  
    16.  
    17. var isHoverGUI : boolean = false;
    18. var rect : Rect = Rect (10,10, 150, 50);
    19.  
    20. function OnGUI ()
    21. {
    22.  
    23. if (GUI.Button (Rect (10,10, 50, 50), GUIContent( boxIcon, "Build a box") ))
    24. {
    25. building = GameObject.Find("Cube");
    26. buildingCost = cubeCost;
    27. var ghost = cubeGhost;
    28. }
    29.  
    30. if(GUI.Button (Rect (60,10,50,50), GUIContent( sphereIcon, "Build a sphere") ))
    31. {
    32. building = GameObject.Find("Sphere");
    33. buildingCost = sphereCost;
    34. ghost = sphereGhost;
    35. }
    36.  
    37. if(GUI.Button(Rect ( 110,10,50,50), GUIContent( cylinderIcon, "Build a cylinder") ))
    38. {
    39. building = GameObject.Find("Cylinder");
    40. buildingCost = cylinderCost;
    41. ghost = cylinderGhost;
    42.  
    43. }
    44. isHoverGUI = rect.Contains( Event.current.mousePosition );
    45. }
    46.  
    47. function Update () {
    48.  
    49. if(showGhost == true) {
    50. var hitGhost : RaycastHit;
    51. var rayGhost : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    52. if(Physics.Raycast(rayGhost,hitGhost,300)) {
    53. ghost.position = hitGhost.point;
    54. }
    55. }
    56.  
    57. if(Input.GetMouseButtonDown(0)  !isHoverGUI )
    58. {
    59.  
    60. var hit : RaycastHit;
    61. var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    62. Destroy(ghost);
    63. if(Physics.Raycast(ray,hit,300)&building  cash >= buildingCost)
    64. {
    65. if(hit.transform.tag == "terrain")
    66. {
    67. Instantiate(building,hit.point,Quaternion.identity );
    68.  
    69.  
    70. switch(building)
    71. {
    72. case GameObject.Find("Sphere"):
    73. cash -= 200;
    74. break;
    75.  
    76. case GameObject.Find("Cube"):
    77. cash -= 400;
    78. break;
    79.  
    80. case GameObject.Find("Cylinder"):
    81. cash -= 600;
    82. break;
    83.  
    84. default: print ("nothings the case!");
    85. break;
    86. }  
    87. }  
    88. }
    89. else
    90. {
    91. print("not enough money");
    92.  
    93. }
    94. }
    95. }
    96.  
    Or something like that, I have to get to bed.
     
    Last edited: Feb 14, 2012
  13. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    I've tried something like yours, the cubeGhost reference appears once I click on the box icon and sticks to the mouse. Although, it immediatly moves toward the camera view and hits the camera. Then it repeats the process continueously. I also can't place any buildings while its doing this.

    It takes a second or two for it to travel from the ground to the camera. Then once its at the camera it goes back to the ground and cycles through. I have no clue as to whats causing this.


    Code (csharp):
    1. var cash : int = 2000;
    2. var boxIcon : Texture2D;
    3. var sphereIcon : Texture2D;
    4. var cylinderIcon : Texture2D;
    5. var cubeCost : int = 400;
    6. var sphereCost : int = 200;
    7. var cylinderCost : int = 600;
    8. var cubeGhost : Transform;
    9.  
    10. private var building;
    11. private var buildingCost : int = 0;
    12. private var ghost;
    13. private var showGhost : boolean = false;
    14.  
    15. var isHoverGUI : boolean = false;
    16. var rect : Rect = Rect (10,10, 150, 50);
    17.  
    18. function OnGUI ()
    19. {
    20.  
    21.     if (GUI.Button (Rect (10,10, 50, 50), GUIContent( boxIcon, "Build a box") ))
    22.     {
    23.         building = GameObject.Find("Cube");
    24.         buildingCost = cubeCost;
    25.         ghost = cubeGhost;
    26.         showGhost = true;
    27.    
    28.  
    29.  
    30. }
    31.    
    32.  
    33.    if(GUI.Button (Rect (60,10,50,50), GUIContent( sphereIcon, "Build a sphere") ))
    34.     {
    35.         building = GameObject.Find("Sphere");
    36.         buildingCost = sphereCost;
    37.     }
    38.  
    39.     if(GUI.Button(Rect ( 110,10,50,50), GUIContent( cylinderIcon, "Build a cylinder") ))
    40.     {
    41.         building = GameObject.Find("Cylinder");
    42.         buildingCost = cylinderCost;
    43.        
    44.        
    45.  
    46.     }
    47.         isHoverGUI = rect.Contains( Event.current.mousePosition );
    48. }
    49.  
    50. function Update () {
    51.  
    52. if(showGhost == true)
    53. {
    54. var hitGhost : RaycastHit;
    55. var rayGhost : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    56. if(Physics.Raycast(rayGhost,hitGhost,300)){
    57.        
    58.             ghost.position = hitGhost.point;
    59.             }
    60. }
    61.  
    62.  
    63. if(Input.GetMouseButtonDown(0)  !isHoverGUI )
    64. {
    65.    
    66. var hit : RaycastHit;
    67. var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    68.  
    69. if(Physics.Raycast(ray,hit,300)&building  cash >= buildingCost)
    70. {
    71. if(hit.transform.tag == "terrain")
    72. {
    73. Instantiate(building,hit.point,Quaternion.identity);
    74.  
    75.  
    76. switch(building)
    77. {
    78.     case  GameObject.Find("Sphere"):
    79.     cash -= 200;
    80.     break;
    81.    
    82.     case   GameObject.Find("Cube"):
    83.     cash -= 400;
    84.     break;
    85.    
    86.     case GameObject.Find("Cylinder"):
    87.     cash -= 600;
    88.     break;
    89.    
    90.     default: print ("nothings the case!");
    91. break;
    92.                     }  
    93.                 }                  
    94.             }
    95.             else
    96.             {
    97.             print("not enough money");
    98.            
    99.             }
    100.         }
    101.     }
     
    Last edited: Feb 14, 2012
  14. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    507
    As a side note, you may wanto change this code:

    to this:

    The purpose of undoRefund, is if the person decides to not place the building (presses escape or something) or does a ctrl-z after placing the building.

    ... and you might want to check if the player has enough money after they click the icon, before you shows the ghost object.
     
  15. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,433
    You may also want to ask this sort of question in the scripting forum next time.
    This forum is for showcasing work you are currently doing to other people.
     
unityunity