Skip to main content
Game Physics – Complete Beginner to Advanced Guide
CHAPTER 16 Intermediate

Physics Systems in Unity

Updated: May 16, 2026
25 min read

# CHAPTER 16

Physics Systems in Unity

1. Introduction

Throughout this course, we have discussed universal physics concepts that apply to any engine. Now, we will look at how these concepts are explicitly implemented in the world's most popular indie game engine: Unity. Unity does not calculate its own 3D physics; it acts as a wrapper for Nvidia PhysX, a highly optimized, industry-standard C++ physics library. In this chapter, we will master Unity's specific physics implementation. We will explore the Rigidbody component, understand why physics code *must* go in FixedUpdate, and learn how to debug invisible colliders in the editor.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Configure a Unity Rigidbody for dynamic simulation.
  • Differentiate between Update() and FixedUpdate() in Unity C#.
  • Utilize Unity's Physics.Raycast for instant hit detection.
  • Apply Unity PhysicMaterial to control friction and bounciness.
  • Use Unity's CharacterController for Kinematic player movement.

3. The Unity Rigidbody

To hand an object over to Nvidia PhysX, you attach a Rigidbody component.
  • Mass: Unity assumes 1 Unit = 1 Meter, and Mass = Kilograms.
  • Drag & Angular Drag: Air resistance.
  • Use Gravity: A simple checkbox. Unchecking it allows objects to float in space while still bouncing off walls.
  • Is Kinematic: Checking this tells PhysX: "I will move this object using my C# script, but I still want it to push other rigidbodies around." (Used for moving platforms).
  • Interpolate: If your camera follows a Rigidbody and it looks jittery, turning on Interpolation forces Unity to smooth out the visual rendering between physics frames.

4. The FixedUpdate Loop

This is the most important rule of Unity physics: Never put Rigidbody code in Update().
  • Update() runs every time a frame is drawn (maybe 60 times a second, maybe 144).
  • FixedUpdate() runs on a strict, unyielding timer (by default, exactly 50 times a second, or every 0.02 seconds).
Because PhysX needs mathematical consistency to calculate momentum properly, all rb.AddForce and rb.velocity changes MUST happen in FixedUpdate.
csharp
1234567891011121314
public Rigidbody rb;

// BAD: Will result in inconsistent, jittery jumps
void Update() {
    if (Input.GetKeyDown(KeyCode.Space)) rb.AddForce(Vector3.up * 10f);
}

// GOOD: Frame-rate independent physics calculation
void FixedUpdate() {
    if (jumpRequested) {
        rb.AddForce(Vector3.up * 10f, ForceMode.Impulse);
        jumpRequested = false;
    }
}

5. Unity Colliders and Materials

Unity offers several primitives: BoxCollider, SphereCollider, and CapsuleCollider. To make them bounce or slide:
  1. 1. Right-click in the Project window -> Create -> Physic Material.
  1. 2. Set Bounciness (0 to 1).
  1. 3. Set Dynamic Friction (sliding friction).
  1. 4. Drag this material into the Material slot on your Collider component.

6. The CharacterController Component

Unity's built-in CharacterController does *not* use a Rigidbody. It is a highly advanced Kinematic script. It allows you to call controller.Move(velocity * Time.deltaTime).
  • It automatically handles sliding against walls.
  • It automatically steps up stairs (using the "Step Offset" setting).
  • It handles slopes (using the "Slope Limit" setting).
*Always use this for First-Person and Third-Person players instead of a Rigidbody!*

7. Visual Learning: Unity Physics Debugging

In the Unity Editor, you often cannot see what the physics engine is doing.
  1. 1. At the top of the Scene View, click the Gizmos button. This draws green outlines around all invisible Colliders.
  1. 2. In C#, use Debug.DrawRay(startPos, direction, Color.red) to visually draw your mathematical Raycasts in the Scene View!

8. Best Practices

  • Collision Matrices in Unity: Go to Edit -> Project Settings -> Physics. At the bottom, you will see a massive grid of checkboxes. Uncheck interactions (e.g., "PlayerProjectiles" vs "PlayerProjectiles") to instantly optimize your Unity game.

9. Common Mistakes

  • Scaling Colliders via the Transform: If you have a SphereCollider, and you change the GameObject's Transform.Scale from (1, 1, 1) to (1, 5, 1), you have created an oval. PhysX hates non-uniform scales for spheres and will often glitch or throw errors. Always scale the SphereCollider's *internal* Radius property, not the GameObject's Transform!

10. Mini Project: Unity Physics Playground

Objective: Create a script that utilizes Unity's specific physics API to jump and detect ground.
csharp
12345678910111213141516171819202122232425262728293031323334353637383940
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class UnityPhysicsPlayer : MonoBehaviour
{
    private Rigidbody rb;
    public float jumpForce = 5f;
    private bool shouldJump = false;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        
        // Optimize: Prevent the physics engine from tipping the player over!
        rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
    }

    void Update()
    {
        // 1. Get Input in Update (because inputs are lost if checked in FixedUpdate!)
        if (Input.GetKeyDown(KeyCode.Space))
        {
            shouldJump = true;
        }
    }

    void FixedUpdate()
    {
        // 2. Apply Physics in FixedUpdate
        if (shouldJump)
        {
            // Shoot a Unity Raycast down 1.1 meters to check for the floor
            if (Physics.Raycast(transform.position, Vector3.down, 1.1f))
            {
                rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            }
            shouldJump = false; // Reset the trigger
        }
    }
}

11. Practice Exercises

  1. 1. What is the name of the C++ physics engine that runs under the hood of Unity 3D?
  1. 2. Why is it a critical error to place Rigidbody.AddForce() calls inside the standard Update() method?

12. MCQs with Answers

Question 1

A moving platform in Unity needs to push physical crates around, but it shouldn't fall due to gravity or be pushed back by the crates. How should its Rigidbody be configured?

Question 2

To make a first-person shooter character gracefully walk up a set of stairs without bouncing violently, which Unity component should be used?

13. Interview Questions

  • Q: Explain the mechanical difference between Unity's Update() and FixedUpdate() loops. Provide a scenario where mixing them up results in a gameplay bug.
  • Q: How do you configure a Unity PhysicMaterial to create a perfect, endlessly bouncing rubber ball? What properties are changed, and what are their values?
  • Q: A junior developer uses Transform.Translate to move an enemy through a wall, bypassing the Rigidbody entirely. Explain why this breaks the PhysX pipeline and what method they should use instead.

14. FAQs

Q: Does Unity have 2D physics? A: Yes! Unity uses a completely separate engine (Box2D) for 2D games. You must use Rigidbody2D, BoxCollider2D, and Physics2D.Raycast. Do not mix 3D and 2D physics components; they cannot see each other!

15. Summary

In Chapter 16, we translated our universal physics knowledge into the Unity Engine. We learned that Unity relies on Nvidia PhysX, accessible via the Rigidbody component. We established the golden rule of Unity physics: all Rigidbody modifications must occur in the mathematically consistent FixedUpdate loop. We utilized the CharacterController for smooth bipedal movement, and learned how to configure PhysicMaterials. We are now ready to build commercial physics games in Unity.

16. Next Chapter Recommendation

Unity uses PhysX, but what about the engine behind *Fortnite* and *The Matrix Awakens*? Proceed to Chapter 17: Physics Systems in Unreal Engine 5.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·