UNITY GAME DEVELOPMENT
High School • Lesson 10

Lesson 10: Trigger Zones

Make invisible areas that detect when the player enters them.

Easy45–60 minutesTriggers • Collider2D • OnTriggerEnter2D

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

  1. Explain the difference between a physical collision and a trigger.
  2. Create visible and invisible trigger zones.
  3. Configure Is Trigger on a Collider2D.
  4. Use OnTriggerEnter2D() and OnTriggerExit2D().
  5. Identify the player by its tag and perform an action when a zone activates.
  6. 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

4. Step-by-Step Instructions

Step 1 — Open or create a project

  1. Open Unity Hub and create a 2D project named PX_TriggerZone_L10_lastname, or use an existing project.
  2. Create a scene and save it as TriggerZoneScene.
  3. Keep the Scene and Game views visible.

Step 2 — Create the player

  1. In the Hierarchy, choose 2D Object → Sprites → Square.
  2. Name it Player. Set Position to (-3, 0, 0) and Scale to (0.7, 0.7, 1).
  3. Change its Sprite Renderer color to blue.
  4. Add Rigidbody 2D and Box Collider 2D.
  5. Set Rigidbody2D Body Type = Dynamic, Gravity Scale = 0, Collision Detection = Continuous, and freeze Rotation Z.
  6. Assign the built-in Player tag in the Inspector.

Step 3 — Create a visible trigger zone

  1. Create a second square sprite and name it TriggerZone.
  2. Set Position to (2, 0, 0) and Scale to (2, 2, 1).
  3. Change its color to green.
  4. Add Box Collider 2D and check Is Trigger.
Expected Scene: Top-Down Trigger Zone Player Trigger Zone Move right Enter area Green is a temporary visual guide. Later, the sprite is hidden while the trigger collider stays active.

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;
    }
}
Unity version note: This script uses Unity 6 and the legacy Input Manager or Both input handling. In Unity 2022/2023, use 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

  1. Save the script and return to Unity.
  2. Open Window → General → Console.
  3. Press Play and move the player into the green square using WASD or arrow keys.
  4. 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

  1. Select TriggerZone in the Hierarchy.
  2. Disable its Sprite Renderer component in the Inspector.
  3. Leave Box Collider 2D and the GameObject enabled.
  4. Press Play and move the player through the area again.
Expected result: The green graphic is invisible, but entry and exit messages still appear. Do not disable the entire GameObject or the collider.

Step 9 — Add an interactive checkpoint

  1. Create a new square sprite named Checkpoint.
  2. Set Position to (0, 2, 0) and Scale to (1.5, 1.5, 1).
  3. Add Box Collider 2D and enable Is Trigger.
  4. Create CheckpointZone.cs and attach it to Checkpoint.
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

  1. What is the difference between a trigger and a normal collider?
  2. Why does a trigger zone need a Collider2D?
  3. Why does the player need a Rigidbody2D in this project?
  4. How does OnTriggerEnter2D() know which object entered?
  5. Where are invisible trigger zones used in real games?

6. Programming Challenge

Complete the following enhancements:

Bonus: Display a message on screen using a Unity UI text element when the player enters a trigger.

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

Answer all five questions and select Check Answers.

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

  1. Completed Unity project or project folder, as instructed.
  2. Screenshot showing trigger zones in Scene view.
  3. Screenshot of Console entry and exit messages.
  4. PlayerMovement.cs, TriggerZoneDetector.cs, and CheckpointZone.cs.
  5. Completed experiment table and reflection answers.
Success Criteria: The player enters and exits zones, Console messages appear, invisible zones still work, and the checkpoint changes the player color.

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.