- Home /
How many triggers is too many? Do they affect performance too much?
Sorry if this is a stupid question but I currently have like 20 triggers in one scene. I'm pretty sure that isn't a lot but I will probably end up with hundreds at the end of the whole project so I'm worried if using triggers is a bad practice. I'm sure I can work around using triggers to start events if need be. Thank you in advance for any response.
I believe OnTriggerEnter() is only executed on a single frame, when something enters the trigger's area. So say you have 100 triggers on a race track or something, only one would fire at a time. But say you have 100 cubes that are triggers, and they are all triggered at once, that may have some performance impact. OnTriggerEnter.
I am pretty sure OnTriggerStay() would be fired on all frames that a collider is in it's area. OnTriggerStay
The links will provide more info. Hope this helps.
Answer by TonyLi · Jul 31, 2013 at 02:15 PM
Depending on your target platform, hundreds of triggers will probably be fine, and is generally a good practice over writing your own physics simulation. The Physics subsystem is very optimized.
It updates on the fixed timestep interval, which you can set through Project Settings > Time > Fixed Timestep. This is a different interval than Update(), which gets called every time the frame is redrawn.
OnTriggerEnter() and OnTriggerExit() only get called on the fixed update where the colliders enter or exit triggers, not every frame. OnTriggerStay(), on the other hand, gets called every fixed update that the colliders are in contact, so you generally want to avoid it.
One thing that will really hurt performance is if your static triggers move (including just rotation) and don't have rigidbodies. If you're going to move a trigger, attach a rigidbody.
The Rigidbody Sleeping page has more information on this and tips for static triggers and other physics objects.