Collision detection in fast moving objects in 2D.

Hi,

I set “Continuous” as Collision detection mode on my Rigidbody2D objects, mostly bullets, that move very fast to avoid the “tunneling effect”.

My question is, do I need to do anything else? IIRC, in 3D we needed to set Continuous to static objects and CCD on moving objects.

Thanks.

Just using Continuous on the moving object is enough; the 2D physics engine is different from the 3D one so it behaves somewhat differently.

Yes. Due to how physics is simulated (and computational limits) if they’re moving fast enough, they may pass through each other. The conventional way to prevent this is to use ray-casts.

RaycastHit hit;
if(Physics.Raycast(last.position, curr.position, out hit, Vector3.Distance(last.position, curr.position)) {
  curr.position = hit.position;
}

Because Raycasts occur at a different time than physics simulations, this checks if we “should have” hit something. If we would of, we pull the object back to the hit location and because the object is now touching the collisions, our OnHit() event will trigger.

Edit: I did this is 3D, but it will work in 2D as well.