Search code examples
unity-game-enginetriggerscollider

Which Game Object need to have trigger Check and which one can use OnTriggerEnter() function?


like what i have in Question , I know it's something simple and there is a question like this but not enough to solve that confusion,my work flow was: A & B are 2 GO , A has a collided with trigger unchecked , B has a collider with trigger checked i use the OnTriggerEnter2D(Collision collision) function in the A GO to Check if B (collision) touch A GO, i was working with that until now when i discovered that also it works when i used the OnTriggerEnter2D(Collision collision) function in the B script to check if it's touched with A GO even that A hasn't trigger checked. So does just i need to check on trigger in one of them (A or B) to use the OnTriggerEnter2D() function in anyone ?


Solution

  • So does just I need to check on trigger in one of them (A or B) to use the OnTriggerEnter2D() function in anyone ?

    Yes, just one of them. You can also enable it on both of them. Doesn't really matter.

    This is confusing to those new to Unity.

    When any type of collision callback function such as OnTriggerEnter2D and OnCollisionEnter2D are called, both of the GameObject involved in the collision will have the callback functions called.

    It doesn't matter which one has IsTrigger set to true. As long as one has is trigger, both will have OnTriggerEnter2D called on them if there is a script that implements the callback function. The-same thing applies to OnCollisionEnter2D. When OnCollisionEnter2D or OnTriggerEnter2D is called on one Object, it will also be called on the other Object invloved in the Collins.