Search code examples
unity-game-enginecollision-detectionunity3d-2dtools

Solve collisions outside of the normal physics cycle


Is there a way to test and resolve collisions outside of the usual collision system?

There is a situation where I want most of the game objects to collide using the basic filters system unity provides.

But a very special group of objects (within 4 different layers) will behave differently and collide in some situations differently.

I will add child objects with circular triggers to serve as the broad phase, then I would like to call the engine's collision resolution only if the objects are supposed to collide.

How should I proceed to trigger the collision resolution manually?


Solution

  • You can use Physics2D.BoxCast, Physics2D.CircleCast, Physics2D.OverlapArea or Physics2D.OverlapCircle to check collisions outside of physics' simulation routine.

    You may resolve collisions based on data received from methods above. AFAIK Physics2D is adopted Box2D, which is open-source. So you may find solution for coliisions in its source code.