Day 29 — Adding sounds in Unity

Hey and welcome!

Today we’re going to look into adding audio into our game, which thankfully is pretty easy! First let’s start off by adding in some background music to our game, create yourself an empty object called AudioManager and then another one within that called BackgroundAudio.

With that added we can now add the Audio Source component which is going to be our main means of getting audio into our game for this article. With the component added simply drag in you background music audio file (I’m using WAV file types here) into the AudioClip option and make sure that your Play On Awake and Loop options are checked.

You should now have yourself some music playing when you start your game! Cool eh?

Right now let’s get a little more complicated with this, you may remember that our enemies have a little explosion that happens when they’re destroyed. Well let’s spruce this up by adding some explosion audio at this point.

So first off make sure that your enemy prefab has an Audio Source component on it and that you uncheck the Play On Awake option as we don’t want the clip to play when the game starts this time.

[SerializeField]
private AudioClip _enemyExplodeAudio;
AudioSource _audio;void Start()
{
_audio = GetComponent<AudioSource>();
if (_audio != null)
{
_audio.clip = _enemyExplodeAudio;
}
}
if (other.tag == "Player")
{
_audio.Play();
}
else if (other.tag == "Laser")
{
_audio.Play();
}

Again I’ve omitted some code here so I can just show off what we’re adding. One of the first things you’ll notice is that we’re caching a reference to the Audio Source component for our enemy and then assigning our explosion clip to it using .clip.

If you run the game and check out your instantiated enemies you’ll see that their AudioClip section in their Audio Source will be populated thanks to the above.

Now with the relevant clip assigned it’s just a case of appending .Play() to our _audio when our enemy collides with the laser or player. You may notice if we didn’t have the delay we currently have on destroying the enemy that the audio would not even play at all since we’re trying to access a component that’s part of our enemy.

Well there’s an alternative way we can go about this and that’s by making use of AudioSource.PlayClipAtPoint(). As you might be able to guess from the name this let’s us play a clip of our choosing at a position of our choosing in our game. Once the clip has finished playing it’s then automatically destroyed. So to implement it we can change the above code to look a bit like this:

[SerializeField]
private AudioClip _enemyExplodeAudio;
if (other.tag == "Player")
{
AudioSource.PlayClipAtPoint(_enemyExplodeAudio, new Vector3(0, 0, 0);
}
else if (other.tag == "Laser")
{
AudioSource.PlayClipAtPoint(_enemyExplodeAudio, new Vector3(0, 0, 0);
}

This is certainly pretty handy if you’re destroying the objects immediately but I’ve opted to not go for this method on the enemies since there’s already a delay before they get destroyed and it stops us from spawning in extra clutter in our game and potentially slowing it down.

So a good use of this would be a noise that plays when we collect our powerups since we destroy those immediately on contact with the player and there’s no reason to delay when the powerup gets destroyed.

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.