In this lecture, we're going to add some audio into our game for when Barry crosses the finish line
or when Barry bumps his head.
So let's get started implementing some audio.
Let's start with a little bit of audio terminology so that we're on the same page here.
We'll need an audio listener in our scene and it acts kind of like a microphone in that it receives
the sounds and plays them through your computer speakers.
It's not literally a microphone, but metaphorically.
It's listening for what sounds are being played.
Usually the audio listener is placed on the camera in the scene.
So by default, our camera.
Let's just quickly have a look at this.
We'll click on our main camera, already has an audio listener that's sitting there saying, I'm listening
for sounds, which is very cool.
We have an audio source which allows us to play the audio and also allows us to adjust settings, for
example, to adjust the volume to be louder or less loud for particular things we want to play in our
scene.
And then of course we have audio clips.
This is what contains the audio data to be played within our scene.
For example, MP threes or waves or Ogg or Ogg files.
They're some of the file formats that unity can play.
The first thing to do is for us to grab a couple of sound effects.
So we're going to need two sound effects one for the crash, one for the finish so you can jump onto
the Internet, do some search for royalty free sound effects or just sound effects in general, and
download some that you think would be pretty cool.
Or you could go ahead and make a couple if you know how to do that using audacity, for example.
So I want you to find some sound effects you can use for crash or finish, or if you're feeling a little
bit lazy, you can just download the ones that I've provided you against this lecture.
I've given you two sound effects.
So if you don't want to go search for any, you can just use mine.
So there's your challenge.
Take that on C back here when you're done.
I'm going to download and unzip the sound effects that I've got.
I'm going to click on my assets folder, actually, so I can drag these.
Click, click, drag them into unity.
And from here I'll right click, create a folder and that folder we will call audio.
I think it's a good name for that.
We'll grab both of these and pop them into the folder.
Just a quick one.
Maybe if English is not your first language.
SFX The letters there.
SFX is just another way of writing sound effects.
The letters F and X kind of sound like when you say them quickly.
So that's how we write sound effects.
Now I'm going to start with the finish line.
If I double click on that, takes me all the way down here.
Actually, you know what?
Let me just very quickly bring this all the way back next to barrier.
So if we want to trigger this sound effect, we're not having to scoot halfway across the world to get
there.
Now, the finish line, what we need to do, first of all, is add a audio source so that we have a
way of playing a sound effect.
So click on Add component search for audio source.
You can see that there put that drops one on in here.
You can see there's some fields and some settings.
We have the audio clip and this is the place that the audio source is going to say, I'm going to play
this particular thing.
So for me, when it comes to the finish line, I'm going to drag my finished sound effects onto audio
clip.
Or I could have clicked the selector and found it within my project.
Now, this approach of dragging or having something set within the audio clip field is for if we're
only going to be triggering one specific sound effect for this game object or for this audio source.
So when we cross the finish line, it's always going to be this particular sound effect.
So I'm comfortable with that.
While I'm here, I'm going to drop the volume down to say 0.5 because I'm pretty sure it's going to
be a bit too loud.
I'm not going to mess around with any of the other things, but it's kind of cool to play around with
pitch.
If you wanted to make your sound effect a little bit higher, a little bit lower, you can change pitch.
And while I'm here, I'm going to click play on a wake because we don't want the sound effect to go.
When I click on the play button, we want it when the snowboarder flies through this collision area.
Now, what script is going to deal with that?
It's going to be the finish line script.
So let's open up finish line now because we're only playing this one sound effect.
Where are we?
Back to finish line audio source.
It's just this one sound effect.
All we need to do is say, Let's get this component and play.
That's all we need to do.
So we'll jump in here and do this after we have finished our thing and done our thing.
Yep.
Down in there.
So right after we're invoking reload scene, let's simply get component.
We can get component because the finish line script is on the same game object as our audio source because
the component we're going to get is audio source, parentheses and then dot.
Now that we've got the audio source, what do we want to do?
Well, audio source comes along with some methods.
And one of those methods is to play very similar to the particle effect.
So play and then parentheses semicolon just a group this I know it's a minor, minor thing, but I'm
just going to cut that and pop it right next to the particle effect so I can see what I'm scrolling
through this.
Okay, play particle, play audio and then reload the scene.
Click Save.
We'll see if this works.
Okay, Cruz, Cruz, Cruz.
When we hit the thingy.
There we go.
You should have heard that.
Now, that's all there is to triggering audio.
Pretty.
Pretty simple, huh?
But we're going to add a little bit of complexity.
Not a lot, but a little bit of complexity.
We're going to have a look at battery and we're going to say with battery, we might want to play many
different sound effects with Barry.
So let's go ahead and add a component.
Add the audio source component, which is a way for us to play our different clips.
We might say, You know what?
There's going to be a sound effect if you crash, a sound effect if you land, a sound effect if you
do a spin.
So we don't want to go and drag in one specific clip into the audio clip area in here because it's going
to change.
But we can play different things through code.
How do we do that?
Well, let's open up our crash detector, open that up.
And within here, we need to save.
First of all, what is the sound effect that we want to play if we crash?
So very similar to the way we did with the particle system, we'll serialize a field.
This will be audio clip.
So I've type audio clip and then we'll call this crash sound effects or SFX for short now as we did
before, we'll look for where it is that we're crashing here.
It is that we crash.
We're doing our particle effect.
And then next we will once again get the component.
So get component audio source.
However, instead of just using play in here, we're going to do something slightly different.
We're going to use a method called play one shot.
And that allows us we put in our parentheses, you can see in here, it says plays an audio clip and
scales the audio source volume by volume scale.
So the main thing in here is that we can actually say this is the specific audio clip that we want to
play and we've set that up already that we've identified.
The audio clip is going to be our crash.
SFX So we'll pop in crash.
SFX In here and then semicolon getting the audio source we're telling it to play.
This time we're saying play one shot as our approach.
And what are we playing?
We're playing.
Sound effects.
Save that back to unity.
Now we need to go and tell it.
Which thing is the crash sound effect?
Click on Barry and will find in here.
Where is our crash detector script?
There it is.
Crash SFX, which one do we want?
Well, this time I'll use the selector just to show you a different flavor.
And I'm going to use the crash sound effect.
And we could, of course, have in here other fields for maybe there's a land sound effect or a spin
sound effect.
And then within our code, wherever it might be, it doesn't have to be just within this one particular
script.
It doesn't have to be in crash detector.
It could be within player controller.
We might want to play a different sound effect, but we'll still be accessing the same audio source
and telling it to do the play one shot.
But let's see if this works.
Click on play here.
Okay, cool.
Does a nice little sound effect.
Excellent.
So there we go.
We've got a couple of pieces of audio in our level, finish line and crash.
It's pretty straightforward once you get the knack of it, but it really adds a lot of extra flavor
to your game.
Okay, I'll see you in the next lecture.