Scope - how does it know which collission is happening?

I think this is a question about object scope - Unity knows where to look.

OnTriggerEnter2D takes a collider2d as an argument, and we then tell it that if that collider2d has the tag “Ground” then do something.

Why don’t we have to specify that it’s our player’s collider (not some other collider in the code? Does that scope get set because the script is a Component of our player class? Could you contrast that to the FindObjectOfType<> call we did when we cuased the ground effector to boost player speed?

Why don’t we have to specify which of the player’s colliders could be colliding (the capsule vs the circle). If I wanted to check the condition only against the circle collider, how could I do so?

Thank you again for this lovely course!

1 Like

You don’t need to specify which collider belongs to the player because the OnTriggerEnter2D method is called on the GameObject with the Collider2D component.

Therefore, the collider that enters the trigger is guaranteed to belong to some other object, and not the same object as the script. This is because colliders on the same object do not collide with each other.

Yes, the scope of the OnTriggerEnter2D method is set because the script that contains the method is a component of the player GameObject. When you attach a script to a GameObject in Unity, the script becomes a component of that GameObject, and its methods can be called by Unity’s event system, such as the OnTriggerEnter2D event.

Sure! In the case of the FindObjectOfType<> call, we’re using it to find a specific object in the scene by its type or tag, regardless of which GameObject the script is attached to.

For example, if we want to find the “GroundEffector” GameObject in the scene and access its GroundEffector component, we can use FindObjectOfType() to get a reference to the component. This method searches the entire scene for an object with the GroundEffector component attached, so it doesn’t matter which GameObject the script is attached to.

If you want to check the condition only against the circle collider of the player, you can use the GetComponent method to get a reference to the CircleCollider2D component attached to your player GameObject. Then you can check if the collider that triggered the event is the circle collider by comparing its reference with the circle collider reference.

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.CompareTag("Ground"))
    {
CircleCollider2D circleCollider = GetComponent<CircleCollider2D>();
}
        if (other == circleCollider)
        {
            // do something only if the circle collider triggered the event
        }
    }

I hope this solves your issues!

Edits: I managed to format the code sorry for all the edits

1 Like

Thanks Christopher. I follow you.

1 Like

@Christopher_Powell
Are you sure this code provided at the end would work?
The “Collider2D other” passed in the function is describing other game objects collider - the one which collided with our 2d Colliders attached to the gameobject with script. Or as documentation states:

Sent when another object enters a trigger collider attached to this object (2D physics only).

Further information about the other collider is reported in the Collider2D parameter passed during the call.

So, to my understanding, here you check if the CircleCollider2D collided with itself, which will never happen.
But I might not know something, so please correct me if Im wrong.

@joshuamv
What I understand, when you add more then 1 collider to the object, you create compound collider. So it doesnt matter which one collided - it will cause the same collision event - it is used to create more sophisticated shapes of colliders to better match the mesh (or sprite) shape.
You can attach colliders to child game objects, and use this approach to check which collider triggered the event, but its a little more complex. (So for example if you want to check which body part was hit by enemy bullet, youd attach different colliders to different body parts.)

There is more about collision here, especially what types of colliders interact with different types:

1 Like

Oh thanks bro, yeah you are right, I am thinking maybe this could work?

private CircleCollider2D circleCollider;

private void Start()
{
    circleCollider = GetComponent<CircleCollider2D>();
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.CompareTag("Ground"))
    {
        if (other == circleCollider)
        {
            // do something only if the circle collider triggered the event
        }
    }
}

Maybe I misunderstood the question

1 Like

I mean, the circleCollider variable holds reference to the Circle Collider attached to the same object that script is attached to.

In this example circleCollider has to be set to a trigger in order to fire OnTriggerEnter2D function. Every 2d collider attached to this game object, which is set to trigger, will fire it.

other variable inside OnTriggerEnter2D, holds reference to a different collider - the one that entered one of our trigger colliders 2D. (I.e. the one that collided with circleCollider) So if you compare other to our circleCollider, it will never return true - they will always be different colliders.

1 Like

Dang, I’m trying to solve problem on my phone which isn’t best, Im prolly misunderstanding this, at least I did the other stuff, thanks for pointing this out bro.

Edit I reread your post and see that I didn’t completely mess up which is good

The trigger and collide messages are sent for any colliders attached to the game object and there is no way of knowing which one sent the message. If you want to only respond to a specific collider/trigger it has to be the only collider on the game object.
So, if you have a circle collider and a capsule collider on your player, and you want to only respond to the circle collider in some case, you can put it on its own game object and give it the script that should handle the collision with the circle collider

So, your hierarchy would be something like

player root with capsule collider
   +- game object with circle collider and script

However, if the capsule collider is a ‘collider’ and the circle collider is a ‘trigger’ then it’s easy because the capsule collider (collider) will not send a trigger message and the circle collider (trigger) will not send a collide message.

Thanks everyone for your responses. I think I understand the concept, and the workaround if I ever need to do so. Appreciate you all.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms