2D topdown Tilebased dedect Layer

Hello everyone,

i need some help. I make a 2d topdown tilebased game. Every Tile is 32x32 (Like you can see in Picture).
no o got diferent layers like: Ground, Walls, Roof.

Now i need to dedect when a player is under a Roof tile to make this layer Invisible (when player is in a building)

right now i got on every Rooftile object a collider and a script attached to make Roof invisible like following script:

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

public class RoofVisibility : MonoBehaviour
{

    void Update()
    {
        if (ConstantFile.RoofVisibility == false)
        {
            GetComponent<SpriteRenderer>().enabled = false;
        }
        else
        {
            GetComponent<SpriteRenderer>().enabled = true;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            ConstantFile.RoofVisibility = false;
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            ConstantFile.RoofVisibility = true;
        }
    }
}

the problem is, everyitme when the player moves from tile to another tile it will enable and disable the Roof.
has anyone a better solution for this?

Any ideas? :wink:

Hmm noone and idea whats the solution for that?

Are you using a tilemap collider?

No i only can use the tilemap collider without triggers.i attached seperate 2d collider to the tile prefab

If you have a separate collider for each tile, ot looks like you can use this:

and that will make each of the little colliders act like one big collider.

Wow thank you very much, i will test it today :sunglasses: