In this lecture, we're going to add some sound effects so that when the player touches the coin, it
goes diddling.
And you know that you've picked up the coin.
I've given you a sound effect that I made myself in audacity for this particular lecture.
You can download that against this lecture, and I've got a challenge for you right from the get go,
and that is to use play clip at point to play a sound effect when you pick up the coin.
Now, I'm going to be using a very, very simple implementation for this approach.
There's more complicated ways of doing this.
In previous sections of the course, we've made a an audio manager that has all of our different sound
effects in it, and then we call publicly from other classes.
What I'm going to do is just the very quick and dirty, simple, putting the play clip at point, actually
within our coin pick up script so that we can quickly have some sound effect playing.
You can do it either of those two ways if you want to try the more, I guess, proper way of doing things.
And you can set up an audio controller that has all of the different sound effects being fed into it.
Or you can just do the quick and dirty way, which is what I'll be doing as well.
But it's good to see different ways of doing things, so then you can make your own opinion on these
things.
So there's a challenge.
Positive video.
See you back here when you've given it a go.
First step in the process.
I need a new folder which is right click Create Folder.
I'll call this audio within audio double click to open that up.
I'm going to drag in my sound effects coin pickup and it sounds something like now back into my script,
so I'm going to do this quick way.
You've got a couple of different ways to play sound.
You can just say play sound, but because when we're triggering, when the coin is being bumped into
by the player, we're destroying it straight away.
So we need something that sticks around.
That's why we're using play clip at point as opposed to just play.
If you have just play something in here like some sort of SFX Dot play, obviously we'd write the code
more properly than that.
But if you're just using play, then at the moment it starts to play.
It's also destroying itself and that sound goes away along with the audio source and all the other things
that are living on the game object.
Which reminds me, let's go over to our coin prefab prefabs coin.
In addition to these other things, we need to have an audio source so we have something that can actually
be playing these things.
We want it to play within audio source.
We've got some settings in there that we've played around with before.
Play on Awake.
We'll turn that off now.
Within my coin pick up class, I'm going to Serialize Field and I'll create an audio clip.
That's the thing we're going to play and I'll call it Coin.
Pick up as f x semicolon.
Now I'm going to jump down into my on trigger just above destroying in here.
Now we want to be accessing audio source and as we've talked about, play clip at point y play clip
at point well that instantiate the audio so that when we destroy this object, it will still stick around.
What are we playing?
We're going to play the coin pick up SFX and where are we going to play that?
We're going to play it at the position of the camera y the position of the camera.
Well, if we just jump quickly into 3D view, you can see in here that the coin play clip a point,
you can see my little sound thingy on it there.
It's pretty small, so it'll play it right there in 3D space.
However, the camera is over here.
The camera is the thing that's listening.
That's our headset or that's our headphones, if you will.
If we're running around in a3d game, it's kind of nice to have a sound over there.
Sound to the right and a sound over there, sound on the left or a distant or closer.
That's a good thing to do.
But in a2d game, we want to have the sound play the same no matter where it is on the screen.
Well, that's what I want anyway.
So we need to say play the sound right at where the camera is.
The way we do that is by accessing camera dot main because it's the main camera and it's transform dot
position.
We can also add in here if we wanted to add a third parameter, we can add in the volume and that's
a flute.
So I could say if I don't want the coin to be too loud, I could add in here a lower volume.
I can leave that where it is for now.
Don't worry about putting the volume in it because I can adjust the volume over here on the actual game
object itself so I could drag the volume down.
So I'm not going to worry about doing it in code.
This is a fairly quick implementation.
Why am I not going through a bigger, more detailed implementation where we have an audio manager that's
got all the different sound effects in it?
Well, at this stage I've only got one sound effect in my game.
That's all I want to implement.
And I want to do this in a way so you can see if you're prototyping something, if you're messing around
with it, if you want quick results, you don't have to go and implement the big proper full super architected
approach.
You can just say quickly bump play clip at point.
That means it won't be destroyed.
And if we click on Save jump back over into Unity, then we can decide what the sound effect is by clicking
on our coin prefab and then scrolling around to see where the coin pick up sound effects is.
And that's going to come from our audio folder.
Drag that on in, and that is the sound effect we're going to use.
So that for me is one of the simplest ways to get sound effects into your game that stick around.
Nice and simple, let's click on place if it's actually worked.
But then there we go.
So just some very simple sound effects to get us going along that road to get a little bit of audio
into our game.
You can go and add some other sound effects if you want.
For when the bullet hits the wall, the bullet hits an enemy or the bullet hits other things.
And as I've said a couple of times in this lecture, if you want to do a more full implementation,
then create yourselves an audio manager, as we've done in other sections of the course, and you can
go about getting more structure to it.
But for now, I just wanted to do a quick sound effect and to move on to the next part of what we're
doing in our game.
So good stuff.
I'll see you in the next lecture.