Audio Positional
Updated
3D sound that gets quieter with distance.
- Type
ent-audio-positional- Script class
PositionalAudioEntity- Family
- Audio
Overview
Section titled “Overview”An Audio Positional entity plays a sound file from a point in the world. The sound comes from where the entity stands, turns as the player turns their head, and gets quieter as the player walks away: a radio on a desk, a fountain in a square, a machine humming in a workshop. On headphones the player can tell whether it is left, right, in front or behind. The entity draws nothing in play; in the editor it shows a loudspeaker icon, and while selected, a faint sphere at its full-volume distance.
Its sound comes from the required Positional Audio component (PositionalAudioComponent in scripts). It behaves like the global Audio entity in every other way: the same src, volume, repeat and autoplay props, the same play(), pause(), stop(), setVolume(v), isPlaying() and load() methods, and the same lifecycle (autoplay in play, audition on selection in the editor). Its events are named AudioPositional::… instead of Audio::….
Use it for any sound that belongs to an object. Pick the global Audio for music and interface sounds, an Audio Ambisonic for a full 360-degree recorded ambience, or an Audio Tone Positional for notes a script generates at a point in space. To keep the sound of one room from leaking into the next, add an Audio Zone.
The props that matter most, besides src, volume, repeat and autoplay:
refDistance: the distance in metres within which the sound plays at full volume. Raise it for big, loud sources such as a waterfall; lower it for small ones such as a clock.rolloffFactor: how fast the volume falls pastrefDistance. Higher values fade faster.distanceModel: the shape of the falloff.inverse(the default) fades naturally and never quite reaches silence;linearfalls in a straight line to silence atmaxDistance;exponentialfalls faster asrolloffFactorgrows.maxDistance: with thelinearmodel, the distance at which the sound becomes silent. Withinverse, it changes nothing.
Set it up
Section titled “Set it up”- Add an Audio Positional entity to the scene. To make it follow an object, place it as a child of that object in the hierarchy.
- Set its
srcto a sound file, and chooserepeatandautoplayas for any sound:repeatand on for a loop,onceand off for a sound a script triggers. - Set
refDistanceto the radius of full volume around the source, for example1.5for a radio. - For a sound that must stop at a known range, set
distanceModeltolinearandmaxDistanceto that range. KeeprefDistancebelowmaxDistance. - Add a Signal Emitter with a
signalif scripts need to hear its events.
Examples
Section titled “Examples”A radio that plays when clicked
Section titled “A radio that plays when clicked”Start and pause a positional sound when the player clicks the object it belongs to.
#pragma lifecycle(startup)
// Scene: a mesh "Radio" with a Selectable and a Signal Emitter on 'radio/clicked'.// As its child, an Audio Positional entity "Radio Sound" (autoplay off, repeat).let sound;
function startup() { sound = findEntity('Radio Sound').getComponent(PositionalAudioComponent);
on('radio/clicked', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again if (sound.isPlaying()) sound.pause(); else sound.play(); });}How it works:
- The sound entity is a child of the radio, so the sound always comes from the radio, wherever it is moved.
- The click arrives through the radio’s Signal Emitter as
{ entityId, eventType, data }; the script keeps onlySelectable::select. pause()keeps the position, so the next click resumes the song.
A buzzing fly that circles the player’s desk
Section titled “A buzzing fly that circles the player’s desk”Move a positional sound in a circle so the player hears it travel around them.
#pragma lifecycle(startup, update)
// Scene: an Audio Positional entity "Fly" at the scene root// (src: a buzzing loop, autoplay on, repeat, refDistance: 0.3).const CENTER = { x: 0, y: 1.2, z: -1 };const RADIUS = 0.8;const SPEED = 1.5; // radians per secondlet fly;let angle = 0;
function startup() { fly = findEntity('Fly');}
function update(dt) { if (!fly) return; angle += SPEED * dt; fly.position.set( CENTER.x + Math.cos(angle) * RADIUS, CENTER.y + Math.sin(angle * 3) * 0.1, CENTER.z + Math.sin(angle) * RADIUS, );}How it works:
- Moving the entity moves the sound: every frame the fly is placed on a circle, with a little vertical wobble.
update(dt)receives the frame’s length in seconds, so the fly moves at the same speed whatever the frame rate.- Writing
positiondirectly is fine here: the fly has no collider and nothing listens to its Transform events. - A small
refDistancemakes the volume change clearly as the fly passes close to the player’s head.
Start a sound only when the player is near
Section titled “Start a sound only when the player is near”Play a fountain only while the camera is within range, and pause it beyond, to save work in a large scene.
#pragma lifecycle(startup, update)
// Scene: an Audio Positional entity "Fountain" (autoplay off, repeat, refDistance: 3).const RANGE = 25;const sourcePos = new THREE.Vector3();const eyePos = new THREE.Vector3();let fountain;let sound;
function startup() { fountain = findEntity('Fountain'); sound = fountain.getComponent(PositionalAudioComponent);}
function update() { if (!camera || !sound) return; fountain.getWorldPosition(sourcePos); camera.getWorldPosition(eyePos); const near = eyePos.distanceTo(sourcePos) < RANGE;
if (near && !sound.isPlaying()) sound.play(); if (!near && sound.isPlaying()) sound.pause();}How it works:
camerais the camera the player sees through; it isundefineduntil rendering starts, so the script checks it first.getWorldPositioncompares positions in world space, whatever the entity’s parents are. The two vectors are created once and reused every frame.- The sound plays only within 25 m. Past that distance the default
inversemodel would make it very quiet anyway.
An alarm whose reach grows
Section titled “An alarm whose reach grows”Change the distance props of a playing sound so an alarm carries further at each level, with a hard edge.
#pragma lifecycle(startup)
// Scene: an Audio Positional entity "Alarm" (src: an alarm loop, autoplay off, repeat)// with a Signal Emitter on 'alarm/sound'.// Another script emits 'alarm/level' with 0 (off), 1, 2 or 3.const REACH = [0, 10, 25, 60]; // metres of each levellet alarm;
function startup() { alarm = findEntity('Alarm').getComponent(PositionalAudioComponent);
on('alarm/level', (level) => { if (level === 0) { alarm.stop(); return; } alarm.setProps({ distanceModel: 'linear', refDistance: 2, maxDistance: REACH[level] }); if (!alarm.isPlaying()) alarm.play(); });
on('alarm/sound', (event) => { if (event.eventType === EventType.AudioPositional.Error) log('Alarm cannot load:', event.data.message); });}How it works:
- With
distanceModel: 'linear', the alarm is at full volume withinrefDistance(2 m) and silent exactly atmaxDistance, so each level has a precise radius. setPropschanges the falloff of the sound that is already playing; there is no need to restart it.refDistancestays below everymaxDistance, as thelinearmodel requires.- A script’s own
emit('alarm/level', 2)delivers the number itself to the callback, while the Signal Emitter’s events arrive as{ entityId, eventType, data }and useEventType.AudioPositional.
- With the default
inversemodel,maxDistancehas no effect. Switch tolinearfor a hard edge, or use an Audio Zone to confine the sound to a room. entity.positionis relative to the parent. When you move a sound to a world point (an impact, a footstep), keep it at the scene root, or convert the point into the parent’s space first.- The player hears through the active camera, and in a headset through their head, so positional sound works the same on desktop, mobile and XR.
Components
Section titled “Components”What a new Audio Positional 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 Positional | comp- |
Required | One | 3D sound that gets quieter with distance. |
You can also add 17 optional components, such as AI Speech Capture, Animation Graph, Animation Timeline, Attribution, Audio Analyzer, Audio Chorus Filter.
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 |
|---|---|---|
distanceModel |
"inverse" |
|
refDistance |
1 |
0. |
maxDistance |
10000 |
0. |
rolloffFactor |
1 |
0 ≤ x ≤ 10 |
volume |
1 |
0 ≤ x ≤ 1 |
repeat |
"repeat" |
|
autoplay |
true |
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.
AudioPositional::readyAudioPositional::errorAudioPositional::playAudioPositional::pauseAudioPositional::stopAudioPositional::end
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.
A sound inside an Audio Zone gains a long Reverb as the listener walks in: the zone's enter and exit events tween the filter's wet mix up and down.
With your sound on, hold W for about a second to walk the camera into the cave: the drum beat inside sounds, and the cave's reverb swells in. Hold S to walk out. Sounds inside a zone are silent from outside it. The beat is a short loop 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 Positional.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Audio Positional'); if (!(entity instanceof PositionalAudioEntity)) return;
// Its Audio Positional component holds the settings listed under Props. const component = entity.getComponent(PositionalAudioComponent); log('refDistance is', component.props.refDistance); component.setProps({ refDistance: 1.5 });}