- Home /
MonoBehaviour.OnTriggerX vs Collider.OnTriggerX
I haven't found this question anywhere.
If you check Collider's and MonoBehaviour's description (comparing OnTriggerStay function) you would see that the Collide one "is called almost all the frames" and the other "is called once per frame".
I'm using an empty game obj with a trigger box collider witch has all OnTrigger functions (no need for a rigidbody), checking against moving kinematic rigidbody game objs with non-trigger box collider.
The logic I'm trying to accomplish is when is not triggering with any other obj, then spawn a new moving obj.
void OnTriggerEnter( Collider other )
{
Debug.Log("Enter");
isTriggering = true;
}
void OnTriggerStay( Collider other )
{
Debug.Log("Stay");
isTriggering = true;
}
void OnTriggerExit( Collider other )
{
Spawn();
Debug.Log("Exit");
isTriggering = true;
}
The new spawned obj will be to the right of the empty obj, a few units inside the collider, so that way the next frame OnTriggerEnter would be called (each obj move to the left each frame). Then OnTriggerStay is called, and lastly when the obj gets outside the trigger collider, OnTriggerExit is called. In other words, one trigger function should be called every frame. Should be.. but sometimes neither of them get called. That's why I'm using the isTriggering flag.
void LateUpdate()
{
if( !isTriggering )
{
Spawn();
Debug.Log("Trigger failed");
}
isTriggering = false;
}
As you can see in the image below I'm using Collider's OnTrigger functions, and what I want to ask is how can I manage to force these functiones to get called every frame? (to use MonoBehaviour triggers).
I've tried with Courutines and Invokes every frame but OnTrigger functions need a Collider obj as parameter to check with whom collided with, and that info is provided by the Unity Event system.
Note: I'm using LateUpdate to check if no trigger collision was detected and to reset the flag.
So why there are two variants of OnTrigger and OnCollider functions?
Answer by Eric5h5 · Oct 05, 2014 at 02:30 AM
The docs are wrong about it being called every frame; they should say that it's called every physics frame. It's not possible to make it be called every frame.
So you say $$anonymous$$onoBehaviour's OnTrigger functions are called as OnCollision functions? every FixedUpdate?
Only OnTriggerStay is called every physics frame; OnTriggerEnter/Exit are only called once. Same with collision.
Yes, you are right. I know that Trigger events are separated between three actions:
Enter: when is the first time a trigger collision is cough (if the last frame there wasn't a collision)
Stay: if the last frame was cough a trigger collision.
Exit: if this frame was not cough a collision.
The problem that I'm having (even with OnTriggerStay) is that is not being called every frame. I checked that with the isTriggering flag.
I think the reason is that OnTriggers are being called sometimes every physics frame, compared with OnCollisions, which are called each frame.