Trigger area around player.

Hello :slight_smile:

I’m working on my first project (Top-Down 2D game) and I have a question since I looked everywhere and couldn’t found an exact similar topic. I will simplify the question with an example as there is no reason for unnecessary details.

There are (let’s say 3) stationary Game Objects with specific names (Obj1, Obj2, Obj3) that I want specific events (event1, event2, event3) to happen when my player is NEAR them. (Not necessary collided with them)

I tried 2 ways but none of them triggered the events:

1st way) I added a 2nd Box Collider 2D (isTrigger) to my player and size bigger than my actual player.

2nd way) Added ‘isTrigger’ box colliders to the 3 objects. (bigger than their actual size)

Added the script to my player on both ways:

void OnTriggerEnter2D(Collision2D Collision)

{

    if (Collision.collider.name == "Obj1")
    {
        (do event1);
    }
 }

(etc. other 2 objects)

Then I tried again the aboved 2 ways by adding child to my player with attached boxcollider2D (also for the objects) but no combination is working.

I tried to add RigiBody to the objects but nothing changed.

I know I could try the ‘reversed’ method by making 3 scripts, one on each of my objects but I want to keep it in one script because there are more than 3 objects that I want this to happen.

The whole idea is working perfectly with the ‘OnCollision’ function but as I mentioned I would like the events to happen even either If my player is inside a specific ‘triggered’ area, or if I move close to an object so this object will come inside my player’s ‘triggered area’.

Thanks.

You can also use physics 2D overlap circle to learn more about it check out unity docs physics 2D overlap circle.
,If your making a 2d game then you can use a physics 2d overlap circle which just gets info on anything that enters a circle and gets 3 main arguments.

1.the circle radius.
2.the point or the center point of the circle.
3.layer mask so the that it will only trigger on a specific layer.

you write the script in an update method you can use the players position.

physics2d. overlap circle(player.position,radius,layermask)

This code is only to show you how to place the arguments do not use it for your script there alot of mistakes .

You can learn for unity docs over lap circle.