- Home /
OnCollisionEnter before Physic simulations?
Hello, my game relies on collision detection but every time I call OnCollisionEnter it's always a frame too late because OnCollisionEnter is called after FixedUpdate and all the physics simulations. Is there a way around this?
I don't follow exactly but are you looking for LateUpdate() ? (see docs)
OnCollisionEnter should be called before the physics calculation are applied to the 2 colliding objects in question. OnCollisionExit is called the following frame.
Thanks for the responses. It says here that the OnCollision functions are called after the Physics simulations
http://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html
All Awake calls
All Start Calls
while (stepping towards variable delta time)
All FixedUpdate functions
Physics simulation
OnEnter/Exit/Stay trigger functions
OnEnter/Exit/Stay collision functions
Rigidbody interpolation applies transform.position and rotation
On$$anonymous$$ouseDown/On$$anonymous$$ouseUp etc. events
All Update functions
Animations are advanced, blended and applied to transform
All LateUpdate functions
Rendering
Have an empty gameObject as a child with a collider set to trigger, and slightly larger than the parent collider?
I highly suspect that list to be misleading and that the OnEnter callback would actually be called from within the physics simulation stage.
I could always be wrong.
Answer by DanJC · May 06, 2013 at 05:49 PM
Just in case anyone else is looking along these lines, the short answer is no.
All of the OnCollision*() functions are called after the physics simulation is completed. When OnCollisionEnter() is called, transform.position will already reflect the new location.
The way around it is to manually look for upcoming collisions with your own code.
The further downside to the OnCollision*() functions is that they do not detect a collision in response to the physics simulation. They detect a collision in response to where the object was when it started FixedUpdate().
In other words, if an object is at point A, moves to point B, and collides at point B, the OnCollisionEnter() function will not catch this as a collision because at the start of FixedUpdate() the object was still at point A. This is regardless of anything that took place in the physics simulation, even if the collision is clearly apparent on screen, even if transform.position is clearly in a place where a collision must exist. OnCollision*() will not be called until after the FixedUpdate() (AND after that FixedUpdate()'s following physics simulation) in which the object started in a colliding position.
Which means that the collision at point B will be called after the object has moved to point C. Sounds crazy but that's how it works. Further discussion here:
http://answers.unity3d.com/questions/27138/oncollisionenter-event-seems-to-be-called-late.html
To make matters worse, when you use data from the Collision object passed in, that data is also from one FixedUpdate() ago. Collision data and transform data will, at the same time inside any OnCollision*() function, point to an object in 2 different places.
Answer by jj_unity328 · Aug 17, 2018 at 07:34 AM
In the physic loop, FixedUpdate
is first called before OnTriggerXXX
and OnCollisionXXX
.
But you'll also notice that yield return new WaitForFixedUpdate
happens right after them, so if required, you might also move your code there.