- Home /
Get position of the centre of each grid tile
I'm splitting a Plane into a grid using the following code:
public int numRows = 10;
public int numColumns = 10;
//get the level object
GameObject theLevel = GameObject.FindWithTag("Level");
//get the bounds
float levelWidth = theLevel.renderer.bounds.size.x;
float levelDepth = theLevel.renderer.bounds.size.z;
float realTileWidth = levelWidth / numRows;
float realTileHeight = levelDepth / numColumns;
Now I know the size of each tile, I can go through and add them all to my tile list. My problem now is trying to figure our the World position of the centre of each tile. What is the best way of doing this? I know I can get the centre of the Plane using:
theLevel.renderer.bounds.centre
But after that I'm at a loss of how to use that to figure out the centre of every grid tile?
Thanks in advance!
Answer by skovacs1 · Oct 28, 2010 at 05:41 PM
Assuming you know the exact size of your tiles, which assumes you know the size of your space space (note that bounds are axis-aligned and do not rotate with your object), you could do something like adding the column realTileWidth and row realTileHeight to the center of a known tile position on your space, adjusted by which space that is.
If you are using the center of a center tile(assumes an odd number of tiles), it would look something like:
//somewhere... Vector2 offset = new Vector2(numColumns / 2.0f - 1.0f, numRows / 2.0f - 1.0f);
//row and col are the row and column of the tile in question from 0-9 tilePosition.x = center.x + (col - offset.x) realTileWidth; tilePosition.z = center.z + (row - offset.y) realTileHeight
If you were using the bottom-left corner as a point of reference (make no assumptions about the number of tiles):
//somewhere... Vector2 offset = new Vector2(realTileWidth / 2.0f; realTileHeight / 2.0);
//row and col are the row and column of the tile in question from 0-9 tilePosition.x = col realTileWidth + offset.x; tilePosition.z = row realTileHeight + offset.y;
As for the height (tilePosition.y), that depends on your tiles.
Brilliant, that's exactly what I needed. Thanks for taking the time to answer this.
Your answer
Follow this Question
Related Questions
Question about finding out edges of a box collider 0 Answers
How to Place and arrange 3D cards using Grid and Bounds 0 Answers
Are there any world bounds in Android? 0 Answers
Asset that can achieve a sonic game 4 Answers
Fastest way to draw a grid of textures 2 Answers