I have set a tilemap whose tile anchor is (1, 0, 0) unlike the default value (0.5, 0.5, 0) to achieve the look I desire. Although I have an issue with it. Using the WorldToCell() method of Grid class which I think doesn’t really take the tilemap’s anchor into account, will not give me the actual cell position in integers since the tiles may end up occupying multiple cells due to the anchor.
I tried to check around tiles as well if the cell is not found at first attempt like in the code below:
var cellPosition = tilemap.WorldToCell(tilemap.LocalToCellInterpolated(innerPoint));
var sprite = tilemap.GetSprite(cellPosition);
if (sprite == null) {
// Scan the tiles around the tile, this is needed when the tilemap's anchor or offset is different than default
for (var x = -1; x <= 1; x++) {
for (var y = -1; y <= 1; y++) {
sprite = tilemap.GetSprite(cellPosition + new Vector3Int(x, y, 0));
if (sprite != null)
break;
}
}
}
This approach somehow does not work from time to time and in my opinion, has performance cost since this might be executed many times in a row during gameplay.
Do you guys have a way to deal with finding cell positions via world positions when the tilemap has different anchor points? Thanks.