Force collision detection to run *now* !

Greetings !

I want to completely by-pass Unity’s loop. I’ve successfully done so, quite easily, for everything but the collisions callbacks. My problem is that I have objects with a OnTriggerEnter callback that I can’t trigger myself: I have to wait for Unity’s loop to finish executing my code, before it makes any collision check and calls the OnTriggerEnters callbacks accordingly.

I want OnTriggerEnters to be called when I ask for it. Is it possible ?

No. The best that you can do is manually check for collision using Physic class and manually call the trigger events.

foreach(var c in  Physics.OverlapSphere(, trigger.radius)) (
   trigger.gameObject.SendMessage("OnTriggerEnter", c);

Remember that engines and frameworks have a proper way to work, if you don’t adapt your code into the engine way you will be in a eternal fight against them :stuck_out_tongue:

You can’t prove a negative, but I also think NO. No way to fire collision code immediately. Lots of questions about that over the years.

Something new like RayCastHit[] Transform.OverlapTest(int layerMask) sure would be nice. But I assume the actual collision testing is too optimized and tangled with other internal data to do that well.

Along with OverlapSphere (which claims to get nearby false positives, but I haven’t tested) Rigidbody.sweeptest can sometimes be used to fake it. Alternately, have triggers maintain lists during each Enter/Exit. The list will always be one physics update old, same as usual, but at least you can check it right away.

I needed to check if the objects are colliding at the Instantiate momment, becase when I am spawning stuff I don’t want them to be intersected, they will spawn randomly and they have a tiny chance of intersecting another of the same object. I need to test this before the next frame happens, I do not want to show the objects and the move them on the next frame through the OnCollisionEnter.

So you can use the collider bounds and check if both colliders are intersectng, works like a charm for me. Great implementation from the Unity dev team.