In this lecture, we're going to start adding some audio to our game.
Before we get into music, though, we're going to start with our explosions and our firing effect.
Now, the first step to adding any audio into our game is to find something to use.
So this is going to be your first challenge to find some audio that you like the sound of.
Here I am on Kenny Nel again, where we got our sprites from and if we add up to assets, Kenny also
has some audio.
So if we filter by audio, I'm going to go with the sci fi sounds pack.
Once you've downloaded your chosen audio, we need to import it into our project and I've placed mine
under my assets pack and sci fi sound.
So I've got all of my Kenny sounds in here.
And now let's think about how we're going to play this audio.
There are many options for playing audio in unity depending on whether you're using sound effects or
music and things like that.
So let's start by looking at playing one shot audio clips, and these are going to be audio clips that
are instantiated into the world at runtime, exist for the lifetime of the audio clip and then get destroyed
up in our hierarchy.
Let's right click and create a new empty game object and let's call this the audio player.
It doesn't really matter, but let's reset the transforms, keep it tidy.
And next we're going to add a new script to this.
So in our scripts folder, let's right click create a new C-sharp script.
Let's call this the audio player and attach that to our audio player game object.
With that set up, let's open up that script and see what we need to write.
Let's start by getting rid of start and update because we're not going to need them for this.
And I said we were going to play audio clips for when we're firing projectiles and when the enemies
are firing projectiles at us, and also whenever something takes damage.
So when that explosion happens, we're going to have a nice explosion effect to go with it to start
with.
Let's just go through the shooting effects, though, and then I'll leave the damage effect to you as
a challenge now, because our audio player is going to be managing multiple different pieces of audio,
it would make sense to separate up all variables using some headers.
So first of all, let's add a new header for our shooting.
And under this header we're going to need two variables.
We're going to need a serialized filled of type audio clip.
So the thing we want to play and let's call this the shooting clip and the second variable we want is
again going to be a serialized filled, but this is going to control the volume of our clip.
So this is going to be of type float and we'll call it the shooting volume to give it a default value.
Let's set this to one to start with, which is full volume.
And because our volume is going to range between zero and one, let's also add a range attribute to
this so we can say range.
And then in parentheses we can give it a lower bound of zero F and an upper bound of one F, and this
attribute will then constrain our value between those bounds.
This also has another nice benefit of adding a slider to our inspector to make it really easy to use.
So with our variables set up now, let's create the method that we're going to call from another script
to play this audio clip because it's going to be called from elsewhere.
Let's go ahead and make it public, but it's not going to need to return anything.
And we'll call this method play shooting clip.
In here.
The first thing we want to do is check whether our shooting clip has something assigned to it so we
can say if our shooting clip does not equal null, then we want to go and play this clip.
And the way we play this audio clip is by saying audio source don't play clip at point.
So what this method does is takes our audio clip and instantiate it into the world.
This means we're going to have to give this method some parameters.
And the first one is the clip we want to play.
So this is going to be our shooting clip.
From our tooltip, we can then see the next parameter is going to be a position in world and to make
the audio as audible as possible, we're going to place it directly in position of the camera.
We can get the camera's position by saying camera dot main dot transform dot position.
And finally we want to specify the volume.
So that is going to be our shooting volume.
Now this line is very long, so let's break it up and make it easier to read.
And that is all there is to this method.
Now we need to work out where we want to call this method from, and we're currently dealing with all
of our shooting behavior in our shooter script.
So let's open up our shooter script.
And in here the first thing we want to do is grab a reference to our audio player.
So let's say audio player and we'll call it the audio player.
Let's add an awake method.
And in this awake method we're going to say audio player equals find object of type audio player.
Then if we carry on scrolling down our script, we're dealing with all of our firing behavior in this
routine.
And this code routine is getting pretty messy.
So we may want to break this out into multiple smaller methods to make it easier to manage.
But for now I'm going to leave it as is, but feel free to update yours if you prefer.
And instead, I'm just going to add another new line in here.
This line is going to be our audio player dot play shooting clip.
So with that done, let's save our script and jump into unity to test this out.
If we check out our audio player, we can see how this header is going to come in useful for separating
the different clips in our game.
And we've also got this nice range slider between zero and one.
Before we can play though, we do need to set up our shooting clip.
So let's find our audio clips in our folder and I'm going to choose the laser small one for my audio
clip.
Now in our game view, if we just extend this over so we can see some of the options we need to make
sure that mute audio is unselected.
This can trip you up quite a lot of times, so if you've placed audio in your game and you can't hear
it, then just make sure that this option is unchecked in the game view.
With that deselect selected, let's go ahead and hit play and we've got some nice shooting effects.
Now this isn't too bad as it is, but with all rapid firing projectiles, this could get a little bit
overwhelming.
So let's turn it down a tad because otherwise it can get very annoying.
And I'm going to turn it right way down to around 2.4.
This seems like a much more reasonable volume and hopefully you can still hear it on your end.
So let's come out of play mode and we made that change in play mode.
So let's just reset that to 0.4.
And now I think it's time for your challenge.
I want you to go ahead and extend our audio player to play an audio clip in damages taken.
So when those explosions happen in our game, as a hint, it's a very similar process to playing our
firing audio clip.
So you can use all of what we've just done as a reference and have a think about which script is handling
the damage processing for our game.
So pause the video now and give that a go.
Okay.
Welcome back.
So let's add our damage audio to our audio player.
And it's so similar.
In fact, I'm going to start off by copying and pasting my variables that I've already written and I'm
going to change my header to be damaged.
I'm going to change my audio clip to be the damaged clip, and I'm going to change the volume to be
the damage volume.
Then we're going to need a method just like our play shooting clip.
So again, let's just copy and paste this and reuse what we've got.
We'll call this play damage clip.
And we now want to say that if our damage clip does not equal null, then our audio source play clip
point should be playing our damaged clip at the camera's position and we want the damage volume.
So that's all there is to this side of the script.
But I'm noticing that these two methods are virtually identical, so it would be quite nice if we could
simplify this in case we need to add any more audio to our game later on.
So let's actually write another private method of type void and we're just going to call this play clip.
And if we look at the similarities that we can draw from both of our currently written methods, the
things that really change are the clip and the volume.
So our method can accept these as parameters.
We need an audio clip and we'll just call this the clip for now and we need a float for the volume.
Then we can say that if our clip does not equal null.
Then the first thing I want to do is try and shorten the length of this line of code.
So I'm actually going to take the camera position as a variable.
So this is a vector three called camera pause and this is equal to our camera dot main dot transform
dot position.
And then we can play our audio by saying audio source play clip at point.
And we want to play our clip at the camera position at the specified volume.
With our more generic methods set up.
Now, we can use this in both of these other helper methods.
So let's delete everything that's in there and just say Play clip.
And we want to play our shooting clip at our shooting volume.
And for the second one, we want to play our clip and we want to play our damage clip at our damage
volume.
So a little bit of annoying refactoring out of the way there, but we've now got a much more robust
solution.
With that all set, we now have to actually call this play damage clip from somewhere and damage is
currently being handled entirely within our health script.
So let's open up our health script at the top of our script.
Let's grab a reference to our audio player and call this audio player as well.
In Awake, we can say that our audio player equals find object of type audio player.
And finally in our on trigger enter when we're taking damage playing our hit effect, let's say that
our audio player should play our damage clip.
So let's save this up now and jump into unity to test that all out.
In our audio player.
Let's set up a damaged clip and I'm going to go with explosion crunch for.
And if we once again hit play to test this out.
We have our shooting sounds and we have a nice explosion sound as well.
Just like our shooting audio though this may be a tad too loud, but until we get our background audio
in, I think it's okay to leave it as it is for now and then balance it later on.
So congratulations on adding the first pieces of audio to your game, and in the next lecture we're
going to start adding some background music.
So I'll see you there.