Home > database >  How to detect when a specific entity is touching a tile and what tag that tile has?
How to detect when a specific entity is touching a tile and what tag that tile has?

Time:10-19

I have a simple game which looks like this:

The image of my what my game looks like

I want to detect whether the yellow square is touching the orange tiles or the red tiles. How may I do this?

CodePudding user response:

If the tiles should be walkthrough then set the collider mode to trigger and this line of code.

void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.tag == "Whatever the name of your tag")
    {
        whatever it is you wanna do.
    }
}

if its not walkthrough then:

void OnCollisionEnter2D(Collision2D col)
{
    if (col.gameObject.tag == "Whatever the name of your tag")
    {
        whatever it is you wanna do.
    }
}

CodePudding user response:

I got it working all I had to do was add a rigid body 2d to my square sprite and of course add the code Aseed Kid mentioned earlier.

  • Related