Bird
Raised Fist0
Unityframework~10 mins

3D colliders in Unity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - 3D colliders
Start: GameObject with Collider
Physics Engine checks collisions
Is Collider Trigger?
YesTrigger Event
OnTriggerEnter()
Collision Event
OnCollisionEnter() called
Respond to collision
End
This flow shows how Unity checks 3D colliders on objects, decides if they trigger events or collisions, and calls the right functions.
Execution Sample
Unity
void OnCollisionEnter(Collision collision) {
    Debug.Log("Hit " + collision.gameObject.name);
}

void OnTriggerEnter(Collider other) {
    Debug.Log("Triggered by " + other.gameObject.name);
}
This code runs when a 3D collider hits another or enters a trigger, printing the other object's name.
Execution Table
StepEvent TypeCollider StateFunction CalledOutput
1Collision detectedIsTrigger = falseOnCollisionEnterHit Enemy
2Trigger detectedIsTrigger = trueOnTriggerEnterTriggered by PowerUp
3Collision detectedIsTrigger = falseOnCollisionEnterHit Wall
4No collision or trigger---
💡 No more collisions or triggers detected, so no functions called.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
collision.gameObject.name-Enemy-WallWall
other.gameObject.name--PowerUp-PowerUp
Collider.IsTriggerfalsefalsetruefalsefalse
Key Moments - 2 Insights
Why does OnTriggerEnter run instead of OnCollisionEnter sometimes?
Because the collider's IsTrigger property is true, Unity calls OnTriggerEnter (see execution_table step 2). If IsTrigger is false, OnCollisionEnter runs (steps 1 and 3).
What happens if two colliders both have IsTrigger false and collide?
OnCollisionEnter is called on both objects involved in the collision, as shown in steps 1 and 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function is called at step 2?
AUpdate
BOnCollisionEnter
COnTriggerEnter
DStart
💡 Hint
Check the 'Function Called' column at step 2 in the execution_table.
At which step does the collider have IsTrigger set to true?
AStep 2
BStep 1
CStep 3
DNo step
💡 Hint
Look at the 'Collider State' column in the execution_table and variable_tracker.
If the collider's IsTrigger was false at step 2, what would happen?
AOnTriggerEnter would still be called
BOnCollisionEnter would be called instead
CNo function would be called
DThe game would crash
💡 Hint
Refer to the key_moments explanation about IsTrigger property and function calls.
Concept Snapshot
3D Colliders in Unity detect physical contact.
If IsTrigger is false, OnCollisionEnter runs on contact.
If IsTrigger is true, OnTriggerEnter runs when overlapping.
Use these to respond to hits or triggers in your game.
Set IsTrigger in the Collider component in Inspector.
Full Transcript
In Unity, 3D colliders are components that detect when objects touch or overlap. When two colliders meet, Unity checks if either is set as a trigger. If a collider's IsTrigger property is true, Unity calls OnTriggerEnter to handle the event. If IsTrigger is false, Unity calls OnCollisionEnter instead. This lets you decide if you want physical collisions or just trigger events. The example code shows how to print the name of the other object when these events happen. The execution table traces these events step by step, showing which function runs depending on the collider's trigger state. Understanding this helps you control game interactions clearly and avoid confusion about which event runs.

Practice

(1/5)
1. What is the primary purpose of a 3D collider in Unity?
easy
A. To detect when two objects touch or collide
B. To render 3D models on the screen
C. To control the animation of a character
D. To manage game audio effects

Solution

  1. Step 1: Understand the role of colliders

    3D colliders are used to detect physical interactions between objects in a game.
  2. Step 2: Differentiate from other components

    Rendering, animation, and audio are handled by other systems, not colliders.
  3. Final Answer:

    To detect when two objects touch or collide -> Option A
  4. Quick Check:

    3D collider = collision detection [OK]
Hint: Colliders detect contact, not visuals or sounds [OK]
Common Mistakes:
  • Confusing colliders with rendering components
  • Thinking colliders control animations
  • Assuming colliders handle audio
