- Home /
Check if something enters my trigget withowt OnTriggerEnter
Could I wheck if something enters my trigget withowt OnTriggerEnter? The goal is to avoid using OnTriggerEnter, in cases when I want to check intersection just once or twice during my level.
I have a lot of triggers that needed to be checked very rarely, and I trying to find a way to remove all controllers (that just has OnTriggerEnter and changes one public var) and just check the intersection of trigger by specified object from outside. Does it exists?
PS. collider.bounds.intersects() is too rough for my case.
Answer by tanoshimi · Oct 18, 2014 at 06:40 PM
No. By definition, the only way you can act the moment something has entered your trigger (however rarely that may occur) is to check every frame, and that's what OnTriggerEnter does.
If you want to disable trigger checking based on some condition (e.g. perhaps don't bother checking when all triggering objects are a long distance away), you can disable the script component containing the OnTriggerEnter, and then re-enable it again when the potential triggering object(s) gets close. But then that requires adding a distance check and the overhead of enabling/disabling the component, so it might not be worth the effort.
thank your for your answer. but I`m realy puzzled, why so. Unity definitely hav some code that it used every frame to detect the intersections... why not let game-developers use them too when it needed, and avoid per-frame calls of it?
There is no one-shot "Has anything entered this trigger in this frame?" method. Nor is there a (potentially more useful) one-shot "Is anything currently inside this trigger?" method. That's just how it is. Have you reason to believe that per-frame collision detection is causing a significant performance hit in your code?
there is widespread belief that a lot of objects with OnTriggerEnter method is bad in terms of performance. Unfortunately, I have no proof nor disproof yet, I just respect this popular opinion :)
Well, the colliders of every gameobject in your scene are exposed via the API, so there's nothing to stop you writing your own collision-checking functions.
I'm not sure how 'widespread' that belief is @hav_ngs_ru
OnTriggerEnter really shouldn't bog down performance at all. What, if anything, would cause the performance hit would be a lot of objects with complicated colliders on them (like mesh colliders ins$$anonymous$$d of sphere or box).
Your answer
Follow this Question
Related Questions
Colliders that do not collide... 1 Answer
OnTriggerEnter not being called with multiple colliders 1 Answer
How to not trigger its own collision box? 0 Answers
OnTriggerEnter 1 Answer