Collisions and triggers
Updated
Most game logic starts with “when this touches that”: a ball crosses the goal line, the player steps on a pressure plate, a crate smashes into the floor hard enough to break. Physics reports these moments as collision events. This page explains the four events, the data they carry, how to receive them in a script, and how to build invisible trigger volumes with sensor colliders.
Before you start
Section titled “Before you start”- You have entities with colliders, as described in Rigid bodies and colliders.
- You know how a script listens to a signal with
on(signal, callback)and comparesevent.eventTypewithEventTypeconstants. See Scripting and Events and signals. - Physics, and therefore every event on this page, runs in play only.
Contacts and overlaps
Section titled “Contacts and overlaps”Two colliders that meet do one of two things:
- Collide. They push each other apart: a solid contact. A ball lands on the floor; a crate hits a wall.
- Intersect. They pass through each other and only report the overlap. This happens when either collider is a sensor, or when their physics layers are set to intersect.
Each kind has an event when it begins and one when it ends:
| eventType | Constant | When |
|---|---|---|
Physics |
Event |
A solid contact began. |
Physics |
Event |
It ended. |
Physics |
Event |
An overlap began: either collider is a sensor, or the two layers intersect. |
Physics |
Event |
It ended. |
Each touching pair sends one enter and then the exit of the same kind, also when the other collider is removed while touching. Two layers set to ignore each other send nothing at all.
Receiving the events
Section titled “Receiving the events”Components never call your script directly. A collider hands its events to every Signal Emitter on its entity, and the Signal Emitter puts them on the event bus under its signal. Your script listens to that signal.
- Add a Signal Emitter to the entity whose contacts you care about, and give it a
signal, for examplegoal. - In a script, call
on('goal', (event) => { … }). - Inside, check
event.eventTypeagainst anEventType.PhysicsColliderconstant.
Every event the listener receives has this shape:
| Field | Description |
|---|---|
event. |
The entity carrying the Signal Emitter: the one whose collider was touched. |
event. |
One of the four strings above. |
event.data |
The details, listed below. |
event.data carries:
| Field | Type | Description |
|---|---|---|
otherEntityId |
string, optional | The other entity, when the other collider belongs to one. Absent for stray world geometry. |
contact |
{ x, y, z }, optional |
The world-space contact point. Only on collision-, and only when a contact point was produced. |
sceneId |
string, optional | The scene this entity belongs to. |
otherSceneId |
string, optional | The scene the other entity belongs to. Entity ids can repeat across loaded scenes, so use it when several scenes are loaded. |
collider |
number, optional | The live handle of this collider. |
other |
number, optional | The live handle of the other collider. With collider, it tells apart the different shapes of a compound body. |
Filtering events
Section titled “Filtering events”A Signal Emitter forwards every event of its entity by default. Its eventFilter prop, a list of eventType strings, lets only those through. For example, a Signal Emitter with signal set to crate/hit and eventFilter set to PhysicsCollider::collision-enter sends only the start of solid contacts, so the script does not even need to check the type. An entity may carry several Signal Emitters with different signals and filters.
The project’s physics settings can also switch off collision reports or intersection reports for the whole app. Both are on by default.
Sensors and trigger volumes
Section titled “Sensors and trigger volumes”A sensor is a collider with its sensor prop on. It reports overlaps but never pushes anything, and nothing pushes it. Sensors are how you make zones:
- a goal line, a finish line, a checkpoint;
- a pressure plate or a doorway that opens a door;
- a “kill zone” under the level that respawns whatever falls in;
- a room that changes the music when the player enters.
To build a trigger volume:
- Add a Group where the zone should be.
- Add a Box Collider to it with a
sizecovering the zone, and turn onsensor. - Add a Signal Emitter with a signal, such as
checkpoint. - Listen for
IntersectionEnterandIntersectionExitin a script.
A group has no visible mesh, so the zone is invisible in play. Without a Rigid Body the group gets a fixed body, which is what a trigger needs.
A sensor never blocks rays: PhysicsRaycaster passes through sensors. A sensor on a fixed body also sees kinematic bodies, such as a Character Controller walking through it.
Characters
Section titled “Characters”A Character Controller’s capsule sends collider events too, on a Signal Emitter on the character’s entity:
intersection-enterandintersection-exitfor the triggers it walks through;collision-enterandcollision-exitfor what it rests on, such as the floor.
A wall or a box the character walks into is not a contact: the capsule slides along it and stops just short of it, so no event is sent. To detect what a character bumps into, put a slightly larger sensor around it, or test ahead of it with a ray.
Asking instead of waiting
Section titled “Asking instead of waiting”Events tell you when something happens. When you need to ask at a given moment (“is there ground under me?”, “can the guard see the player?”), cast a ray against the colliders with PhysicsRaycaster. It tests colliders only, never hits sensors, and its collisionLayers option limits it to some physics layers. See Picking and raycasts.
Examples
Section titled “Examples”1. Score a goal
Section titled “1. Score a goal”A sensor box behind the goal line reports the ball crossing it.
#pragma lifecycle(startup)
let score = 0;
function startup() { on('goal', (event) => { if (event.eventType !== EventType.PhysicsCollider.IntersectionEnter) return; const other = findEntity(event.data.otherEntityId || ''); if (!other || other.name !== 'Ball') return;
score += 1; log('Goal! Score:', score); emit('score/changed', { score }); });}A sensor Box Collider in the goal reports the ball passing into it with PhysicsCollider::intersection-enter, and a script counts the goals.
Drag the ball into the blue goal and let go. Scroll to zoom.
- The goal’s collider is a sensor, so the ball passes through it and the goal reports an intersection, not a collision.
otherEntityIdis the entity that entered.findEntity(… || '')handles the case where it is absent.- Checking
other.nameignores anything else that wanders in, such as a player. emitpasses the new score to other scripts, for example one that updates a UI label.
2. A checkpoint for the player
Section titled “2. A checkpoint for the player”When the player walks through a checkpoint, it becomes the new respawn point.
#pragma lifecycle(startup)
let player;let respawn = null;
// Each checkpoint is a group with a sensor Box Collider and a Signal Emitter with the signal 'checkpoint'.// The Player entity has a Character Controller.function startup() { player = findEntity('Player');
on('checkpoint', (event) => { if (event.eventType !== EventType.PhysicsCollider.IntersectionEnter) return; if (event.data.otherEntityId !== player.entityId) return;
const checkpoint = findEntity(event.entityId); respawn = checkpoint.position.clone(); log('Checkpoint reached:', checkpoint.name); });
on('player/died', () => { if (respawn) player.getComponent(CharacterControllerComponent).teleport(respawn); });}Two gates are sensor colliders on the signal checkpoint: walking through one makes it the respawn point, and Respawn teleports the player's Character Controller back to the last one reached.
Click the scene, walk through a green gate with WASD, wander off, then press Respawn to return to the gate. Drag to look around.
- Every checkpoint uses the same signal,
checkpoint;event.entityIdsays which one fired. - The fixed sensor sees the Character Controller’s kinematic capsule, so walking through it sends
IntersectionEnter. - Comparing
otherEntityIdwithplayer.entityIdignores crates or other characters. - On
player/died, emitted by another script, the Character Controller’steleportputs the player on the last checkpoint.
3. Sparks where something hits hard
Section titled “3. Sparks where something hits hard”A crate shows sparks at the contact point when it hits something fast enough.
#pragma lifecycle(startup, update)
const HARD_HIT = 4; // metres per second
let body;let lastSpeed = 0;
// On a dynamic Crate with a Rigid Body, a Box Collider and a Signal Emitter with the signal 'crate'.// A disabled entity named 'Sparks' in the scene is the effect to copy.function startup() { body = self.parent.getComponent(PhysicsRigidBodyComponent);
on('crate', (event) => { if (event.entityId !== self.parent.entityId) return; if (event.eventType !== EventType.PhysicsCollider.CollisionEnter) return; if (lastSpeed < HARD_HIT || !event.data.contact) return;
const sparks = spawn('Sparks', { position: event.data.contact, enabled: true }); delay(() => destroy(sparks), 1000); });}
function update() { const v = body.linearVelocity(); lastSpeed = Math.hypot(v.x, v.y, v.z);}A falling crate copies a disabled Sparks entity at the contact point with spawn whenever it lands faster than 4 m/s, and destroys the copy a second later.
Watch the crate land in a burst of sparks. Grab it, lift it high and let it drop to see them again. Drag elsewhere to orbit the camera.
updatekeeps the crate’s speed from the previous frame. At the moment of impact the body has already slowed, so the speed just before is the one that matters.- If several crates share the signal
crate, theentityIdcheck keeps only this crate’s own events. event.data.contactis the world point where the contact began. It is only oncollision-enter, and can be missing, hence the check.spawncopies the Sparks entity at that point;delaydestroys the copy a second later.
4. A pressure plate that counts what stands on it
Section titled “4. A pressure plate that counts what stands on it”A door stays open while at least one body is on the plate.
#pragma lifecycle(startup)
const onPlate = new Set();
// The Plate is a group with a thin sensor Box Collider and a Signal Emitter with the signal 'plate'.function startup() { on('plate', (event) => { const id = event.data.otherEntityId; if (!id) return;
const before = onPlate.size; if (event.eventType === EventType.PhysicsCollider.IntersectionEnter) onPlate.add(id); if (event.eventType === EventType.PhysicsCollider.IntersectionExit) onPlate.delete(id);
if (before === 0 && onPlate.size > 0) emit('door/open'); if (before > 0 && onPlate.size === 0) emit('door/close'); });}A sensor plate counts the bodies on it: the first one in opens the door, the last one out closes it again, each through an Enable On Signal on the door.
Drag a crate onto the green plate to open the door; add the other one, then take both off to close it. Drag elsewhere to orbit the camera.
- A
Setholds the ids of the bodies on the plate, so the same body never counts twice. - Every enter is followed by an exit of the same kind, so the set always returns to empty.
- The door is told only when the count crosses zero, not on every step on or off.
- Another script, or an Enable on Signal component, reacts to
door/openanddoor/close.
5. Jump only while touching the ground
Section titled “5. Jump only while touching the ground”A ball may jump only while it rests on something. Counting its solid contacts tells the script whether it is grounded.
#pragma lifecycle(startup, update)
let body;let contacts = 0;
// On a dynamic Ball with a Rigid Body, a Sphere Collider and a Signal Emitter with the signal 'ball'.function startup() { body = self.parent.getComponent(PhysicsRigidBodyComponent);
on('ball', (event) => { if (event.entityId !== self.parent.entityId) return; if (event.eventType === EventType.PhysicsCollider.CollisionEnter) contacts += 1; if (event.eventType === EventType.PhysicsCollider.CollisionExit) contacts = Math.max(0, contacts - 1); });}
function update() { if (contacts > 0 && Input.keyboard.isKeyReleased(Key.Space)) { body.applyImpulse({ x: 0, y: 3, z: 0 }); }}A ball counts its solid contacts from collision-enter and collision-exit, and Space kicks it upwards only while it touches something, so it cannot jump again in mid-air.
Click the scene, then press Space to make the ball jump. Pressing again in mid-air does nothing. Drag to orbit the camera, scroll to zoom.
- Every solid contact that begins is followed by its exit, so
contactsis the number of things the ball touches right now. Math.maxkeeps the count from going below zero if the script starts while the ball is already touching something.- A wall the ball touches also counts. To accept only the floor, also check
event.data.otherEntityId, or cast a shortPhysicsRaycasterray downwards instead. - On a compound body, each of its shapes reports its own contacts.
event.data.colliderHandleandotherColliderHandletell the pairs apart when you need to.
Common mistakes
Section titled “Common mistakes”- No Signal Emitter, or no signal. Without an enabled Signal Emitter with a signal when the body is built, nothing is sent.
- Adding the Signal Emitter later. Events are wired when the body is built. Add the emitter in the editor, not halfway through play.
- Listening for collisions on a sensor. Sensors only ever send
intersection-enterandintersection-exit. - Expecting wall bumps from a Character Controller. Its capsule slides along walls and sends no contact for them.
- Reading
contacton other events. It exists only oncollision-enter, and not always. - Using the same signal for several entities without checking. Check
event.entityIdto know which entity sent the event. - Two layers set to ignore. Colliders on ignoring layers never meet and send nothing. Check the physics layers.