CHAPTER 07
Beginner
Physics and Collisions
Updated: May 16, 2026
30 min read
# CHAPTER 7
Physics and Collisions
1. Introduction
If you build a maze and tell the player to navigate it, you expect the walls to stop them. Without collision detection, the player will simply ghost through the walls and walk off the edge of the world. Calculating collisions between 3D objects is intensely complex, but Unity's built-in physics engine (Nvidia PhysX) abstracts that complexity into a few simple components. In this chapter, we will master Physics and Collisions. We will explore the interplay betweenColliders and Rigidbodies, understand the critical distinction between a hard collision and an invisible Trigger, and learn how to run C# code when two objects touch.
2. Learning Objectives
By the end of this chapter, you will be able to:- Explain the relationship between a Collider and a Rigidbody.
- Differentiate between a physical Collision and an overlapping Trigger.
-
Use
OnCollisionEnterto detect solid impacts.
-
Use
OnTriggerEnterto detect invisible zones (like coin pickups).
- Optimize collision logic using Unity Tags.
3. The Collider Component
A Collider is an invisible mathematical boundary wrapped around your 3D mesh.- A 3D model (the Mesh) is just for the player's eyes. The physics engine only cares about the Collider.
- Box Collider: Perfect for walls, crates, and floors. Extremely fast to calculate.
- Sphere Collider: Perfect for balls and projectiles.
- Capsule Collider: A cylinder with rounded ends. The industry standard for humanoid players so they slide up stairs smoothly.
4. The Rigidbody (Making it Dynamic)
If you place two cubes in the sky (both with Box Colliders), nothing happens. They just sit there. To make gravity pull a cube down, and to force the engine to calculate a bounce when it hits the floor, you must attach a Rigidbody component to at least one of the objects.- A Collider makes an object solid.
- A Rigidbody hands the object over to the physics simulation.
5. Solid Collisions (OnCollisionEnter)
When a Rigidbody hits a solid Collider (like a car hitting a brick wall), the physics engine physically pushes them apart. You can run C# code the exact frame they touch using a special Unity method.
csharp
6. Triggers (OnTriggerEnter)
What if you don't want a solid crash? What if you want a toxic gas cloud, or a floating coin? You want the player to pass *through* it, but you still want the code to know they touched it.- 1. Click the Collider component on the Coin.
- 2. Check the box that says Is Trigger. The object instantly becomes a ghost!
- 3. To detect the overlap in code, you use a different method:
csharp
7. Unity Tags (Identification)
In the code above, we usedtag == "Coin". Tags are simple text labels you assign to GameObjects at the very top of the Inspector.
- If you don't use Tags, your player script will think it collected a coin every time it accidentally touches the floor or a wall!
- Always tag your important objects: "Player", "Enemy", "Coin", "Wall".
8. Visual Learning: The Physics Matrix
txt
9. Best Practices
- Never move Static Colliders: If a wall has a Collider but no Rigidbody, Unity assumes it will NEVER move, and optimizes it heavily. If you write a script to move that wall, Unity will panic and recalculate the entire level's physics, causing massive lag. If a wall needs to move (like a sliding door), you *must* give it a Rigidbody and check the "Is Kinematic" box!
10. Common Mistakes
-
Forgetting the Rigidbody on Triggers: For
OnTriggerEnterto fire, at least one of the two objects involved MUST have a Rigidbody attached. If your player is walking through coins and the code isn't firing, ensure your Player has a Rigidbody!
11. Mini Project: The Coin Collector
Objective: Build a simple collection game using Triggers and Tags.- 1. Create a Cube (The Floor) and a Capsule (The Player). Give the Player a Rigidbody and a Movement script.
- 2. Tag the Player as "Player" in the top left of the Inspector.
- 3. Create a small Sphere (The Coin). Check the Is Trigger box on its Sphere Collider.
- 4. Go to the top of the Inspector, click the Tag dropdown -> Add Tag. Add a tag named "Coin". Apply it to the Sphere.
-
5.
Create a script named
CoinPickupand attach it to the Coin.
csharp
- 6. Press Play and walk the player into the coin. It will vanish!
12. Practice Exercises
- 1. What checkbox must you tick on a Collider component to make it an invisible zone that players can walk through?
- 2. Why is the Unity Tag system critical when writing collision code?
13. MCQs with Answers
Question 1
You want to execute C# code the exact moment a physics-driven bowling ball smashes into a solid bowling pin. Which Unity method must you use?
Question 2
Two objects touch, but the collision/trigger event fails to fire in the C# script. Both objects have Colliders. What is the most likely missing component?
14. Interview Questions
- Q: Explain the mechanical difference between a solid Collision and a Trigger in Unity. Provide a real-world gameplay example for each scenario.
- Q: Explain the rule of moving Static Colliders. Why does moving a standard Collider without a Rigidbody cause immense CPU performance drops?
-
Q: A developer writes
if (hit.gameObject.name == "Enemy_01")inside a collision check. Why is relying on the GameObject's name a terrible practice, and what system should they use instead?
15. FAQs
Q: Can one object have a solid collision AND a trigger? A: Yes! This is very common. An enemy might have a solid BoxCollider for its body (so bullets hit it), and a massive invisible SphereCollider (set to Is Trigger) around it to act as its "Line of Sight" vision cone.16. Summary
In Chapter 7, we defined the physical rules of our universe. We learned thatColliders give objects solid boundaries, while Rigidbodies subject them to gravity and force. We mastered the critical difference between hard, bouncing collisions (OnCollisionEnter) and invisible ghost zones (OnTriggerEnter). By utilizing Unity's Tag system, we wrote intelligent code that can differentiate between a bullet hitting a wall and a player picking up a coin.