I made a sample project of hexagonal map.
Whenever user click on cell, robot will move to the cell position.
public Tilemap _tilemap;
public GameObject _player;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int coordinate = _tilemap.WorldToCell(mousePos);
Debug.Log(string.Format("coordinate pos is [X: {0} Y: {0} Z: {0}]", coordinate.x, coordinate.y, coordinate.z));
Vector3 selectCellPos = _tilemap.CellToWorld(coordinate);
Debug.Log(string.Format("cell world pos is [X: {0} Y: {0} Z: {0}]", selectCellPos.x, selectCellPos.y, selectCellPos.z));
selectCellPos.z = 0;
_player.transform.position = selectCellPos;
}
}
It works fine, but when I checked Log something confused me.
When I click center cell, tilemap.WorldToCell return (0, 0, 0).
When I click cell at top right of center , tilemap.WorldToCell still return (0, 0, 0).
However, tilemap.CellToWorld return different value when both cell input (0, 0, 0).
Why?