To The Sound — Audio Makes the Experience!
Let’s take a look at the current build:
It plays decently, but the lack of audio keeps the player from really engaging with the medium.
So how do we add sounds in Unity? It’s really very simple!
To add a looping background track that plays when the game starts and plays indefinitely, we can do the following:
- Create an empty gameobject to hold an Audiosource component. In my case, I just added an Audiosource to my background gameobject.
2. Now, drag whatever sound you want to loop into the “AudioClip” slot.
Now you have background music!
For any other sound effect (shooting, collecting a powerup, shooting a gun), there is a simple implementation that you can further edit to fit your needs. I will show how I implemented the laser firing sound.
- Add an AudioSource to the gameobject you want the sound to come from. I put it on my Player.
2. Go into the C# script associated with the gameobject and add two new variables:
3. Use GetComponent to set your AudioSource variable.
4. Use the AudioSource.PlayClipAtPoint to play your sound effect. This line of code should go in whatever function you want to start your sound effect.
In my case, I want the sound effect to play when the player fires a laser.
Lastly, don’t forget to place the audioclip into your inspector slot!
Hear the difference!