Lesson 9: Collision Playground
Lesson Overview
In this lesson, you will create a Collision Playground using the Unity game engine. Build a simple 2D environment containing walls, obstacles, and a player that can move around the scene. Learn how Unity's physics engine detects collisions and keeps objects from passing through solid surfaces. You will also write C# scripts that identify collisions and triggers.
1. Learning Objectives
- Explain collision detection in 2D games.
- Add Collider2D components to GameObjects.
- Explain the difference between Rigidbody2D and Collider2D.
- Create walls and obstacles that block player movement.
- Write C# code that detects collisions.
- Use the Unity Console to display collision messages.
- Test and troubleshoot collision behavior.
2. Key Vocabulary
| Term | Definition |
|---|---|
| Collider2D | Defines a 2D object's collision boundary. |
| Rigidbody2D | Enables an object to participate in Unity's 2D physics simulation. |
| Collision Detection | Determines when two objects touch or overlap. |
| Box Collider 2D | Rectangular collision boundary. |
| Circle Collider 2D | Circular collision boundary. |
| Trigger | A collider that detects overlaps without physically blocking objects. |
| OnCollisionEnter2D() | A method called when a qualifying physical collision contact begins. |
| OnTriggerEnter2D() | A method called when a qualifying trigger overlap begins. |
3. Materials Needed
- Computer with Unity Hub and Unity Editor.
- Existing Unity 2D project or a new 2D project.
- Visual Studio or another C# code editor.
- Basic knowledge of GameObjects and components.
- Experience with Rigidbody2D from Lesson 8.
4. Step-by-Step Instructions
Step 1 — Open or create a Unity project
- Open Unity Hub.
- Open your previous 2D project, or create one called
Lesson9_CollisionPlayground. - Open the Scene view.
- Save the scene as
CollisionPlayground.
Step 2 — Create the player
- In the Hierarchy, right-click and select 2D Object → Sprites → Square (wording may differ by Unity version).
- Rename the object
Player. - Set Position to X = 0, Y = 0, Z = 0.
- Set Scale to X = 0.7, Y = 0.7, Z = 1.
- Change its color to blue.
- Add Rigidbody 2D and Box Collider 2D components.
| Rigidbody2D Setting | Value |
|---|---|
| Body Type | Dynamic |
| Gravity Scale | 0 |
| Linear Damping | 0 |
| Freeze Rotation Z | Enabled |
| Collision Detection | Continuous |
Step 3 — Create four walls
- Create another Square sprite and rename it
WallTop. - Set its Position to (0, 4, 0) and Scale to (10, 0.5, 1).
- Change its color to gray and add a Box Collider 2D.
- Duplicate the wall to create the other three boundaries using the table below.
| Wall | Position X | Position Y | Scale X | Scale Y |
|---|---|---|---|---|
| WallTop | 0 | 4 | 10 | 0.5 |
| WallBottom | 0 | -4 | 10 | 0.5 |
| WallLeft | -5 | 0 | 0.5 | 8 |
| WallRight | 5 | 0 | 0.5 | 8 |
Do not add Rigidbody2D to the stationary walls; their Box Collider 2D components act as solid obstacles. Ensure Is Trigger is unchecked.
Step 4 — Add three obstacles
Create three square sprites and add a Box Collider 2D to each one.
| Object | Position (X, Y, Z) | Scale (X, Y, Z) | Color |
|---|---|---|---|
| Obstacle1 | (-2, 1, 0) | (1, 1, 1) | Red |
| Obstacle2 | (2, -1, 0) | (1, 2, 1) | Orange |
| Obstacle3 | (0, 2, 0) | (2, 0.5, 1) | Purple |
Step 5 — Program player movement
Create a C# script named PlayerCollisionMovement and attach it to Player.
using UnityEngine;
public class PlayerCollisionMovement : MonoBehaviour
{
public float moveSpeed = 5f;
private Rigidbody2D rb;
private Vector2 movement;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
movement = new Vector2(moveX, moveY).normalized;
}
void FixedUpdate()
{
rb.linearVelocity = movement * moveSpeed;
}
}
Code explanation: moveSpeed controls speed; Input.GetAxisRaw() reads keyboard controls; .normalized avoids faster diagonal movement; FixedUpdate() runs on the physics update schedule; and rb.linearVelocity sets the player's velocity.
linearVelocity property. In Unity 2022/2023, replace it with velocity. For Unity 6, enable the legacy Input Manager or Both under Edit → Project Settings → Player → Active Input Handling so Input.GetAxisRaw() works.Step 6 — Test collisions
- Save your script and return to Unity.
- Press Play and click inside the Game view.
- Use W/A/S/D or arrow keys to move.
- Move into each boundary and obstacle.
Step 7 — Detect collisions with C#
Create a second C# script named CollisionDetector and attach it to Player.
using UnityEngine;
public class CollisionDetector : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Player collided with: "
+ collision.gameObject.name);
}
private void OnCollisionExit2D(Collision2D collision)
{
Debug.Log("Player stopped touching: "
+ collision.gameObject.name);
}
}
Open Window → General → Console, press Play, and bump into obstacles. You should see messages similar to these:
Player collided with: WallTop
Player stopped touching: WallTop
Player collided with: Obstacle1
Remember: OnCollisionEnter2D() runs when physical contact begins; it does not repeat every frame while the player remains in contact.
Step 8 — Experiment with triggers
- Select
Obstacle3. - In its Box Collider 2D component, check Is Trigger.
- Create a C# script named
TriggerDetectorand attach it to Player.
using UnityEngine;
public class TriggerDetector : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Player entered trigger: " + other.name);
}
private void OnTriggerExit2D(Collider2D other)
{
Debug.Log("Player exited trigger: " + other.name);
}
}
Press Play and move through Obstacle3. The player can pass through this obstacle, but Unity still detects the overlap. Triggers can be used for collectibles, checkpoints, doors, and similar interactions.
5. Student Activity — Collision Experiments
Predict the outcome, test it in Unity, and record your observations. Restore the original settings after each experiment.
| Experiment | Prediction | Actual Result |
|---|---|---|
| Remove Player's Box Collider 2D | ||
| Disable Is Trigger on Obstacle3 | ||
| Enable Is Trigger on a wall | ||
| Change Player speed to 10 | ||
| Disable Freeze Rotation Z |
Reflection Questions
- What happens when the player touches a wall?
- What happens when a Collider2D is removed?
- What is the difference between a collider and a trigger?
- Why does the player need a Rigidbody2D?
- How can collision detection improve a video game?
6. Programming Challenge
Improve your Collision Playground. Check each task as you complete it.
7. Common Problems and Troubleshooting
| Problem | Possible Solution |
|---|---|
| Player moves through walls | Check colliders; make sure Is Trigger is unchecked for solid walls. |
| Player does not move | Check the movement script, input handling settings, and Console errors. |
| Player rotates on impact | Enable Freeze Rotation Z. |
| No collision message appears | Confirm CollisionDetector is attached and physics components are configured correctly. |
| Trigger message does not appear | Check Is Trigger on the obstacle and Rigidbody2D on the Player. |
linearVelocity compiler error | Use velocity in older Unity versions. |
8. Knowledge Check
Choose the best answer for each question. Select Check Answers to see your score and explanations.
9. Grading Rubric
| Criteria | Points |
|---|---|
| Player created with Rigidbody2D and Box Collider 2D | 15 |
| Four walls configured correctly | 15 |
| Three obstacles with colliders | 15 |
| Player movement script works | 20 |
| Collision detection messages work | 15 |
| Trigger detection experiment completed | 10 |
| Written questions and experiment table completed | 10 |
| Total | 100 |
10. Submission Requirements
- Completed Unity project or project folder, as directed by your teacher.
- Screenshot of the Collision Playground showing the player, walls, and obstacles.
- Screenshot of the Unity Console displaying collision messages.
- Copies of
PlayerCollisionMovement.cs,CollisionDetector.cs, andTriggerDetector.cs. - Completed experiment table and reflection questions.
11. Lesson Summary
You learned to create a 2D environment using Unity's collision detection system. Rigidbody2D and Collider2D components provide physical interactions, while C# event methods detect contact and overlap. These skills form the foundation for walls, mazes, collectibles, obstacles, enemies, and checkpoints.