0
\$\begingroup\$

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: enter image description here

This is pretty disturbing to me and it will be to most players (not programmers) too I guess.

I would like something like this: enter image description here

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

I tried this:

Vector3 currentWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int currentCellPos = _grid.WorldToCell(currentWorldPos); // This gives the coordinates as in the Unity Editor except for z
currentCellPos.z = 0; // Set z to 0 so the coordinates of the cell are exactly as in the Unity Editor
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?

\$\endgroup\$
  • \$\begingroup\$ Your proposed coordinate system is inconsistent. The euclidian coordinate distance between two vertical adjacent tiles is 2, while the distance between two horicontal adjacent tiles is only 1. The manhatten distance also does not hold up in your coordinate system. Think carefully if you really need to change the coordinate system. Do you need to show players the coordinates? \$\endgroup\$ – LukeG Mar 9 at 17:21
1
\$\begingroup\$

The coordinate system you've chosen is not a Cartesian plane. The axis x=0 forms a zig-zag instead of a straight vertical line, because you've shifted the start point of alternating rows.

Vector2Int ToJaggedRow(Vector3Int isometric) {
    int row = isometric.x + isometric.y;
    int column = isometric.x - isometric.y;

    // Shift alternating rows by one.
    if((Mathf.Abs(row) & 1) == 1)
        column++;

    // All column numbers are now even numbers.
    // Divide by 2 to get them into consecutive integers.
    column /= 2;

    return new Vector2Int(column, row);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.