Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity instantiate object in a grid?

Discussion in 'Scripting' started by Testnia, Jul 23, 2014.

  1. Testnia

    Testnia

    Joined:
    Jul 4, 2014
    Posts:
    11
    I'm trying to get some objects to spawn in Unity on the a grid which I located. However while the objects do spawn on click, they are not placed on the grid. The objects spawn in a random location off the actual playing field.

    I've inserted the script used.
    Code (JavaScript):
    1. //NGU items
    2.  
    3. //var buildPanelOpen : boolean = false;
    4. //var buildPanelTweener : TweenPosition;
    5.  
    6. //Placement Plane Items
    7. var placementPlanesRoot : Transform; // attach the placement plane root
    8. var hoverMat : Material; // attach the hover material to this slot
    9. var placementLayerMask : LayerMask; // calls up the layer mask selection
    10.  
    11. private var originalMat : Material;
    12. private var lastHitObj : GameObject;
    13.  
    14. //Build Selection Items
    15. var onColor : Color;
    16. var offColor : Color;
    17. var allStructures : GameObject[];
    18. //var buildBtnGraphics : UISlicedSprite[];
    19.  
    20. private var structureIndex : int = 0;
    21.  
    22. function Start ()
    23. {
    24.     //reset the structure index, refresh the GUI
    25.     structureIndex = 0;
    26.     UpdateGUI();
    27. }
    28.  
    29. function Update ()
    30. {
    31.  
    32.         var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );
    33.         var hit : RaycastHit;
    34.  
    35.                 if ( Physics.Raycast ( ray, hit, 1000, placementLayerMask ) )
    36.                 {
    37.                     if ( lastHitObj )
    38.                     {
    39.                     lastHitObj.renderer.material = originalMat;
    40.                     }
    41.                 lastHitObj = hit.collider.gameObject;
    42.                 originalMat = lastHitObj.renderer.material;
    43.                 lastHitObj.renderer.material = hoverMat; // sets the planes material to the highlighted material
    44.                 }
    45.  
    46. else // if the raycast didn’t hit anything
    47.         {
    48.             if ( lastHitObj ) // if we had previously hit something
    49.             {
    50.             lastHitObj.renderer.material = originalMat; // visually de select that object
    51.             lastHitObj = null; // nullify the plane selection
    52.             }
    53.         }
    54.  
    55.         // drops a turrent on click
    56. if ( Input.GetMouseButtonDown ( 0 ) && lastHitObj ) //left mouse button was clicked and there is a last hitObject // if left mouse button (0) is clicked and we have something selected
    57. {
    58.     if ( lastHitObj.tag == "Placement_Open" )
    59.     {
    60. // drop the chosen structure exactly at the tile’s position and rotation of Zero. See how the “index” comes into play here?
    61.     var newStructure : GameObject = Instantiate( allStructures[structureIndex], lastHitObj.transform.position, Quaternion.identity );
    62. // set the new structure to have a random rotation. just for looks
    63.     newStructure.transform.localEulerAngles.y = ( Random.Range ( 0, 360 ) );
    64. // set this tile’s tag to “Taken” so we can’t double place a structure
    65.     lastHitObj.tag = "Placement_Taken";
    66.         }
    67.     }
    68. }
    69.  
    70.  
    71. function UpdateGUI ()
    72. {
    73. /*//Go through all structure buttons (buttons in the build panel). and set them to “off”
    74. for ( var theBtnGraphic : UISlicedSprite in buildBtnGraphics )
    75. {
    76. theBtnGraphic.color = offColor;
    77. }
    78. //set the selected build button to “on”
    79. buildBtnGraphics [ structureIndex ].color = onColor;
    80. */
    81. }
    82.  
    83.  
    84. // Called whenever a structure choice is clicked ( the button in the build panel )
    85. function SetBuildChoice ( btnObj : GameObject )
    86. {
    87. var btnName : String = btnObj.name;
    88.  
    89. if ( btnName == "Button_Turret" )
    90. {
    91. structureIndex = 0;
    92. }
    93. else if ( btnName == "Button_Mine" )
    94. {
    95. structureIndex = 1;
    96. }
    97. else if ( btnName == "Button_Sam" )
    98. {
    99. structureIndex = 2;
    100. }
    101. UpdateGUI ();
    102. }
    Would appreciate any help I can get on where I might have gone wrong.