Sound
<Sound url> plays a sound file inside a World from when it mounts until the file ends or the component unmounts. Where you mount it decides where the player hears it from:
- Inside an Entity, the sound comes from the entity and grows quieter with distance from the camera.
- Anywhere else, the player hears it the same everywhere: an ambience, or a sting at the end of a round.
Its props are in SoundProps.
Roblox’s Sound works the same way: parented to a part it plays from there, and parented anywhere else it plays everywhere. Unity’s AudioSource and Godot’s AudioStreamPlayer3D are the placed kind; Godot’s AudioStreamPlayer is the everywhere kind.
When sound starts
Section titled “When sound starts”A browser, a phone’s included, keeps a page silent until the player presses a key, clicks or touches the screen. A Sound mounted before that input starts playing on it. A one-off sound that a pickup or a footstep plays before that input does not play.
An iPhone with its ring switch set to silent plays no page sound, a game’s included, until the player sets it back to ring.
Footsteps and pickups
Section titled “Footsteps and pickups”Two sounds need no Sound of their own:
<Player footsteps>takes a list of files and plays one of them at random for each stride she walks. A stride is 1.67 metres, a third of her top speed of 5 m/s, which matches the three steps a second of her run.<Pickup sound>plays its file once, heard everywhere, as she takes the pickup. It does not play when the scene ends around a pickup nobody took.
The library’s sounds
Section titled “The library’s sounds”sounds names the files in the shared asset library, which ship with the engine, and says what each one is for.
Sample
Section titled “Sample”import { Entity, Pickup, Player, Sound, sounds, World,} from "@daniel-zarinski/engine";
export function Run() { return ( <World map="meadow"> <Player footsteps={sounds.footstepsGrass} /> <Sound url={sounds.forestAmbience} loop volume={0.5} /> <Entity position={[0, 0, -3]}> <Pickup reward={1} sound={sounds.pickup} /> </Entity> </World> );}Where it runs
Section titled “Where it runs”Client. Sound changes nothing in the world; each player hears their own. Removing a game’s frame frees its audio with everything else it held, as the trust boundary says.