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 theRigidbody 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
Rigidbodyfor dynamic simulation.
-
Differentiate between
Update()andFixedUpdate()in Unity C#.
-
Utilize Unity's
Physics.Raycastfor instant hit detection.
-
Apply Unity
PhysicMaterialto control friction and bounciness.
-
Use Unity's
CharacterControllerfor Kinematic player movement.
3. The Unity Rigidbody
To hand an object over to Nvidia PhysX, you attach aRigidbody 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 inUpdate().
-
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 every0.02seconds).
rb.AddForce and rb.velocity changes MUST happen in FixedUpdate.
csharp
5. Unity Colliders and Materials
Unity offers several primitives:BoxCollider, SphereCollider, and CapsuleCollider.
To make them bounce or slide:
- 1. Right-click in the Project window -> Create -> Physic Material.
- 2. Set Bounciness (0 to 1).
- 3. Set Dynamic Friction (sliding friction).
-
4.
Drag this material into the
Materialslot on your Collider component.
6. The CharacterController Component
Unity's built-inCharacterController 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).
7. Visual Learning: Unity Physics Debugging
In the Unity Editor, you often cannot see what the physics engine is doing.- 1. At the top of the Scene View, click the Gizmos button. This draws green outlines around all invisible Colliders.
-
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.Scalefrom(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*Radiusproperty, 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
11. Practice Exercises
- 1. What is the name of the C++ physics engine that runs under the hood of Unity 3D?
-
2.
Why is it a critical error to place
Rigidbody.AddForce()calls inside the standardUpdate()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()andFixedUpdate()loops. Provide a scenario where mixing them up results in a gameplay bug.
-
Q: How do you configure a Unity
PhysicMaterialto create a perfect, endlessly bouncing rubber ball? What properties are changed, and what are their values?
-
Q: A junior developer uses
Transform.Translateto 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 useRigidbody2D, 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 theRigidbody 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.