Immersion Starts with Sound — Bringing Audio to our 2D Galaxy Shooter in Unity
Objective: To introduce the next element into our Unity project — Sound!
It is now time to get started with adding some cool sound effects to our game like the background music, lasers shooting and enemies exploding to take our game immersion to the next level. Let’s first get started with adding some background music to our scene.
Adding Background Music
Just like we have a Game Manager, Spawn Manager and UI Manager, it is time to create an Audio Manager that is going to handle the audio for our game. First let’s add an empty object to our scene and name it ‘Audio Manager’ and then within this, let’s add a new child game object and call it ‘Background’.
Right click in Hierarchy → Create new empty object → Rename to ‘Audio Manager’ → Right click on Audio Manager → Create new empty object → Background.
Next we need to add an Audio Source component to the Background game object. The Audio Source component represents audio sources in a 3D format and is usually attached to a game object. In order to play sounds, you must also have an Audio Listener component attached to a game object in the scene and this is usually set up on the Main Camera:
For this project, I have an Audio Clip ready to go so I am going to drag this from the Project window into this box and I also want to make sure that the background music ‘Plays On Awake’ and ‘Loops’.
Hit ‘Play’ with whatever Audio Clip you have dragged in and you should now be able to hear the music!