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
Detecting Collisions and Triggers in Unity 2D
📖 Scenario: You are making a simple 2D game in Unity where a player can bump into walls and collect coins. You want to detect when the player hits a wall and when the player collects a coin.
🎯 Goal: Build a Unity C# script that uses OnCollisionEnter2D to detect hitting walls and OnTriggerEnter2D to detect collecting coins.
📋 What You'll Learn
Create a C# script with a OnCollisionEnter2D method that detects collisions with walls
Create a OnTriggerEnter2D method that detects triggers with coins
Use CompareTag to check if the collided object is a wall or a coin
Print messages to the console when collisions or triggers happen
💡 Why This Matters
🌍 Real World
Collision and trigger detection is essential in games to respond to player interactions with the environment and objects.
💼 Career
Game developers use these Unity methods daily to create interactive and responsive gameplay experiences.
Progress0 / 4 steps
1
Create the PlayerCollision script with empty methods
Create a C# script called PlayerCollision with empty methods OnCollisionEnter2D and OnTriggerEnter2D that take a parameter Collision2D collision and Collider2D other respectively.
Unity
Hint
Start by writing a public class named PlayerCollision. Inside it, add two methods: OnCollisionEnter2D with a Collision2D parameter and OnTriggerEnter2D with a Collider2D parameter. Leave the methods empty for now.
2
Add tag checks for walls and coins
Inside OnCollisionEnter2D, add an if statement that checks if collision.gameObject.CompareTag("Wall"). Inside OnTriggerEnter2D, add an if statement that checks if other.gameObject.CompareTag("Coin").
Unity
Hint
Use CompareTag on the gameObject inside the collision or trigger parameter to check if it matches "Wall" or "Coin".
3
Add print statements inside the tag checks
Inside the if block for walls in OnCollisionEnter2D, add Debug.Log("Player hit a wall!"). Inside the if block for coins in OnTriggerEnter2D, add Debug.Log("Player collected a coin!").
Unity
Hint
Use Debug.Log inside each if block to print the messages exactly as shown.
4
Test the script by printing messages on collision and trigger
Run the game and make sure when the player hits a wall, the console shows Player hit a wall! and when the player collects a coin, the console shows Player collected a coin!. Write Debug.Log statements as in the script and confirm the output.
Unity
Hint
Make sure your Unity console shows the exact messages when collisions and triggers happen.
Practice
(1/5)
1. Which Unity method is called when two solid objects with Rigidbody2D and Collider2D components collide physically?
easy
A. OnTriggerEnter2D
B. OnCollisionEnter2D
C. OnCollisionExit2D
D. OnTriggerExit2D
Solution
Step 1: Understand collision detection methods
OnCollisionEnter2D is called when two solid objects collide physically, both having Rigidbody2D and Collider2D components without trigger enabled.
Step 2: Differentiate triggers from collisions
OnTriggerEnter2D is called when an object enters a trigger collider, which is a collider set as a trigger, not a solid collision.
Final Answer:
OnCollisionEnter2D -> Option B
Quick Check:
Physical collision = OnCollisionEnter2D [OK]
Hint: Solid collisions use OnCollisionEnter2D, triggers use OnTriggerEnter2D [OK]
Common Mistakes:
Confusing trigger events with collision events
Using OnTriggerEnter2D for solid collisions
Assuming OnCollisionEnter2D works with trigger colliders
2. Which of the following is the correct method signature for detecting trigger entry in a 2D Unity script?
easy
A. void OnTriggerEnter(Collider other)
B. void OnCollisionEnter2D(Collider2D other)
C. void OnTriggerEnter2D(Collider other)
D. void OnTriggerEnter2D(Collider2D other)
Solution
Step 1: Identify correct method name and parameter type
The method for trigger detection in 2D physics is OnTriggerEnter2D and it takes a Collider2D parameter.
Step 2: Check parameter types and method names
void OnTriggerEnter2D(Collider2D other) matches the exact signature: void OnTriggerEnter2D(Collider2D other). Other options have wrong parameter types or method names.
Final Answer:
void OnTriggerEnter2D(Collider2D other) -> Option D
Hint: Use OnTriggerEnter2D with Collider2D parameter for 2D triggers [OK]
Common Mistakes:
Using 3D Collider instead of Collider2D
Mixing OnTriggerEnter with OnTriggerEnter2D
Wrong parameter type in method signature
3. What will happen when the following Unity script is attached to a GameObject with a Collider2D set as a trigger, and another Rigidbody2D object enters it?
B. The parameter type should be Collider, not Collider2D.
C. The method name should be OnTriggerEnter2D, not OnTriggerEnter.
D. The method should be private, not public.
Solution
Step 1: Check method name correctness
The method for 2D trigger detection must be named exactly OnTriggerEnter2D. The code uses OnTriggerEnter, which is for 3D physics.
Step 2: Confirm parameter type and method signature
The parameter Collider2D is correct for 2D triggers, but the method name mismatch prevents Unity from calling it.
Final Answer:
The method name should be OnTriggerEnter2D, not OnTriggerEnter. -> Option C
Quick Check:
Correct method name = OnTriggerEnter2D [OK]
Hint: Use exact method names for 2D events: OnTriggerEnter2D [OK]
Common Mistakes:
Using 3D method names for 2D physics
Wrong parameter types
Expecting return values from event methods
5. You want to detect when a player touches a collectible item in a 2D game without blocking movement. Which setup and method should you use to detect the event correctly?
hard
A. Set the collectible's Collider2D as a trigger and use OnTriggerEnter2D in the player's script.
B. Set the collectible's Collider2D as non-trigger and use OnCollisionEnter2D in the player's script.
C. Set both player and collectible colliders as triggers and use OnCollisionEnter2D.
D. Use OnTriggerEnter with 3D colliders on both objects.
Solution
Step 1: Choose collider settings for collectibles
To allow the player to pass through collectibles without blocking, the collectible's Collider2D must be set as a trigger.
Step 2: Select correct detection method
Use OnTriggerEnter2D in the player's script to detect when the player enters the collectible's trigger collider.
Final Answer:
Set the collectible's Collider2D as a trigger and use OnTriggerEnter2D in the player's script. -> Option A