Lesson 5: Physics & Collision Detection - Making Things Interact

In Lesson 4 you used C# and Transforms to move objects with code. In this lesson you will let Unity’s physics engine do the work: gravity, bouncing, and real collisions so your GameObjects interact like solid, physical things.

By the end of this lesson you will:

  • Add Rigidbody and Collider components and understand their roles.
  • Use physics materials to control bounciness and friction.
  • Detect collisions and triggers in C# with OnCollisionEnter and OnTriggerEnter.
  • Choose between 2D and 3D physics so your project stays consistent.

1. Why use physics?

Moving everything manually in Update is fine for a single character, but for many objects (projectiles, platforms, pickups, enemies) you want:

  • Gravity and forces applied automatically.
  • Collisions so objects don’t pass through each other.
  • Realistic (or stylized) bounce and slide.

Unity’s physics engine handles this. You only need to:

  1. Add a Collider (shape that defines “solid” volume or area).
  2. Add a Rigidbody (optional for moving objects; required for objects that should be pushed by forces and gravity).
  3. Optionally use Physics Materials to set bounciness and friction.

2. Rigidbody and Collider (3D)

  • Rigidbody

    • Makes the GameObject part of the physics simulation.
    • Adds mass, gravity, and response to forces (e.g. AddForce).
    • Use Rigidbody for 3D and Rigidbody2D for 2D.
  • Collider

    • Defines the shape used for collisions (e.g. Box, Sphere, Capsule).
    • Use BoxCollider, SphereCollider, etc. for 3D and BoxCollider2D, CircleCollider2D for 2D.
    • At least one of the two colliding objects must have a Rigidbody for a physics response; the other can be Collider-only (e.g. static floor).

Quick setup (3D):

  1. Create a Cube (or any GameObject).
  2. Add Rigidbody (Add Component → Rigidbody).
  3. It already has a Box Collider.
  4. Press Play: the cube falls and stops on any object with a Collider (e.g. a Plane).

3. Physics materials (3D)

Physics Material controls bounciness and friction:

  • Create: Project window → Right-click → Create → Physic Material.
  • Bounciness: 0 = no bounce, 1 = full bounce.
  • Dynamic Friction / Static Friction: Sliding resistance.

Assign the material in the Collider’s Material slot. Use the same or different materials on each object; Unity combines them for the pair.

4. Detecting collisions in C# (3D)

Two ways to react when shapes overlap:

  • Collision (solid): Objects block each other. Use Collider (not trigger) and Rigidbody on at least one.
  • Trigger (overlap only): No physical push; you only get a callback. Check Is Trigger on the Collider.

Collision callbacks (non-trigger):

  • OnCollisionEnter(Collision collision) – first frame they touch.
  • OnCollisionStay(Collision collision) – every frame they’re touching.
  • OnCollisionExit(Collision collision) – first frame they separate.

Trigger callbacks:

  • OnTriggerEnter(Collider other)
  • OnTriggerStay(Collider other)
  • OnTriggerExit(Collider other)

Example: damage the player when an enemy touches them (collision), or collect a coin when the player enters a trigger zone.

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        // Apply damage, play sound, etc.
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Pickup"))
    {
        // Add score, destroy pickup, etc.
        Destroy(other.gameObject);
    }
}

Use Tags (e.g. "Player", "Pickup", "Enemy") so you can identify objects safely instead of relying on names.

5. 2D vs 3D physics

  • 3D: Rigidbody, BoxCollider, SphereCollider, etc. Callbacks use Collision / Collider.
  • 2D: Rigidbody2D, BoxCollider2D, CircleCollider2D, etc. Callbacks use Collision2D / Collider2D.

Do not mix 2D and 3D components on the same object. Keep your project either 2D or 3D for physics.

6. Best practices

  • Static environment: Use Colliders without Rigidbody for walls, floors, obstacles.
  • Moving objects: Use Rigidbody + Collider; avoid moving physics objects by changing Transform.position every frame—prefer forces or Rigidbody.MovePosition so the engine can resolve collisions correctly.
  • Performance: Prefer simple colliders (Box, Sphere, Capsule) over MeshCollider when possible.
  • Layers: Use Physics Layers and Layer Collision Matrix to control which objects collide (e.g. projectiles vs projectiles off).

7. Troubleshooting

Object falls through the floor

  • Ensure the floor has a Collider and at least one of the two objects has a Rigidbody.
  • Check that both are on layers that can collide (Layer Collision Matrix).

No collision/trigger callback

  • One object must have a Rigidbody (for 3D/2D as appropriate).
  • For triggers, Is Trigger must be checked and you must use OnTriggerEnter (not OnCollisionEnter).

Jittery or unstable motion

  • Avoid moving Rigidbody objects by setting Transform.position every frame. Use forces or Rigidbody.MovePosition.
  • Increase Fixed Timestep (Edit → Project Settings → Time) only if needed; usually default is fine.

Summary

You learned how to make objects interact using Unity’s physics: Rigidbody for gravity and forces, Collider for shape, and Physics Material for bounce and friction. You can react to collisions (solid) or triggers (overlap-only) in C# with OnCollisionEnter and OnTriggerEnter, and you know to keep 2D and 3D physics separate.

In the next lesson you will apply this to 2D game development: sprites, 2D physics, and animation so you can build a small 2D game from scratch.