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:
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?