Skip to content

Audio Positional

Updated

3D sound that gets quieter with distance.

Type
ent-audio-positional
Script class
PositionalAudioEntity
Family
Audio

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 past refDistance. Higher values fade faster.
  • distanceModel: the shape of the falloff. inverse (the default) fades naturally and never quite reaches silence; linear falls in a straight line to silence at maxDistance; exponential falls faster as rolloffFactor grows.
  • maxDistance: with the linear model, the distance at which the sound becomes silent. With inverse, it changes nothing.
  1. 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.
  2. Set its src to a sound file, and choose repeat and autoplay as for any sound: repeat and on for a loop, once and off for a sound a script triggers.
  3. Set refDistance to the radius of full volume around the source, for example 1.5 for a radio.
  4. For a sound that must stop at a known range, set distanceModel to linear and maxDistance to that range. Keep refDistance below maxDistance.
  5. Add a Signal Emitter with a signal if scripts need to hear its events.

Start and pause a positional sound when the player clicks the object it belongs to.

RadioSwitch.jx
#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 only Selectable::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.

Fly.jx
#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 second
let 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 position directly is fine here: the fly has no collider and nothing listens to its Transform events.
  • A small refDistance makes 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.

NearbySound.jx
#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:

  • camera is the camera the player sees through; it is undefined until rendering starts, so the script checks it first.
  • getWorldPosition compares 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 inverse model would make it very quiet anyway.

Change the distance props of a playing sound so an alarm carries further at each level, with a hard edge.

AlarmReach.jx
#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 level
let 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 within refDistance (2 m) and silent exactly at maxDistance, so each level has a precise radius.
  • setProps changes the falloff of the sound that is already playing; there is no need to restart it.
  • refDistance stays below every maxDistance, as the linear model 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 use EventType.AudioPositional.
  • With the default inverse model, maxDistance has no effect. Switch to linear for a hard edge, or use an Audio Zone to confine the sound to a room.
  • entity.position is 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.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Audio Positional comp-audio-positional 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.01 ≤ x ≤ 10000
maxDistance 10000 0.001 ≤ x ≤ 10000
rolloffFactor 1 0 ≤ x ≤ 10
volume 1 0 ≤ x ≤ 1
repeat "repeat"
autoplay true

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • AudioPositional::ready
  • AudioPositional::error
  • AudioPositional::play
  • AudioPositional::pause
  • AudioPositional::stop
  • AudioPositional::end

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

PositionalAudioEntity.jx
#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 });
}