How can I check squares around my character?

I am trying to program Turn Based Combat for my game and I wondered upon a problem. I have grid based movement in my game (something like XCOM) and I want to check all the squares that are in range for enemy to move to. How can this be done? Because I tried several things and none of them work. I feel like I am missing something really obvious.

Good day.

If you have a grid, all tiles have coords. You only need to operate with the cords.

If you are at tile (5,2), you need to check 4,2 6,2 5,1 and 5,3

soo if you are at X,Y, you need to check x-1,1 x+1,y x,y+1 x,y-1

Whats the problem?


just need the Distance formula.

//this first line goes at the very top of script
	using System.Collections.Generic;

	public Vector2[] CoordinatesWithinRadius (int x,int y, int dist) {
		List <Vector2> r = new List<Vector2> (); 
		// create a circle in a 2d grid
		int size = dist * 2;

		int xx = size;while(xx>0){xx--;
		int yy = size;while(yy>0){yy--;
				//check distance formua from center for coordinates in square distance
					r.Add(new Vector2(xx-dist+x,yy-dist+y));
		return r.ToArray();}

You could try adding multiple box colliders that are the length and rotation of each available movement. So if they could move 4 tiles in each direction, you attach 4-6 colliders (set to trigger) that are each 4 tiles long, and facing the proper direction.

If the tiles also each have a trigger collider attached to them, then it would be a simple matter of comparing the character tag in an OnTriggerStay() method, with a simple “canMove” boolean on the tiles to determine if the active character can move there.

It’s a bit rudementary, as you would have to do this for each character uniquely, but it would also work.

Assuming you don’t already have a preset ‘grid’ set up, you could use Physics.OverlapBox. Assuming that one unit equals one ‘grid’ for your player, you could use something like the following:

bool CheckSurroundings()
    int intLayerMask = 1 << 9; // Skip if you want to include everything
    Vector3 BoxSize = new Vector3(0.5f,0.5f,0.5f);
    for (int x = 0; x < 3; x++)
        for (int z = 0; z < 3; z++)
            Vector3 boxPosition = gameObject.transform.position + new Vector3(x-1, 0, z-1);
            if (boxPosition != gameObject.transform.position)
                // Physics.OverlapBox returns an array of all the colliders found in that area.
                // You can traverse the list afterwards to find out what was there.
                Collider[] coll = Physics.OverlapBox(gameObject.transform.position, BoxSize, Quaternion.identity, intLayerMask);
                if (coll.length > 0)
                    // We have a hit! We can use the boxPosition vector to find out where that hit was.
                    // You can log it, or do something with the object in that square here.
                    // Return true because we found something!
                    return true;
    // No hits, so nothing in the area
    return false;