Hexagonal Tilemap Mouse over bug

,

Hi,

I have encountered a bug I don’t understand.
I’m trying to highlight a tile when the mouse is over it on a hexagonal grid.

Here is the script i’m using :

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

public class GridController : MonoBehaviour
{
    private Grid grid;
    [SerializeField] private Tilemap interactiveMap = null;
    [SerializeField] private Tile hoverTile = null;


    private Vector3Int previousMousePos = new Vector3Int();

    // Start is called before the first frame update
    void Start()
    {
        grid = gameObject.GetComponent<Grid>();
    }

    // Update is called once per frame
    void Update()
    {
        // Mouse over -> highlight tile
        Vector3Int mousePos = GetMousePosition();
        if (mousePos != previousMousePos)
        {
            interactiveMap.SetTile(previousMousePos, null); // Remove old hoverTile
            interactiveMap.SetTile(mousePos, hoverTile);
            previousMousePos = mousePos;
        }
    }

    Vector3Int GetMousePosition()
    {
        Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        return grid.WorldToCell(mouseWorldPos);
    }
}

I tried this with a regular tilemap and it works. So I really don’t understand why it doesn’t work with a Hexagonal Tilemap.
Anyone know where this comes from ?

Hi, would it be possible to share what you are seeing and what you are expecting instead? This would help to identify the issue. Thanks!

Can’t really see enough for your code to diagnose the issue here.

Tarodev has a very cool system of highlighting hexes, that I use myself.

I fixed it, it was setting the tile to the mouse position but with the Z set the same as the camera so it was not visible. I corrected it by getting only the x and y from the mouse position and setting the z coordinate to 0.