Need help with grid-based movement on isometric z as y tilemap

Pretty new to Unity here and trying to make a tactics RPG like Final Fantasy Tactics. I’ve been working with 2D isometric Z as Y Tilemaps in order to build my map, but i’m having issues with being able to do grid based movement with these tiles. Here’s a gif of the issue:

6091086--661599--GridMovementIssue.gif

Since the second level of the map has a higher Z-index, the cursor isn’t properly respecting that change and it overlaps the side instead. It works perfect on the base layer where z = 0. I want a way to offset the cursor based on the current Tile’s z-index, but I can’t figure out how to get that information in code.

The code for my cursor looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class Cursor : MonoBehaviour
{
    [SerializeField]
    private Tilemap _tilemap;

    private Vector3Int _currentCell;

    // Start is called before the first frame update
    void Start()
    {
        _currentCell = Vector3Int.zero;
        // start at 0
        transform.position = _tilemap.CellToLocal(_currentCell);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            UpdateCurrentCell(Vector3Int.up);
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            UpdateCurrentCell(Vector3Int.left);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            UpdateCurrentCell(Vector3Int.down);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            UpdateCurrentCell(Vector3Int.right);
        }
    }

    void UpdateCurrentCell(Vector3Int offset)
    {
        _currentCell += offset;
        transform.position = _tilemap.CellToLocal(_currentCell);
    }
}

I thought I might be able to access the z value of the current tile in the Tilemap, but it seems that data just doesn’t exist anywhere. Anyone know how I can achieve what I want?

1 Like

I figured this out, though it required me to manually create all of my board information and to render the tiles manually as well. Here’s what the UpdateCurrentCell method looks like with these updates:

void UpdateCurrentCell(Vector3Int offset)
{
    Vector3Int newPosition = _currentPosition + offset;
      
    if (!_board.ContainsPoint(newPosition.x, newPosition.y))
    {
        return;
    }

    _currentPosition += offset;

    CustomTileData tileData = _board.tileData[_currentPosition.x, _currentPosition.y];
    // tiles seem to render 1 x and 1 y position off from what is expected
    // this may be due to me using tall block tiles instead of flat tiles
    Vector3Int offsetNeededForRenderingOnMap = new Vector3Int(1, 1, tileData.position.z);
    transform.position = _tilemap.CellToLocal(_currentPosition + offsetNeededForRenderingOnMap);
}
1 Like

My solution

Project: GitHub - Solfylte/Grid-Placement-system: Unity. Grid placement system and simple flat map generator