[Solved] OnTriggerEnter2D not working

Hello, I am trying to recreate among us in unity and seemed to have stumbled across a problem. I am currently working on the kill, death, and ghost mechanics and am having trouble with the OnTriggerEnter2D method. The way I am getting the closest objects is by using that method and storing the gameobjects inside a list. I saw that the list wasn’t being added to and the detection wasn’t working. On my player prefab, I have one collider that handles collisions, such as walls, and I recently added another box collider to detect what is in range of them. I have made sure to check the is trigger button on the collider. If anybody could help me please respond, and thank you for helping me. I have never had a problem with this in 3D (I work mainly with 3D) but 2D might be different.

Are your colliders BoxCollider2D or just BoxCollider? Most of the 3D Colliders have a 2D specific one (i.e. Sphere Collider is a CircleCollider2D).

If that isnt the problem you should show your code.

Yes I am using the correct collider and here is my code:

public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            Debug.Log("Collided with player");

            PlayerMovement tempTarget = other.GetComponent<PlayerMovement>();
          
            if (tempTarget.GetComponent<PlayerRoles>().imposter)
            {
                return;
            }
            else
            {
                targets.Add(tempTarget);
                Debug.Log(tempTarget.name);
            }
        }
    }

    public void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            PlayerMovement tempTarget = other.GetComponent<PlayerMovement>();
            if (targets.Contains(tempTarget))
            {
                targets.Remove(tempTarget);
            }
        }
    }

So this looks right so its probably be something in your inspector. Did you tag the player object as “Player”? Sometimes that is a common mistake. Is the script actually attached to your walls or w/e it is that its supposed to be on?

I did tag the players correctly, and here is a screenshot from the inspector.

6470984--726308--Capture.PNG

So i see 2 colliders on that object. One of them is not marked as trigger. It could be that the one NOT marked as trigger is bigger and swallows the one that is the trigger. Basically, your player’s collider may not actually being hitting the trigger but hitting the collider that is not the trigger.

I made the trigger collider bigger because I wanted people to kill from a distance just like among us. So I don’t believe it is that.

Hmmmmmmm weird weird. Your OnTriggerEnter2D function isnt inside of Update or anything is it?

Oooo maybe i just saw it. Try using other.gameObject.tag == “Player” instead of other.tag.

I will give your suggestion a go and get back to you soon.

Though it was a good suggestion, it didn’t work.

Weird, thats how i use all of my tag comparisons in OnTriggerEnter2D and OnCollisionEnter2D. Now im really confused since it seems like everything is working properly. I mean, your Player has a Collider2D on it as well im sure, right?

Check your collider’s size on both the player and walls or w/e are checking for trigger collision. Maybe take off the public label on both OnTriggerEnter2D and OnTriggerExit2D. I never see it as public so maybe that has some quirk with it.

Here are some other links to possible things to look for:

https://discussions.unity.com/t/531469

https://answers.unity.com/questions/592567/why-isnt-my-ontriggerenter2d-function-working.html

Ok, normally I can solve these problems in less than 10 minutes but this one stumped me. Glad I’m not the only one confused

I normally have issues with OnCollisionEnter2D.

Maybe try taking off the other Collider2D that isnt a trigger? Just to see if thats an issue too.

If that fails as well, then just recreate something super simple that gets the OnTriggerEnter2D working. Like 2 sprites each with a Collider2D and a simple script that checks if they collide. Or just watch a video on it and they might explain something so simple im forgetting it.

Sorry im not more help.

I will try that, and if it doesn’t work I want to thank you anyway for putting the time into this.

One more question, I have been using OnTriggerEnter2D, do you think it would be better to use OnCollisionEnter?

If it is a wall or something physical then i would say yes, use OnCollisionEnter2D. Make sure both objects have 2D colliders and AT LEAST ONE of them has to have a Rigidbody2D.

If it is a laser or security camera view, then Trigger is better, imo.

Ok, thank you!

I figured it out! In among us, they made it so players couldn’t collide with each other. I wanted this to so I used a Physics2D.IgnoreLayer() function. This made it so the trigger wouldn’t work.

1 Like

Ahhhh gotcha. Glad you figured it out! Also, you can go to the Physics2D and it has a layer matrix which says what layers can collide with other layers. Its just a nice way to do this without using code. Regardless, glad you figured it out!