Grid Placement In Game

So i’m looking for some input to see if i’m going about this the most efficient way. I created some code that would spawn a grid of objects, I tried this with cubes,planes and emptys each with the renderer.enabled = false. I am trying to create a grid for building placement.

The intial attempt I set the size to 5x5 and spawned them to fill the entire terrain which was 512x512. It was roughly 1000 objects I left a slight margin around the edge of the terrain. There was a slight delay on startup around 5 seconds. I thought of creating a prefab with the grid rather than instantiating it, but unity would just freeze up any time I tried to move that many objects into a prefab.

Second attempt I created a larger grid with empty objects with a box collider attached, this was pritty much instant and I added that to a prefab and left it in the scene. Then created a smaller grid of 0.5 x 0.5 squares with a 10x10 grid that I would instanciate if a certain boolean was true and the players mouse was in that section of the grid and then destroy it. The smaller grid had a renderer on it but would only enabled if the player was placing a building over that tile.

So my questions …

Is there a better way todo it ?

Should I just instantiate everything and add some sort of loading screen?

I had some strange results with changing the size of the box collider and the scale. Should I just change the scale and leave the collider at the default ?

Is there an easy way to make the grid follow the height of the terrain, maybe apply something like a height map to it ?

The function that called the instantiate on the grid would happen if the raycast hit a tag which was the “grid”, the smaller grid would rather than appear in the single section would just spawn a grid in each section. Is this todo with the empty I’m using to hold them ?

A real pain I had is going through and reseting the script so I could actualy chose the object I wanted to instantiate or forgot to set the correct tag on the object. Is there a quick way of reseting/setting multiple scripts/tags on objects ?

Hope this is as clear to everyone else as I think it is :stuck_out_tongue:


I think you’re going about this the wrong way.

You know how big your map is. Just divide that number with how big you want your grids spaces to be. Then move your buildings in increments of that number. => Grid.

To get a visual representation, do you really need to see the grid for the entire map at the same time? You could just make a small texture that displays the grid near your mouse, and move that in increments, just like the buildings. If you do want the entire map enclosed in a visual grid, you could just reuse your first method, but doing it in a coroutine. That way it will not stall the rest of the program.

Hi I use the belowcode solution to get a grid position based on a vector.

public static float gridSpacing = 1f; (this is a 1unit x 1unit size grid)

//Pass your mouse position to the below function and it will return the nearest grid co-ord to you.

public static Vector3 GetGridPosition (Vector3 convertPosition)

float newx = Mathf.Round (convertPosition.x / gridSpacing) * gridSpacing;

float newz = Mathf.Round (convertPosition.z / gridSpacing) * gridSpacing;

convertPosition = new Vector3 (newx, (convertPosition.y), newz);

return convertPosition;