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.
//NGU items
//var buildPanelOpen : boolean = false;
//var buildPanelTweener : TweenPosition;
//Placement Plane Items
var placementPlanesRoot : Transform; // attach the placement plane root
var hoverMat : Material; // attach the hover material to this slot
var placementLayerMask : LayerMask; // calls up the layer mask selection
private var originalMat : Material;
private var lastHitObj : GameObject;
//Build Selection Items
var onColor : Color;
var offColor : Color;
var allStructures : GameObject[];
//var buildBtnGraphics : UISlicedSprite[];
private var structureIndex : int = 0;
function Start ()
{
//reset the structure index, refresh the GUI
structureIndex = 0;
UpdateGUI();
}
function Update ()
{
var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );
var hit : RaycastHit;
if ( Physics.Raycast ( ray, hit, 1000, placementLayerMask ) )
{
if ( lastHitObj )
{
lastHitObj.renderer.material = originalMat;
}
lastHitObj = hit.collider.gameObject;
originalMat = lastHitObj.renderer.material;
lastHitObj.renderer.material = hoverMat; // sets the planes material to the highlighted material
}
else // if the raycast didn’t hit anything
{
if ( lastHitObj ) // if we had previously hit something
{
lastHitObj.renderer.material = originalMat; // visually de select that object
lastHitObj = null; // nullify the plane selection
}
}
// drops a turrent on click
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
{
if ( lastHitObj.tag == "Placement_Open" )
{
// drop the chosen structure exactly at the tile’s position and rotation of Zero. See how the “index” comes into play here?
var newStructure : GameObject = Instantiate( allStructures[structureIndex], lastHitObj.transform.position, Quaternion.identity );
// set the new structure to have a random rotation. just for looks
newStructure.transform.localEulerAngles.y = ( Random.Range ( 0, 360 ) );
// set this tile’s tag to “Taken” so we can’t double place a structure
lastHitObj.tag = "Placement_Taken";
}
}
}
function UpdateGUI ()
{
/*//Go through all structure buttons (buttons in the build panel). and set them to “off”
for ( var theBtnGraphic : UISlicedSprite in buildBtnGraphics )
{
theBtnGraphic.color = offColor;
}
//set the selected build button to “on”
buildBtnGraphics [ structureIndex ].color = onColor;
*/
}
// Called whenever a structure choice is clicked ( the button in the build panel )
function SetBuildChoice ( btnObj : GameObject )
{
var btnName : String = btnObj.name;
if ( btnName == "Button_Turret" )
{
structureIndex = 0;
}
else if ( btnName == "Button_Mine" )
{
structureIndex = 1;
}
else if ( btnName == "Button_Sam" )
{
structureIndex = 2;
}
UpdateGUI ();
}
Would appreciate any help I can get on where I might have gone wrong.
Vector3.zero
. The turret should be placed at the origin of your grid. – Aldour Cheng Jul 23 at 3:58