Clicking On Tiles With An Isometric Z As Y Tilemap?

I can’t seem to register what tile is being clicked on with a z as y tilemap. Using WorldToCell and GetTile gives me the tile located at the cell on the grid but if the tile you’re clicking on has a significant z value it will appear over other cells and you end up registering the tiles behind the one you’re actually clicking on.

Is there any easy way around this? It seems like a relatively simple task.

edit: Gave a more thorough description a few posts down.

Any ideas? I’m really struggling to figure out a solution. I can try to explain it better if needed.

not sure what your trying to say. Show pictures?

1 Like

Ah, sure thing, I’ll post some when I get a chance.

So all the tiles are on the grid shown here. Some are offset using a “z” value on the grid. With the “z as y” type of grid the “z” value on the tile is translated to the y-axis in world space instead because this is a completely 2D image despite appearing 3D.

So the WorldToCell function takes a position in world space and gives you the cell on the grid that position is over. So it’ll give me the cell my mouse is hovering over. The problem here is that this artificial z value adjusting the y of the tile OFF of the grid. So if I click on the now adjusted tile the WorldToCell doesn’t take into account this y movement and will give me the position on the grid where I clicked, not the position the tile actually belongs to, which is what I need to know which tile was actually clicked.

Does that help?

I somewhat understand your problem. Maybe you can offset the click a bit before putting it into worldtocell?

How do I know how much to offset it by and in which direction if I can’t tell what is being clicked on?

I tried some raycasting as well and it just gives me the collider for the whole tilemap and the point of collision not the specific tile that was collided with, seemingly. So that puts me back to not being able to get the correct tile based solely on a point in space.

I’ve been researching this and I can’t find an answer that works for this kind of tilemap, they all seem to rely on a combination of WorldToCell and GetTile. Any way I can ask the tilemap devs what they had in mind for this kind of thing? It seems impossible.

Oh yeah… I got the same problem) And you know, it’s real problem. Because as I understand tile it just an object for rendering, and that all. For example, you can’t even get some uniq instance of tile in tilemap, because all tiles it just scriptable objects, so it just settings for rendering. When you call GetTile from tilemap, you got ScriptableObject, that will be the same for all tiles with this type.

So, to solve this problem I made some complex solution. Hope you will understand it))

I’ve created MonoBehaviour, which finds in TileMap all Positions of tiles, that I need, than creates gameobjects for each tile and adds PolygonCollider to it. And for this PolygonCollider we take points from sprite of tile. And put this gameobjects to TileMap object and give them positions from GetCellCenterLocal method So, you have many gameobjects with colliders for each tile. But now you need to handle click on top collider if they crossed. And for this not enough to use default IPointerClickHandler, because this interface sorts colliders by Z as I understand and I don’t know how to change it to sort by Y (if somebody will find it, I will glad to see solution). So, to solve it, my MonoBehaviour in Update method finds all Raycast hits and give you click for object, that has highest Z or if equal, then lowest Y.

So, this is like general idea) Hope it will be helpful and maybe someone have some easiest solution or will be great to start discussion how to find this easiest solution.

I don’t doubt that works but that gives a similar problem to just using gameobjects instead of tiles. You’re drastically increasing the overhead which kind of defeats the point of using tiles in the first place. There has to be a better way.

I dont really have a better solution and if you cant find anything else perhaps you can try to code in some objects that are over the tilemap. (like clickboxes or whatever) that the player clicks and each of them correspond to a certain tile or a certain area of tiles.

I don’t say that you need to use gameobjects instead of tiles. In this case gameobjects will be just only contains collider and that all. Yeah, it’s absolutely not an ideal solution, but I don’t think, that this solution make really big overhead and defeats the point of using tiles in the first place. It’s just a variant of “tile clicking handler” and no more. But if you find better solution, please share) I will be grateful.

Yeah I might end up doing something like that but considering how basic this is (just click a tile) I was expecting there to be some kind of unity solution. Like did they not anticipate this issue when they designed the z as y tilemap? Or are they working on it? Am I using it wrong? I’d really love to hear from a dev on this, it’s super confusing that something like this wouldn’t have a easy solution.

Each new height layer assumes that you will use a new layer of tilemaps(or sublayer). You should not have any offsets, or if you have them, they should be equal for their layer.

If that’s the case why is there a built-in height system? Tutorials even show using the height offset like that. New tilemap might work as a workaround but I don’t think that’s the intended use case.

I’ve figured out a solution to this, a bit late I know. You need to know the bounds of your isometric map, and for simplicity your tiles should all be above the z = 0 offset. Now, when you click, the world to cell function gives you a tile at the base of map with a z of 0. Create a traversal vector to check all the tile positions that could be in front of such a position;

Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector3Int gridPos = terrain.WorldToCell(mousePos);
            Vector3 travPos = gridPos;
            Vector3Int checkPos = gridPos;
            while (travPos.z < DataManager.instance.GetMapSize().z)
            {
                travPos += new Vector3(-0.5f, -0.5f, 1);
                checkPos = new Vector3Int(Mathf.FloorToInt(travPos.x), Mathf.FloorToInt(travPos.y), Mathf.FloorToInt(travPos.z));
                if(terrain.HasTile(checkPos))
                {
                    gridPos = checkPos;
                }
            }

The key is traversing by half a tile in the x and y direction for every z increment, and rounding to an integer value in order to check the tilemap for a tile.

There’s still a slight problem if you click on a tile (x,y,z) only partially occluded by another tile in a position (x+1, y+1, z+1) from it. I am going to see if I can figure something out for that.

dchapel this is very good. Can I ask what is DataManager?

just points to the current maximum z height of the map

Since I made this post a while back I actually found a seemingly better solution, as the solution I posted above was pretty wonky in a lot of cases. Specifically if your map isn’t just like a sort of deformed mesh with only 1 tile at the top. It’s detailed here, in the top answer, but the other answers also detail some good solutions (including one like mine):

And DataManager was just a place where I was keeping track of the maximum z value in the map. I never ended up doing an isometric game because of this. If I give it a go again I’ll probably make it with a 3d terrain and just billboard sprites on it or something.

This post is quite old already, but I struggeled with the same issue. In my case I wanted to change a tile once a character walked on it, but had to take the different height of the tiles into consideration. I solved this by creating a tilemap for each height level or z-value. Then I iterated over my tilemaps and adjusted the z value in each iteration. Afterwards I returned the correct z-value. This is what I came up with:

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

public class Whatever : MonoBehaviour
{
    public List<Tilemap> levelTilemapsAscending = new List<Tilemap>();
    public Tilemap groundTilemap;
    private Vector3 characterPosition;

        characterPosition = transform.position;
        characterPosition.z = 0;
        Vector3Int currentCell = groundTilemap.WorldToCell(characterPosition);
        currentCell.z = calculateCorrectZ(currentCell);

    private int calculateCorrectZ(Vector3Int cellToCheck)
    {
        int z = 0;
        for (int i = 0; i < levelTilemapsAscending.Count; i++)
        {
            cellToCheck.z = i;
            if (levelTilemapsAscending[i].HasTile(cellToCheck))
            {
                z = i;
            }
        }

        return z;
    }

}

With this I can determine the tilemap, that the highest tile is in and its position. The list of tilemaps is ordered by the z-value used on each tilemap. So levelTilemapsAscending[0] holds the tilemap where tiles are always painted with z=0 and so on.

OP is using a single tilemap