- Home /
Check trigger/collider once?
Hi,
I have two closely related questions, the first which led to the second:
How computationally expensive is it using many triggers and colliders (kinematic only)? I would assume that for OnTriggerEnter to be aware of a collision that behind the scenes it must be checked every frame?
This led me to wonder, is there a way to check (only once, at a precise time) if there are any current collisions for a particular object with a trigger collider? Due to the structure of my game I know exactly when the OnTriggerEnter would be activated, but it is still very useful to use in order to access the objects involved in the collisions.
In a sense, it would be useful to turn on the 'OnTriggerEnter' for a short period of time, and the turn it off again afterwards since I know that I will have no relevant collisions. If it would be a form of optimization then I would like to try it.
Thanks,
Matt
Answer by nsxdavid · Feb 25, 2013 at 07:17 AM
You can achieve this in a sense by setting the trigger collider's enable to false when you are not wanting it to report collisions.
The underlying Physics engine (PhysX) can handle a lot of these things very efficiently, though it really depends on your scene. If there are tons of OnTrigger events going off every frame, your issue would be more about all the callbacks happening than anything.
No need to worry about performance really until you notice a performance issue. Just use the profiler to see how things are shaping up as you go.
Thanks, I think this is one viable option.
Also, the advice about the performance is a sensible one. While it's a reasonable idea to be aware of potential performance impacts before you hit them, if the game works the way it's coded then I suppose optimization isn't needed for the most part.
I should add that I am trying out the Physics.OverlapSphere now which might give me a little more control over the trigger since I can call that as and when I need and not worry about checking for a trigger that I have accidentally turned off elsewhere in my code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093306im_/https://answers.unity.com/themes/thub/images/avi.jpg)