2. Which of the following is the correct way to add a BoxCollider component to a GameObject in Unity C# script?
easy
A. gameObject.AddComponent<BoxCollider>();
B. gameObject.Add<BoxCollider>();
C. gameObject.AddComponent(BoxCollider);
D. gameObject.AddComponent<BoxCollider>;

Solution

  1. Step 1: Recall the syntax for adding components

    In Unity C#, AddComponent is a generic method and requires angle brackets with the component type.
  2. Step 2: Check each option's syntax

    gameObject.AddComponent<BoxCollider>(); uses correct syntax with parentheses and angle brackets. Options A, B, and D have syntax errors.
  3. Final Answer:

    gameObject.AddComponent<BoxCollider>(); -> Option A
  4. Quick Check:

    AddComponent syntax = AddComponent<Type>() [OK]
Hint: Use AddComponent<Type>() with parentheses [OK]
Common Mistakes:
  • Omitting parentheses after AddComponent
  • Using wrong method name like Add
  • Missing angle brackets around type
3. Consider this Unity C# code snippet:
void OnCollisionEnter(Collision collision) {
    Debug.Log(collision.gameObject.name);
}
What will happen when this script is attached to a GameObject with a collider and Rigidbody, and it collides with another object named "Enemy"?
medium
A. A runtime error will occur
B. The console will print the name of the current GameObject
C. Nothing will print because OnCollisionEnter requires a trigger collider
D. The console will print "Enemy"

Solution

  1. Step 1: Understand OnCollisionEnter behavior

    This method is called when the GameObject's collider collides with another collider and at least one has a Rigidbody.
  2. Step 2: Analyze the Debug.Log statement

    It prints the name of the other object involved in the collision, accessed by collision.gameObject.name.
  3. Final Answer:

    The console will print "Enemy" -> Option D
  4. Quick Check:

    collision.gameObject.name = other object's name [OK]
Hint: collision.gameObject is the other object collided with [OK]
Common Mistakes:
  • Thinking it prints own GameObject name
  • Confusing collision with trigger events
  • Assuming runtime error without cause
4. You wrote this code to detect trigger events:
void OnTriggerEnter(Collider other) {
    Debug.Log("Triggered by " + other.name);
}
But the message never appears when objects overlap. What is the most likely reason?
medium
A. Debug.Log cannot print collider names
B. The method name should be OnCollisionEnter
C. The collider is not set as a trigger
D. The script is missing a Rigidbody component

Solution

  1. Step 1: Check trigger setup requirements

    OnTriggerEnter only works if at least one collider is marked as a trigger in the Unity Editor.
  2. Step 2: Evaluate other options

    Method name is correct for triggers, Rigidbody is recommended but not always required, and Debug.Log can print names.
  3. Final Answer:

    The collider is not set as a trigger -> Option C
  4. Quick Check:

    Trigger events need collider marked as trigger [OK]
Hint: Set collider's 'Is Trigger' box to true [OK]
Common Mistakes:
  • Using OnCollisionEnter instead of OnTriggerEnter
  • Forgetting to mark collider as trigger
  • Assuming Rigidbody is always mandatory
5. You want to create a game object that detects collisions but does not move physically when hit. Which setup is best in Unity?
hard
A. Add only a collider component without Rigidbody
B. Add a collider component and a Rigidbody with 'Is Kinematic' enabled
C. Add a Rigidbody without a collider component
D. Add a collider and Rigidbody with gravity enabled

Solution

  1. Step 1: Understand Rigidbody and collider interaction

    A collider alone detects collisions but does not generate collision events without a Rigidbody.
  2. Step 2: Use Rigidbody with 'Is Kinematic' to prevent movement

    Setting Rigidbody to kinematic allows collision detection without physical response (no movement).
  3. Final Answer:

    Add a collider component and a Rigidbody with 'Is Kinematic' enabled -> Option B
  4. Quick Check:

    Rigidbody kinematic = detect collisions, no physics move [OK]
Hint: Use Rigidbody kinematic to detect without moving [OK]
Common Mistakes:
  • Using collider without Rigidbody for collision events
  • Adding Rigidbody but forgetting to set kinematic
  • Enabling gravity causing unwanted movement