Selecting a tile with z as y isometric tilemaps?

So I would like to select a tile with the mouse using the Isometric Z as Y tilemap. The issue I’m having is that using the WorldToCell function does not return the cell position of the tile being clicked on because it isn’t accounting for the additional y value from the z-axis. So clicking on a tile with a large z offset often gives a cell behind it rather than the cell the tile you clicked on actually belongs to.

I attempted raycasting but was unable to get the specific tile being collided with from the tilemap collider.

This only seems to be an issue with the Z as Y tilemap because tiles on the other maps always line up with their respective cells. There doesn’t seem to be any way to account for offset tiles in general actually, I’ve noticed numerous other (unanswered) questions with offset and oversized tiles not lining up with their respective cell positions.

For who Mouse Position to Isometric Z as Y tilemap is still actual :

Use Grid.WorldToCell instead of Tilemap.WorldToCell !

And dont forget set mouse world position to zero (pos.z = 0) ( or other positive value for height offset). Overwise tile be invisible.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0;
        Vector3Int currentCell = _Grid.WorldToCell(pos);
        var tile = _Tilemap.GetTile(currentCell);
        _Tilemap.SetTile(currentCell, _TileToSet);
    }
}

If you use a standard isometric z as y tilemap configration, that is, cell size of grid is (1,0.5,1) and transparency sort mode in project settings/graphics is ‘Custom Axis’ (0,1,-0.26). You can ‘pick’ a tile like this:

void Pick() {
            if (Input.GetMouseButtonDown(0)) {
                var wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    
                for (int z = 0; z < 10; z++) {
                    wp.z = z;
                    var cell = _grid.WorldToCell(wp);
                    var tile = _tileMap.GetTile(cell);
                    if (tile != null) {
                        Debug.Log(cell + " " +  tile.name);
                    }
                }
            }
        }

The API Grid.WorldToCell() consider ‘z’ component of the world point when calculate cell position. So when z is 0, it pick tiles at first floor, 1 for second floor and so on.
In my code above, I try to pick 10 floors and log all tile if any.

Ok, I used both answers here but it was not enough to solve the problem.
Considering the same “isometric z as y” tilemap config, cell size (1,0.5,1) and transparency sort (0,1,-0.26).
Consider that your z is [0, 10[

// Get mouseClick using new input system (you can also use the old version of previous answers)
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());
mousePos.z = 0; // Just for debug clarity, but not mandatory
Vector3Int gridCell = grid.WorldToCell(mousePos);
// Using tilemap instead works aswell
// Vector3Int tileCell = tilemap.WorldToCell(mousePos);

Debug.Log("mousePos : " + mousePos + " cellPos_0  : " + gridCell);

// You can use that loop to get all tiles of current pile of tiles,
// starting from the grid cell (with z = 0) that you clicked.
for (int z = 10; z > -1; z--){
    gridCell.z = z;
    var tile = tilemap.GetTile(gridCell);
    if (tile != null) {
        Debug.Log("grid" + gridCell + " found " +  tile.name);
        // break; // If you need only the top most tile of that pile
    }
}

// You can use this loop to check all the tiles that could be 
// hiding the grid cell with (z = 0) that you clicked.
// We start from the front most tile which with an elevation of 9
// and would correspond to a 2.25 below current y
// and then we decrease z as we increase y by 0.25f (half cell height)
mousePos.y -= 2.5f; // to be adapted depending on your max Z and the start of the loop below
for (int z = 10; z > -1; z--){
    gridCell = grid.WorldToCell(mousePos);
    gridCell.z = z;
    var tile = tilemap.GetTile(gridCell);
    if (tile != null) {
        Debug.Log("grid Z" + gridCell + " found " +  tile.name);
        // break; // If you need only the first tile encountered
    }
    mousePos.y += 0.25f;
}

The idea is that since your cell height is 0.5 , and due to the layout, you should check with z+1 and y-0.25 each time, to see if you find any tile that would be hiding current grid cell. I used the reverse order to be able to stop as soon as we find the front cell.