Detect overlaps with triggers
site/media/lessons/learn-triggers.lesson.ts (5 steps) A trigger (sensor) is a collider that detects overlaps without physically blocking. Mark a
collider isSensor: true and the engine calls onTriggerEnter / onTriggerExit on the
overlapping entities’ GameComponents. This scene has a fixed sensor zone and a Mover
cube that sweeps through it — the zone lights up green while the cube is inside.
-
Open the scene. The scene is already open — if you followed the link above, the triggers scene is loaded and rendering (in the local dev editor:
VGAI_PROJECT=examples/learn-triggers npm run dev): a red sensor zone and a blue mover cube beside it.
A sensor zone (isSensor collider) + a mover cube. -
Enter play — the Mover heads toward the zone. The
Moverdrives a dynamic body across the scene; the zone is still red (no overlap yet).
No overlap yet — the zone stays red. -
The Mover enters —
onTriggerEnterfires. Rapier reports the sensor overlap; the engine’s trigger dispatch callsTriggerZone.onTriggerEnter, which turns the zone green.
Overlap → onTriggerEnter → the zone turns green. -
The Mover exits the far side —
onTriggerExitfires. As the cube leaves,onTriggerExitruns and the zone returns to red.
Overlap ends → onTriggerExit → back to red. -
Stop play mode. Press Stop (or
Escape).
// learn-triggers/components.ts — behavior lives in the trigger callbacksexport class TriggerZone extends GameComponent { update() {} // base update is abstract onTriggerEnter(_other, _ctx) { /* turn green */ } onTriggerExit(_other, _ctx) { /* turn red */ }}Recap
New functionality
- Made a collider a sensor (isSensor)
- Reacted to overlaps with onTriggerEnter / onTriggerExit
- Watched a zone turn green while a body was inside
New concepts & skills
- A sensor detects overlaps without blocking
- The engine dispatches onTriggerEnter/onTriggerExit to the entities' GameComponents
- A fixed sensor needs a dynamic (not kinematic) body to generate events by default
Next lesson → Manual: collisions & triggers
On Your Own!
Extend what you built:
- Make the zone count how many times it was entered and show it in a HUD
- Use onTriggerEnter as a pickup: hide the mover and increment a score
- Add a second sensor with a different reaction (e.g., a damage zone)