Audio Zone
Updated
Inside the box, only sounds in the same zone are heard.
- Type
ent-audio-zone- Script class
AudioZoneEntity- Family
- Audio
Overview
Section titled “Overview”An Audio Zone is an invisible box that keeps positional sound inside a room. While the player (the listener, which rides the camera) is inside a zone, they hear only the positional sources inside the same zone; every positional source elsewhere is muted. With the listener outside every zone, every source plays as usual. Use it for a library that should be quiet even though the workshop next door is noisy, a club whose music must not leak into the street, or a booth with its own soundscape.
Its behaviour comes from the required Audio Zone component (AudioZoneComponent in scripts). For each positional source, the rules are:
- A source inside a zone the listener is also in is heard.
- A source inside zones the listener is not in is muted: zone audio is silent from outside.
- A source in no zone is heard only while the listener is outside every zone.
Zones affect only Audio Positional and Audio Tone Positional sources. Global Audio, Audio Tone and Audio Ambisonic sources are never muted by a zone. A muted source keeps playing silently, so it is heard in place again when the listener comes back. A source with an Audio Fade crossfades when a zone mutes or unmutes it; without one, it cuts.
The one prop is halfExtents: half the box’s size along each local axis, in metres. The default { x: 1, y: 1, z: 1 } is a 2 × 2 × 2 m cube. The entity’s position, rotation and scale move, turn and stretch the box.
Zones act in play only. In the editor a zone draws as a translucent box while gizmos are on; in play it has no geometry. With a Signal Emitter, it reports AudioZone::enter and AudioZone::exit as the listener comes in and goes out, which also makes it a handy room detector for scripts.
Set it up
Section titled “Set it up”- Add an Audio Zone entity to the scene and place it at the centre of the room.
- Set
halfExtentsto half the room’s width, height and depth, for example{ x: 4, y: 2, z: 3 }for an 8 × 4 × 6 m room. Rotate the entity to match the room’s walls. - Place the room’s Audio Positional sources inside the box.
- Optionally add an Audio Fade to those sources so they crossfade instead of cutting when the player walks through the door.
- To react to the player entering and leaving, add a Signal Emitter to the zone and set its
signal, for exampleroom.
Examples
Section titled “Examples”Log entering and leaving a room
Section titled “Log entering and leaving a room”Report when the listener walks into and out of a zone.
#pragma lifecycle(startup)
// Scene: an Audio Zone "Library Zone" with a Signal Emitter on the signal 'library'.function startup() { const zone = findEntity('Library Zone').getComponent(AudioZoneComponent); log('Library half extents:', JSON.stringify(zone.props.halfExtents));
on('library', (event) => { if (event.eventType === EventType.AudioZone.Enter) log('Entered the library'); if (event.eventType === EventType.AudioZone.Exit) log('Left the library'); });}How it works:
zone.props.halfExtentsreads the box’s size,{ x, y, z }in metres.- The zone’s Signal Emitter puts its events on
library.AudioZone::enterandAudioZone::exitcarry no data.
Lower the music inside rooms
Section titled “Lower the music inside rooms”Duck the global music while the player is in any room, so each room’s own sounds stand out.
#pragma lifecycle(startup)
// Scene: several Audio Zones, each with a Signal Emitter on the signal 'room',// and a global Audio entity "Hall Music".let music;let inside = 0;
function startup() { music = findEntity('Hall Music').getComponent(AudioComponent);
on('room', (event) => { if (event.eventType === EventType.AudioZone.Enter) inside += 1; else if (event.eventType === EventType.AudioZone.Exit) inside -= 1; else return;
music.setVolume(inside > 0 ? 0.15 : 0.6); log('Now in', inside, 'zone(s), entered or left', findEntity(event.entityId).name); });}How it works:
- Every zone emits on the same
roomsignal;event.entityIdtells which zone sent the event, andfindEntityturns it back into the entity. - Global music is never muted by a zone, so the script lowers it itself.
insidecounts the zones the listener is in, because zones can overlap: the music comes back to full only when the listener has left them all.
Open a room to the hall
Section titled “Open a room to the hall”Grow a zone from a script when a sliding wall opens, so the room’s sound reaches the hall.
#pragma lifecycle(startup)
// Scene: an Audio Zone "Studio Zone" (halfExtents { x: 3, y: 2, z: 3 }).// Another script emits 'wall/open' and 'wall/close'.const CLOSED = { x: 3, y: 2, z: 3 };const OPEN = { x: 3, y: 2, z: 9 };let zone;
function startup() { zone = findEntity('Studio Zone').getComponent(AudioZoneComponent); on('wall/open', () => zone.setProps({ halfExtents: OPEN })); on('wall/close', () => zone.setProps({ halfExtents: CLOSED }));}How it works:
setProps({ halfExtents })resizes the box at once; pass a complete{ x, y, z }vector.- With the wall open, the box reaches into the hall: a listener standing there is now inside the studio zone and hears its sources.
- Membership is checked every frame in play, so the change is heard on the next frame.
- Zone events go to the zone entity’s own Signal Emitter. Without one, the zone still mutes sound but tells scripts nothing.
- Zones never mute global Audio, Audio Tone or Audio Ambisonic sources. Lower them from a script on
AudioZone::enterwhen needed. - Add an Audio Fade to the positional sources of a room so the soundscape crossfades at the door instead of cutting.
Components
Section titled “Components”What a new Audio Zone is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Audio Zone | comp- |
Required | One | Inside the box, only sounds in the same zone are heard. |
You can also add 4 optional components, such as Animation Graph, Animation Timeline, Motion Path Controller, Signal Emitter.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
halfExtents |
{"x":1,"y":1,"z":1} |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
AudioZone::enterAudioZone::exit
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
Two Audio Zones, a library with rain on its windows and a workshop with a drum loop: their enter and exit events name the room in a HUD label and lower the hall music.
With your sound on, hold A for half a second to walk into the library, or D to walk into the workshop. Inside a room you hear only its own sound, and the label names it. The rain and the beat are short loops generated for this example.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Audio Zone.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Audio Zone'); if (!(entity instanceof AudioZoneEntity)) return;
// Its Audio Zone component holds the settings listed under Props. const component = entity.getComponent(AudioZoneComponent); log(component.props);}