Unity: Getting started with Audio!
Let the player immerse into the game with sounds!
Up until now, we have added many things to make the game feel more like a real game. We have UI, a working and modular powerup system and some quality assets! However, to let the player fully immerse into your game, you should definitely consider to add sound effects and music into it. I bet you haven’t played a fully released game which doesn’t offer some sounds or music during the last three decades, right? Just think about classic games like PAC MAN! It might did not have any background music, but it had some great sound effects throughout the whole game. I’d bet that without these sounds, PAC-MAN would not be as nostalgic or considered a classic as it is today. It did feature sounds which increase the factor of remarkability!
Now then, let’s add sounds and music to the Space Shooter as well!
Setting up the AudioManager
To keep things clean and tidy, let’s first create a Audio Manager GameObject. Doing so is pretty easy:
- Create an empty GameObject
- Rename it to “AudioManager”
In order to be able to actually play some music, we need to add an Audio Source component. Add it in the usual way: Add Component > Audio Source
We now have the AudioManager we wanted to have. And as you can see, we can manipulate and change many parameters and settings. We keep it easy. Therefore, we only focus on the Audio Clip, Play on Awake and Loop settings.
Adding the background music
The background music we would like to use is a audio file, or Audio Clip if you want. Simply drag the audio file you want into the Audio Clip field of the Audio Source component. Enter Play Mode inside the Editor and you will have some music!
One quick tip: Make sure to tick the boxes on “Play on Awake” and “Loop”!
Play on Awake makes sure your audio file will play as soon as you start the game.
Loop will play the audio on loop, so there are no interruptions like a suddenly stopping background music
As GIFs are pretty bad in playing audio, you won’t be able to hear everything as it should be at your Editor this time :)