Chapter 10. Playing audio: sound effects and music
This chapter covers
- Importing and playing audio clips for various sound effects
- Using 2D sounds for the UI and 3D sounds in the scene
- Modulating the volume of all sounds when they play
- Playing background music while the game is going on
- Fading in and out between different background tunes
Although graphics get most of the attention when it comes to content in video games, audio is crucial, too. Most games play background music and have sound effects. Accordingly, Unity has audio functionality so that you can put sound effects and music into your games. Unity can import and play a variety of audio file formats, adjust the volume of sounds, and even handle sounds playing from a specific position within the scene.
This chapter starts with sound effects rather than music. Sound effects are short clips that play along with actions in the game (such as a gunshot that plays when the player fires), whereas the sound clips for music are longer (often running into minutes) and playback isn’t directly tied to events in the game. Ultimately, both boil down to the same kind of audio files and playback code, but the simple fact that the sound files for music are usually much larger than the short clips used for sound effects (indeed, files for music are often the largest files in the game!) merits covering them in a separate section.
The complete roadmap for this chapter will be to take a game without sound and do the following: