Transparent trees in Tilemap

Hello everyone!

I want my trees to be transparent when the player walk behind it.

The tilemap layer for the trees have a TilemapCollider2D (trigger) and this is the code attached to the player :

private Vector3Int pos;
Color alpha = new Color (1, 1, 1, 0.5f);

    void OnTriggerStay2D(Collider2D c){
        if(c.CompareTag("Trees")){
            pos = map.WorldToCell (transform.position);
            map.SetTileFlags (pos, TileFlags.None);
            map.SetColor (pos, alpha);
            map.RefreshTile (pos);
        }

    }

The probleme is the tile don’t change the color nor the alpha.

To test if it the collider and the position of the player work properly I’ve tested with :

void OnTriggerStay2D(Collider2D c){
        if(c.CompareTag("Trees")){
            pos = map.WorldToCell (transform.position);
            map.SetTile (pos, null);
        }

    }

And it’s work well, the tile is erased when the player walk through.

I have also tested to change the material color of the tilemap, but I want a specific tree in the tilemap to be transparent.

renderer = c.GetComponent<Renderer>();
Color rc = renderer.material.color;
rc.a = 0;
renderer.material.color = rc;

I know I can use a tree gameObject with a script and instantiate it in the scene, but I want to use the tilemap and I want to know why the “map.SetColor (pos, alpha);” doesn’t work.

Thanks in advance.

I had a similar issue, though I solved via unlocking the color like this

map.RemoveTileFlags(pos, TileFlags.LockColor);

I see that you’re already doing that, so I don’t know what the problem might be.
Leaving this here in case somebody stumbles upon the same issue that I did.

1 Like