How to prevent colliders from passing through each other

· 1 min read

Collisions with fast-moving objects are always a problem. A good way to ensure that you detect all collisions is to use Raycasting instead of relying on a physics simulation. This works well for bullets or small objects, but won't produce good results for large objects.

void FixedUpdate() {
  Vector3 direction = new Vector3(transform.position - lastPosition);
  Ray ray = new Ray(lastPosition, direction);
  RaycastHit hit;
  if (Physics.Raycast(ray, hit, direction.magnitude)) {
    // Do something if hit
  }

  this.lastPosition = transform.position;
}