- Home /
Find all objects currently colliding with trigger
Lets say I have a simple sphere collider that I am using as a trigger for an enemies vision radius. Is there a way to quickly get an array or list of all the colliders currently colliding with that trigger or within it?
Currently the only way I can think to do this would be to maintain my own list on the object using OnTriggerEnter and OnTriggerExit. However I am assuming I have simply missed a function that does exactly this when I looked through the script reference for it. I also could use Physics.overlapsphere however that would result in me having to place hard coded values within the script and I would much prefer to simply be able to call the trigger sphere collider I already have attached to the object and then acquire a list or array of colliders to work with.
Thank you in advance for any help.
I'm afraid you'll have to maintain your own list from the OnTriggerEnter/OnTriggerExit or use one of the Physics functions (whose parameters could be read from a given Collider).
Collider[] colliders = Physics.OverlapSphere(visionRadius.center,visionRadius.radius);
was the solution I ended up going with, I actually completely forgot I could simply use the sphere collider attatched to the object in the script. Thanks.
I have an issue where I have a trigger that triggers an event on NPCs when they enter it. I cant put them into an array as they might 'die' while still being 'in the array' and we all know the madness of missing references due to the array silliness, so what I want to do is just "hey who is currently touching my collider". is there anyway to do this at all or are we stuck to Enter/Exit and Stay is unreliable due to its being set on certain frames...
Answer by rutter · Mar 26, 2012 at 12:26 AM
I also could use Physics.overlapsphere however that would result in me having to place hard coded values within the script
Not necessarily: you could access your trigger collider to use its position and radius as parameters for the call.
Like BY0LOG1C said, though, you may want to consider using the enter/exit functions provided by the trigger itself.