Detecting collisions at one precise time

Hello forum, I’m not totally sure if my question falls into the “Physics” directory but here it is.

I need to know what objects are currently colliding with one specific trigger object at one specific time.

I don’t need to have behaviours happen when that trigger object enters or exits collisions : I just need to have the list of things it’s actually colliding with at the moment I call one specific script. I would love to write something like :
GameObject[ ] theCollidingObjects = myCollider.listofCurrentCollisions
but unfortunately it does not work like this.

Is there a simple solution to do that ? Or do I have to write OnCollisionEnter and OnCollisionExit functions, where objects log themselves in and out of a log book everytime they enter or exit, so that I can have their list when I need it ? It sounds like an awful waste of CPU resource…

Well that’s what I finally did : all interesting objects in the scene have a OnTriggerEnter and OnTriggerLeave procedure, where they register in and out of a logbook everytime they cross the trigger object’s path, so that when I need it I just have to read the logbook to see who’s here.

It still feels like an awful waste of computing time though.

Maybe Rigidbody.SweepTestAll?

1 Like

Do you require a handle to the gameobject being logged? also if the gameobject is destroyed after it was logged how will you manage this collection?

1 Like

This sounds like the solution I’m looking for, but for some reason I can’t make it work.

I have my “interesting” objects with one rigidbody and sphere collider each, and my “trigger” object with one box collider (in “trigger” mode) and one rigid body (that I added because SweepTestAll needs it).

But when I SweepTest the trigger’s object Rigidbody, the code always returns a void array, even though I make sure that there is at least one “interesting” object inside. I suppose that I’m doing something wrong, but what ?

RaycastHit[ ] stuffInside = myTriggerRigidbody.SweepTestAll (Vector3.one, 0.01f, QueryTriggerInteraction.Collide);

And I always get : stuffInside.Length == 0

(I don’t need the trigger object ta actually sweep, but the result is exactly the same when I set the test the Vector3.zero and maxDistance = 0)

All right, I found the solution - maybe not the only one, but the best for me since I’ve just a simple geometric shape to test :

1 Like