Trigger Areas
Learn how to use trigger areas in your scene
Trigger areas allow you to react to the event of a player entering or leaving an area, or of any other entity entering or leaving an area. This is a fundamental tool for creating interactive scenes. Use them for things like opening a door when the player approaches, or to score a point when a ball enters a goal.
Using trigger areas
To use trigger areas you need to add a TriggerArea component to an entity, then use a triggerAreaEventsSystem to react to the events.
import { engine, Transform, TriggerArea, triggerAreaEventsSystem } from '@dcl/sdk/ecs'
// create entity
const triggerEntity = engine.addEntity()
// set Transform
Transform.create(triggerEntity, {
position: Vector3.create(8, 0, 8)
})
// Trigger area
TriggerArea.setBox(triggerEntity)
// Event when trigger area activated
triggerAreaEventsSystem.onTriggerEnter(triggerEntity, function(result) {
if (result.trigger?.entity !== engine.PlayerEntity) return;
console.log('Player entered trigger area!')
})By default, the TriggerArea component reacts to the event of any player walking into the area. The above code adds if (result.trigger?.entity !== engine.PlayerEntity) return to check that the entity that caused the event is the current player, and not someone else's avatar
Trigger area shapes
Trigger areas can be either a box or a sphere.
To alter the size of the trigger area, you can use the scale property of the Transform component on the entity holding the TriggerArea.
Debugging
To debug your scene and see the area covered by the trigger area, you can add a MeshRenderer component to the entity with the trigger area, and set the shape to the one you want to debug. The dimensions of the default mesh will match the dimensions of the trigger area.
Trigger area events
You can use the triggerAreaEventsSystem to react to the different events of a trigger area:
onTriggerEnter: Triggered when an entity enters the trigger area.onTriggerExit: Triggered when an entity leaves the trigger area.onTriggerStay: Triggered while an entity is in the trigger area, every frame.
Trigger event responses
When a trigger area event is triggered, you can use the result parameter to get information about both the entity that was triggered and the entity that triggered the event.
The following properties are available in the result parameter:
triggeredEntity: The ID of the entity that was triggered (this is the entity that owns the trigger area)triggeredEntityPosition: The position of the entity that was triggeredtriggeredEntityRotation: The rotation of the entity that was triggeredeventType: The type of trigger event (ENTER, EXIT, STAY)timestamp: The timestamp of the trigger eventtrigger: An object with the following fields:entity: The ID of the entity that triggered the trigger (the entity that entered the trigger area)layers: The collision layers of the entity that triggered the triggerposition: The position of the entity that triggered the triggerrotation: The rotation of the entity that triggered the triggerscale: The scale of the entity that triggered the trigger
Common confusion: despite its name, result.triggeredEntity does not refer to the entity that triggered the event. These two fields are easily mixed up:
result.trigger?.entity: the entity that walked into the trigger area. This is usually what you want to check, for example to identify which player or object entered.result.triggeredEntity: the entity that owns theTriggerAreacomponent (the trigger zone itself, not the entity that entered it).
Use result.trigger?.entity when you need to identify the entering entity.
Detect players entering an area
The most common use of trigger areas is to react to players walking in or out. Two collision layers detect avatars, and which one to use depends on who the scene should react to:
ColliderLayer.CL_MAIN_PLAYER: Detects only the player using the local machine. Use this when the response is meant for that player alone, for example to teleport them, play a sound for them, or track their progress in a quest. This is also the cheapest option in terms of performance.ColliderLayer.CL_PLAYER: Detects all avatars, both the local player and any other players being rendered in the scene. Use this when anyone walking in matters, for example a door that opens when anyone approaches, or counting how many people are in a room. This is the default layer if none is specified.
The following example reacts only to the local player:
When using CL_PLAYER, all avatars activate the trigger area. To tell whether the avatar that triggered it is the local player, compare the entity in the result against engine.PlayerEntity:
In most cases, rather than trying to detect all players, the ideal approach is to detect only the current player (via CL_MAIN_PLAYER) and then sync the effects of this trigger between all players. For example, a sliding door opens only for your avatar, but then the open state of that door gets shared to all other players.
Trigger area layers
Use the optional second argument of the TriggerArea component to set the layers that will activate the trigger area. By default, the trigger area is activated only by the layer ColliderLayer.CL_PLAYER, see Detect players entering an area.
You can change the collision layer to detect any other entity by passing it as the second argument of the TriggerArea component.
Allowed values are the same as the ones for the MeshCollider component. See Collision layers for more details.
ColliderLayer.CL_PLAYER: any avatar (local + remote)ColliderLayer.CL_MAIN_PLAYER: only the local playerColliderLayer.CL_PHYSICSColliderLayer.CL_POINTERColliderLayer.CL_CUSTOM1through toCL_CUSTOM8ColliderLayer.CL_NONE
You can also set up a trigger area to detect multiple layers at once.
This will activate the trigger area when any entity with the layers CL_CUSTOM1 or CL_CUSTOM2 enters the trigger area.
Last updated