I have a tilemap that is filled with tiles by script. That works well so far. Now my aim is to make the tiles interactable by making them clickable.
I achieved this by adding an EventSystem object under the tilemap, adding a Physics 2D Raycaster component to the camera and then impementing IPointerDownHandler.OnPointerDown(..) in my script.
But here is the problem: I need to get the tile at the position that is clicked. As far as I read, this should easily be possible by calling the tilemaps WorldToCell function and then using the cell position to get the tile from the tilemap. However WorldToCell does only return me the floored world position instead of the cell position inside the tilemap.
My OnPointerDown function looks like this:
void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
{
Debug.Log("Clicked at " + eventData.position.x + ", " + eventData.position.y);
Tilemap tilemap = gameObject.GetComponent<Tilemap>();
Vector3Int cellPos = tilemap.WorldToCell(new Vector3(eventData.position.x, eventData.position.y, 0));
Debug.Log("Clicked at " + cellPos.x + ", " + cellPos.y);
}
If I click on a tile then then output on console looks like this:
Clicked at 1379,554, 275,0186
Clicked at 1379, 275
Can someone tell me what I got wrong or how WorldToCell actually works? I searched far and wide but none seems to have the same problem.