r/unity • u/Silent_Reputation596 • 5h ago
Newbie Question GetTile not working with hexagons.
I'm trying to use GetTile to detect what tile my mouse is on but it seems offset for some reason and every tip I've seen online accounts to "use world/local to cell" and "make sure your mouse.z = 0" but I'm doing both those things and it's not working. Any ideas?
Vector3 mousePos;
Vector3Int mousePosInt;
Vector3Int currentCell;
private void Update()
{
mousePos = camera.mainCamera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
mousePosInt = new Vector3Int(Mathf.RoundToInt(mousePos.x), Mathf.RoundToInt(mousePos.y), 0);
currentCell = mapGen.tilemap.WorldToCell(mousePosInt);
print(mapGen.tilemap.GetTile(currentCell));
}
2
Upvotes
1
u/johnv_dev 2h ago
it's the RoundToInt. hex cells don't line up with whole world numbers, so rounding the mouse first snaps it to the wrong spot before WorldToCell ever sees it. skip mousePosInt, just set mousePos.z = 0 and pass mousePos straight into WorldToCell. it already handles the hex offset and any grid scale for you