I’m working on my tilemap-based game and I need to get positions of the tiles in a tilemap. I set up like this. The grid’s cell size is 0.16 since the sprites of each tile is 16x16 px:
In TilemapController2, I loop through tilemap’s size and use CellToWorld to get position, but it doesn’t seems to be correct. As in the screenshot, the center of the tilemap is on the left of the point (0, 0), but the iterator gives me position of tile (0, 0) is exactly (0, 0), which is incorrect because the bottom-left tile of the tilemap is far from point (0, 0). This is the content of TilemapController2:
public class TilemapController2 : MonoBehaviour {
public Tilemap Tilemap;
// Use this for initialization
void Start () {
Vector3 tilePosition;
Vector3Int coordinate = new Vector3Int(0, 0, 0);
for (int i = 0; i < Tilemap.size.x; i++) {
for (int j = 0; j < Tilemap.size.y; j++) {
coordinate.x = i; coordinate.y = j;
tilePosition = Tilemap.CellToWorld(coordinate);
Debug.Log(string.Format("Position of tile [{0}, {1}] = ({2}, {3})", coordinate.x, coordinate.y, tilePosition.x, tilePosition.y));
}
}
}
}
I’ve tried several other methods, but none of them give me the correct world position of the tile.