Aego Русский

Spatial sound

A spatial sound fades and pans with its distance to the listener. The listener is the first enabled, active Audio Listener 2D in the hierarchy; another active one later in the hierarchy is ignored (AUD005). Without a listener the camera of the main output listens, and AUD004 reminds you of it on spatial sources; without a camera, the origin.

audio.ListenerPosition() returns the last effective listener position, including the camera fallback, in world space during Play.

A spatial source plays at full volume up to Min Distance and is silent from Max Distance, both in world units. Rolloff is the curve of the volume between them: time 0 is Min Distance, time 1 is Max Distance. Pan Width, from 0 to 1, says how far a sound at Max Distance to the side is panned.

Every field is listed on the Audio Source 2D page.

A keyed Rolloff curve is read as the volume; a preset ease is read as the volume falling away; the default curve drops to a quarter halfway. Pan is the distance to the side divided by Max Distance, times Pan Width.

A sound played from a node follows it; audio.PlayAt and audio.PlayEventAt stay where they started. Positions are read after the frame's movement, so the sound follows what is drawn.

In Try it, the ball carries the listener and the camera follows it. The box in the middle hums: it is loud and centred when the ball passes it, and fades to the side as the ball rolls away. Change Max Distance or Pan Width of the box in the Inspector and listen to the next pass.

Try it

Change the example parameters and compare the result. Reset restores the initial scene.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene4 nodes
Ground
Transform
position[0, 2]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[30, 0.6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.2restitutionfloat0densityfloat1sizeVec2[30, 0.6]
Speaker
Transform
position[0, 0.7]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#22d3eefforiginVec2[0.5, 0.5]sizeVec2[1.4, 1.4]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Audio Source 2D
clipClipasset("ae900000000000000000000000000a03")eventAssetRef<data>asset("00000000000000000000000000000000")volumefloat0.9pitchfloat1panfloat0busenumambienceloopbooltrueplayOnStartbooltruespatialbooltrueminDistancefloat1.5maxDistancefloat12rolloffCurve{ ease=outQuad }panWidthfloat1pauseWithenumgame
Ball
Transform
position[-8, 1.2]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint1tintColor#f59e0bfforiginVec2[0.5, 0.5]sizeVec2[1, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumdynamicmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationbooltruebulletboolfalseallowSleepboolfalseinterpolatebooltruelayerlayer0masklayer mask-1
Circle Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0restitutionfloat0densityfloat1radiusfloat0.5
Camera
Transform
position[-8, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40viewportRect[0, 0, 1, 1]maskint-1orderint0clearbooltrueclearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
Virtual Camera 2D
priorityint0outputstring""zoomfloat0blendTimefloat0.6blendCurveCurve{ ease=inOutQuad }timeDomainenumunscaleduseBoundsboolfalseboundsRect[0, 0, 0, 0]snapToPixelsboolfalse
Camera Follow 2D
targetNodeRefnode("a0d30000000000000000000000000003")deadzoneRect[0.45, 0.4, 0.1, 0.2]dampingVec2[5, 5]lookaheadVec2[0, 0]maxLookaheadVec2[0, 0]lookaheadSmoothingfloat3snapDistancefloat20
Scriptmain.aes
package demo

// Walker rolls the listening ball past the humming box and back; the hum pans as the ball passes it.
script Walker {
    @export var Speed float = 5
    @export var Every float = 4

    var t float = 4
    var dir float = 1

    func Update(dt float) {
        t += dt
        if t < Every {
            return
        }
        t = 0
        body := node.Get<Body2D>()
        body.SetVelocity(Vec2{dir * Speed, 0})
        dir = -dir
    }
}