I am working on a city builder, but I am having trouble figuring out how to let the user place the objects in a grid format. So far I have the building directly following the mouse position with raycasting, but what I need is for the building to move from grid cell to cell based on the mouse position. If anyone can think of any code/help for this that would be amazing! Thanks :)
Also, if anyone has an idea for the best event to script an upgrade to a building that is placed based on there being certain other buildings within a certain area (collider area probably).
It helps if you post the code you already have. Something like this should work:
var gridSize : float = 1.0;
function Update() {
//Your code to set the position based on the mouse position goes here
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.z / gridSize) * gridSize;
}
Of course, if you're only moving the building in two dimensions, you'll only need two of those. Note that this just snaps the position of the GameObject to a grid, you'll still have to draw the grid separately if you want it to be visible.