You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

How to prevent colliders from passing through each other

Ben Liu
Ben Liu

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;
}

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp