# How to get area of grids?

Firstly,I don't know if my question title is true but I couldn't find better title, sorry If I titled wrong.

I'm trying to grid base game. The player is moving grid to grid and never stop between grids even if ıt takes command to move another direction between grids.

So, I want to get area of grids where the character walks on the edge of area.

To clarify myself

Player starts to move and tries to reach edge following brown lines. When player reaches any edge, the all tiles should be colored.

• Like Qix? Jun 23 '21 at 23:51
• What have you tried so far? Why did it not work? Jun 23 '21 at 23:52
• I couldn't do anything so far. I'm trying to create an algorithm but really stucked. I think maybe someone here can show a roadmap or give start point Jun 24 '21 at 5:29

First let me rephrase your question. You are basically asking "how to know my current tile?".

To do that you write Position2Tile function which takes objects position and returns tile coordinates which object is in right now. Then if your object is moving, - in Update() function you just call this function and get tile coordinates from it.

It should look something like this:

int[] Position2Tile(Vector3 pos)
{
int[] tile;

Debug.Log("yeay! current tile is: " + tile[0] + ", " + tile[1]);
return tile;
}


Regarding the logic itself. Let's say grid starts at position 0,0. And let's say that tile size is 1 meter. So if for example players position is (2.73 , 4.86), not too hard to notice that tile coordinates would be [3 , 5]. In such case logic would be like this:

  int xcord = Mathf.CeilToInt(pos.x);
int ycord = Mathf.CeilToInt(pos.y);
tile = new int[] { xcord, ycord };


In case tile size is not 1 meter but some other size, (for example 2.58 meters), divide your position by tile size:

pos.x/tileSize  // do this before converting to integer


Once you know current tile you know where you are all times. For example if tile[0] == 1 then you have reached the left edge.

So in your specific case if you know starting tile (and finishing tile) all tiles to the left, and below will need to be colored.

• Thanks for answer. But I'm not sure If I can use this logic for all tiles in map Jun 24 '21 at 15:57
• why not, why other tiles are different? Jun 24 '21 at 16:11