Making games

Audio

Two components make sound:

  • An Audio Source plays one sound, from where its object is.
  • An Audio Listener is the ears. Put one on the camera or the player. The first one in the scene with Active on is the one that hears.

Audio Source

PropertyWhat it does
ClipThe sound: a .wav, .mp3, .ogg or .flac you’ve imported.
Volume1 is the sound’s own level.
Pitch1 is normal. 2 plays it twice as fast and an octave up.
LoopingStarts over when it ends: music, an engine hum, wind.
Play on awakeStarts by itself when the game starts.
SpatialGets quieter with distance from the listener and pans left and right. Turn it off for music and UI sounds, which should sound the same wherever you are.
StreamingReads the file a little at a time instead of all at once. Turn it on for long music and ambience so it starts at once; leave it off for short sounds that play often, like footsteps.

Playing sounds from a script

Set the source’s properties from a script, as Skyhawk’s engine sound does:

fn on_collision(other, normal) {
    self.set_property("AudioSourceComponent", "play_requested", true)
}

fn quiet() {
    self.set_property("AudioSourceComponent", "volume", 0.3)
}

fn silence() {
    self.set_property("AudioSourceComponent", "stop_requested", true)
}

play_requested plays the clip once; stop_requested stops it. Both reset themselves, so set them again to play again. Volume and pitch can be changed every frame, which is how an engine sound rises with its speed.