I believe I have found a rather serious bug in the Unity 5 2D physics related to triggers - OnTriggerEnter2D seems to be called with one frame delay. I’ve already reported the bug but I’d like to hear your opinion on what’s going on here.
I discovered that when a Rigidbody2D with a Trigger2D attached to it goes through objects, OnTriggerEnter2D is called AFTER the object has moved another step in Unity 5. It is called at the correct moment in Unity 4.
I’ve attached a small package that demonstrates this strange behaviour.
In Unity 4, the test gives this result, which is what makes sense to me:
FixedUpdateFrame 006: Bullet is now at (10.5000000000, 3.5000000000, 0.0000000000)
FixedUpdateFrame 006: Bullet at (10.5000000000, -0.5000000000, 0.0000000000) hit floor at (11.0, -2.0, 0.0)
FixedUpdateFrame 007: Bullet is now at (10.5000000000, -0.5000000000, 0.0000000000)
When the bullet moves from 3.5 to -0.5, it passes over an object, and OnTriggerEnter2D is called.
BUT on Unity 5, the following happens:
FixedUpdateFrame 006: Bullet is now at (10.5000000000, 3.5000000000, 0.0000000000)
FixedUpdateFrame 007: Bullet is now at (10.5000000000, -0.5000000000, 0.0000000000)
FixedUpdateFrame 007: Bullet at (10.5000000000, -4.5000000000, 0.0000000000) hit floor at (12.0, -1.0, 0.0)
Note that OnTriggerEnter IS called, but only after the object has moved from -0.5 to -4.5 - even though it should have already triggered when moving from 3.5 to -0.5. It is called one frame late!
This really messes things up for fast-moving objects. I think I can do a workaround this issue by doing a raycast to prevent this delay, but it’s an expensive operation to do each frame. Very annoying!
2639719–185774–Physics OnTriggerEnter2D Delay Bug.unitypackage (11 KB)