I'm working on an isometric game with Unity and I need to know the centre of the cell in the world coordinates. I'm using the tilemap's GetCellCenterWorld()
method, but it gives me back bad results.
Here is what I expect to be the centre of the cell (basically the logical centre), but here is what Unity gives back (the top vertex of the cell?). I believe it has something to do with the sprite's pivot point (now set to its top centre) or the cell anchor in the tilemap's properties (I set it to (1,1)).
Here's the code I use to "select" the tiles:
public class GridSelection : MonoBehaviour
{
public Tilemap tilemap;
public Transform selector;
void Update()
{
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPos.z = 0;
Vector3Int tilemapPos = tilemap.WorldToCell(worldPos);
selector.position = tilemap.GetCellCenterWorld(tilemapPos);
}
}
How do I get the actual middle point of the cell?
Okay, I think I found what the issue was.
If anyone has the same problem, here's how I resolved it:
GetCellCenterWorld()
method normallyThe code I've posted in the question works just fine now.
[SerializeField] private Tilemap map;
private Camera camera;
[SerializeField] private Transform playerTransform;
void Start(){
camera = Camera.main;
}
// Update is called once per frame
void Update(){
if (Input.GetMouseButtonDown(0)){
var worldPos = camera.ScreenToWorldPoint(Input.mousePosition);
Vector3Int tilemapPos = map.WorldToCell(worldPos);
playerTransform.position = map.CellToWorld(tilemapPos);
Debug.Log(tilemapPos);
}
}
playerTransform.position = map.CellToWorld(clickedCellPosition);
Just convert it back and provide tilemapPos
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With