Search Unity

[Tilemap] Isometric coords to cartesian coords

Discussion in '2D' started by GuiTeK, Mar 2, 2019.

  1. GuiTeK

    GuiTeK

    Joined:
    Apr 28, 2017
    Posts:
    12
    Hi,

    I'm new to the tilemap feature of Unity, and I'm having a hard time with all those coords.

    I created a new project, added an isometric tilemap and left all values to their default. In the Unity Editor (and in-game at runtime as well), the coordinates of the cells are like this:
    coords_not_cartesian.png
    This is pretty disturbing to me and it will be to most players (not programmers) too I guess.

    I would like something like this:
    cartesian_coords.png

    However, I can't figure out how to do the transformation.

    I tried this:
    Code (CSharp):
    1. Vector3 currentWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. Vector3Int currentCellPos = _grid.WorldToCell(currentWorldPos); // This gives the coordinates as in the Unity Editor except for z
    3. currentCellPos.z = 0; // Set z to 0 so the coordinates of the cell are exactly as in the Unity Editor
    4. Vector3 cartesianCellPos = new Vector3((2 * currentCellPos.y + currentCellPos.x) / 2, (2 * currentCellPos.y - currentCellPos.x) / 2, 0); // Try to transform the Unity coordinates into cartesian coordinates: WRONG RESULT
    But this calculation gives weird results, not what I expect at all.

    Do you know how I could transform the Unity coordinates shown in the first screenshot into the coordinates shown in the second screenshot?

    Thank you very much!