Lesson 10: Trigger Zones
Make invisible areas that detect when the player enters them.
Lesson Overview
In this lesson, students create invisible trigger zones in Unity. These zones detect when the player enters or exits an area without physically blocking movement. Students use Collider2D components and C# to show messages, change player color, and activate a checkpoint.
1. Learning Objectives
- Explain the difference between a physical collision and a trigger.
- Create visible and invisible trigger zones.
- Configure Is Trigger on a Collider2D.
- Use
OnTriggerEnter2D()andOnTriggerExit2D(). - Identify the player by its tag and perform an action when a zone activates.
- Test and troubleshoot trigger behavior.
2. Key Vocabulary
| Term | Definition |
|---|---|
| Trigger Zone | An area that detects when another object enters or exits. |
| Collider2D | Component defining a 2D object's collision or trigger boundary. |
| Is Trigger | Allows overlap detection rather than solid collision response. |
| OnTriggerEnter2D() | Called when a qualifying 2D trigger overlap begins. |
| OnTriggerExit2D() | Called when a qualifying 2D trigger overlap ends. |
| GameObject | An object within a Unity scene. |
| Tag | A label used to identify a GameObject. |
| Debug.Log() | Writes a message to the Unity Console. |
3. Materials Needed
- Unity Hub and Unity Editor with a 2D project
- A C# script editor
- Player object with Rigidbody2D and Collider2D
- Familiarity with Lessons 8–9: physics and collisions
4. Step-by-Step Instructions
Step 1 — Open or create a project
- Open Unity Hub and create a 2D project named
PX_TriggerZone_L10_lastname, or use an existing project. - Create a scene and save it as
TriggerZoneScene. - Keep the Scene and Game views visible.
Step 2 — Create the player
- In the Hierarchy, choose 2D Object → Sprites → Square.
- Name it
Player. Set Position to (-3, 0, 0) and Scale to (0.7, 0.7, 1). - Change its Sprite Renderer color to blue.
- Add Rigidbody 2D and Box Collider 2D.
- Set Rigidbody2D Body Type = Dynamic, Gravity Scale = 0, Collision Detection = Continuous, and freeze Rotation Z.
- Assign the built-in Player tag in the Inspector.
Step 3 — Create a visible trigger zone
- Create a second square sprite and name it
TriggerZone. - Set Position to (2, 0, 0) and Scale to (2, 2, 1).
- Change its color to green.
- Add Box Collider 2D and check Is Trigger.
Step 4 — Add player movement
Create PlayerMovement.cs and attach it to Player.
using UnityEngine;
public class PlayerMovement : 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;
}
}
rb.velocity instead of rb.linearVelocity. Check Edit → Project Settings → Player → Active Input Handling if input does not work.Step 5 — Detect entry into the zone
Create TriggerZoneDetector.cs and attach it to TriggerZone.
using UnityEngine;
public class TriggerZoneDetector : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Player entered the trigger zone!");
}
}
}
OnTriggerEnter2D detects entry, other identifies the entering collider, CompareTag verifies it is the player, and Debug.Log reports the event.
Step 6 — Test the trigger
- Save the script and return to Unity.
- Open Window → General → Console.
- Press Play and move the player into the green square using WASD or arrow keys.
- Confirm the player passes through and the Console prints:
Player entered the trigger zone!
Step 7 — Detect when the player leaves
Replace the contents of TriggerZoneDetector.cs with this complete version:
using UnityEngine;
public class TriggerZoneDetector : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Player entered the trigger zone!");
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Player exited the trigger zone!");
}
}
}
Test entering and exiting several times. The Console should report both events.
Step 8 — Make the trigger invisible
- Select
TriggerZonein the Hierarchy. - Disable its Sprite Renderer component in the Inspector.
- Leave Box Collider 2D and the GameObject enabled.
- Press Play and move the player through the area again.
Step 9 — Add an interactive checkpoint
- Create a new square sprite named
Checkpoint. - Set Position to (0, 2, 0) and Scale to (1.5, 1.5, 1).
- Add Box Collider 2D and enable Is Trigger.
- Create
CheckpointZone.csand attach it toCheckpoint.
using UnityEngine;
public class CheckpointZone : MonoBehaviour
{
private bool activated = false;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") && !activated)
{
activated = true;
Debug.Log("Checkpoint activated!");
SpriteRenderer playerSprite =
other.GetComponent<SpriteRenderer>();
if (playerSprite != null)
{
playerSprite.color = Color.yellow;
}
}
}
}
When the player enters, the Console prints Checkpoint activated! and the player turns yellow. The activated variable prevents repeated activation during that play session.
5. Student Activity — Trigger Experiments
Predict the result of each change, test in Play mode, and record your observations. Restore original settings afterward.
| Experiment | Prediction | Actual Observation |
|---|---|---|
| Enable Is Trigger | ||
| Disable Is Trigger | ||
| Disable trigger Sprite Renderer | ||
| Remove Player Rigidbody2D | ||
| Increase trigger zone size |
Reflection Questions
- What is the difference between a trigger and a normal collider?
- Why does a trigger zone need a Collider2D?
- Why does the player need a Rigidbody2D in this project?
- How does
OnTriggerEnter2D()know which object entered? - Where are invisible trigger zones used in real games?
6. Programming Challenge
Complete the following enhancements:
7. Troubleshooting
| Problem | Solution |
|---|---|
| Trigger does not activate | Verify Is Trigger is enabled and the Collider2D is active. |
| No Console message | Make sure the script is attached and there are no compile errors. |
| Player passes through with no event | Check Rigidbody2D, Collider2D, and the Player tag. |
| Invisible trigger stopped working | Disable only Sprite Renderer, not the GameObject or collider. |
| Player cannot move | Review input settings, movement script, and Console. |
linearVelocity compile error |
Use velocity in older Unity versions. |
Important: At least one of the interacting colliders must have a Rigidbody2D for Unity to report 2D trigger events under this setup.
8. Knowledge Check
9. Grading Rubric
| Criteria | Points |
|---|---|
| Player configured with Rigidbody2D and Collider2D | 15 |
| Trigger zone configured correctly | 15 |
| Player movement script works | 15 |
| OnTriggerEnter2D detects entry | 15 |
| OnTriggerExit2D detects exit | 10 |
| Invisible trigger operates correctly | 10 |
| Checkpoint changes player color | 10 |
| Experiment table and reflection questions | 10 |
| Total | 100 |
10. Submission Requirements
- Completed Unity project or project folder, as instructed.
- Screenshot showing trigger zones in Scene view.
- Screenshot of Console entry and exit messages.
PlayerMovement.cs,TriggerZoneDetector.cs, andCheckpointZone.cs.- Completed experiment table and reflection answers.
11. Lesson Summary
Students learned to detect area overlaps in a Unity 2D scene using Collider2D triggers, OnTriggerEnter2D(), and OnTriggerExit2D(). Trigger zones can implement checkpoints, secret areas, collectibles, doors, and level transitions.
Next Lesson — Lesson 11: Collect Objects and Keep Score. Create collectible objects, remove them when collected, and display a score.