I’m coding my own engine based on knowledge of unity, but i have stuck at one moment: are OnTriggerStay triggered at same frame when OnTriggerEnter or OnTriggerExit triggered?
example: object enters in trigger, OnTriggerEnter will be triggered, but will be triggered OnTriggerStay at the same frame?
Not sure, but you could test this in Unity by creating a scene with two objects; one with a trigger collider and a script that logs to the console when the OnTriggerEnter, OnTriggerStay, and OnTriggerExit functions are called.
Make this object slowly move towards the other object in whichever way, then all you have to do is pause the application as it’s running and step through frames manually with the frame-step button beside the pause button.
Once the objects intersect each other, you should see which logs are printed in the console at which frames as you step through them.
First off here is the event flow of Unity:
OnTriggerEnter - occurs on the frame (fixed frame that is) that the colliders first interact. So if previous frame they were not, and this frame they are, then OnTriggerEnter occurs.
OnTriggerStay - occurs if the colliders were intersecting the previous fixed frame, and are still currently intersecting.
OnTriggerExit - occurs if the colliders were intersecting the previous fixed frame, and are currently no longer intersecting.
No, they will not happen on the same frame (fixed frame that is… note the physics engine may calculate multiple times per rendered frame).
Clearly OnTriggerExit wouldn’t, since OnTriggerExit occurs AFTER they’ve stop intersecting. You can’t intersect and not intersect at the same time.
And OnTriggerStay occurs only if the previous frame they were intersecting, which is distinct from the definition of OnTriggerEnter.
Not trying to necro this thread, but it comes up as a top search result when searching for OnTriggerEnter and OnTriggerStay frame queries. And it seems some of this information is either incorrect, or it has changed in more recent versions of Unity.
Under Unity 2018.3, OnTriggerEnter and OnTriggerStay appear to fire in the same frame. There doesn’t appear to be any requirement that the colliders began overlapping in the previous frame. To confirm this, I simply added Debug.Log in the OnTriggerEnter and OnTriggerStay methods on my controller, and walked into the trigger. The log results:
OnTriggerEnter with FirstPersonCharacter on frame 360
OnTriggerStay with FirstPersonCharacter on frame 360
It does appear to be the case that OnTriggerEnter will fire before OnTriggerStay. So hopefully that’s not a coincidence.
Sorry to necro this post, but it is surprising that the functionality for OnTriggerXXX would be different than OnCollisionXXX. (this is also the top link on google for this question)
Like @dgoyette mentioned, OnTriggerEnter/Stay will run on the same frame, but interestingly OnCollisionEnter/Stay will be separated by a frame. OnTriggerStay doesn’t seem to check the previous frame whereas OnCollisionStay does require that the last frame had a collision.
I’m surprised this is the intended functionality, it seems like a bug.