Question about OnCollision2D and OnTrigger2D

Amazing diagram attached :slight_smile:

Scenerio:
Two GameObjects are on top of each other. Object1 was moving then stops when its center matches the center of Object2. OnCollision2D and OnTrigger2D never occurred because a variable switch is in play that was set to false preventing it. Once Object1 is lined up with Object2 the variable switch is set to true.

Question:
Now that collisions are again possible between the two objects, will code in OnCollision2D or OnTrigger2D be triggered or has the collision state been skipped and everything is in the OnCollisionStay2D\OnTriggerStay2D state?

Sounds like whatever ‘a variable switch is in play’ means might affect your answer.

Let us know!

‘a variable switch is in play’ = a script on object1 has code saying when its center is equal to the center of object2 set the variable to true allowing collisions between the two objects to occur again.

Maybe I say it this way:

  1. Object1 has a script that only allows onCollisionEnter2D code to execute if bool isCentered == true
  2. bool isCentered is set to false at Start()
  3. Object1 collides with Object2 but nothing happens because isCentered == false
  4. Once the centers of the two objects align, isCentered is set to true

I’m wondering if the code in OnCollisionEnter2D will now execute? My thought is it would never execute because the two objects seem like they would be in the OnCollisionStay2D state of collision detection.

Maybe I ask it this way:
Does OnCollisionEnter2D code execute when two colliders touch edges or can it execute if one appears inside of the other without ever touching edges?

When you say this, is it your variable? If so, you know when it will be checked and you know what it will affect because it is your code.

If instead you mean that you are manipulating the physics layer interactive matrix (or individual object layer numbers directly) in order to enable or inhibit the physics system from considering the layers for interaction, and then you change that WHILE they are colliding, I am not sure the documentation specifies what happens, at least based on my reading of the docs.

unity physics interaction matrix

As above but also …

A “variable” won’t change what can and does contact and subsequent reporting of collisions so that’s incorrect or you’re not describing what you’re doing correctly.

It’s unclear if you’re talking about the callbacks happening or your code running in those callbacks. There’s a huge difference. If it’s your code running in them then that’s entirely your logic so it makes it extra confusing on why you’re asking here as we have no knowledge of it. You’re not showing any C# code for instance.

You need to be more clear rather than using high-level vague descriptions. If you don’t know how to describe stuff then at least provide some script to demonstrate what you mean.

Please use code-tags .