Hello, I am trying to spawn an object on left mouse click, and this object has to constrained to a grid. So far, it spawns in a grid, but they only spawn in a diagonal line. I tracked this down the the Mathf.Round function, because every object spawned has the same x and z co-ordinates, (5,5 / 4,4 / 3,3) and thats whats causing it to spawn in a diagonal line.
This code goes on my camera:
var gridSize : float = 1.0;
var prefab : GameObject;
var clone : Transform;
function Update () {
if (Input.GetMouseButtonDown(0)) {
// Construct a ray from the current mouse coordinates
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
// Create a particle if hit
clone = Instantiate (prefab, hit.point, transform.rotation).transform;
}
}
}
This code goes on the spawnable object:
#pragma strict
var gridSize : float = 1;
function Start () {
transform.position.x = Mathf.Round(transform.position.x / gridSize) * gridSize;
transform.position.y = Mathf.Round(transform.position.y / gridSize) * gridSize;
transform.position.z = Mathf.Round(transform.position.x / gridSize) * gridSize;
}
Help?