Welcome back, my fellow game developers.
In this video, we've added some sound effects.
So when we run the game,
you'll notice now that when we shoot,
we have the bird sound.
If we open the chest,
we get a rifle.
We have a different sound.
And if we try to break this, there we go.
You can see that we have a sound effect for that also.
So let's not waste any more time,
and let's get started.
Okay, so now it's time to play
some sound effects and make
the game come alive even more.
So what are we going to do?
The first thing is obviously we need
a reference to all of our sound effects.
So in here I'm going to duplicate this.
And instead of music,
I'm going to call this the effects at Enter.
Oops, not enter. There we go.
So now we have a reference to the sound effects.
But there is a problem.
If I want to use the same method
that I've used to play our music,
then our sound effects are going
to be replacing our music.
And that's a no-no.
We don't want the sound effects to replace music.
That's just not while logical.
So we have something different that we can do.
And that is a method that is play clip at point.
And I do recommend that you open the resources of
the audio source and you look
through everything you can find.
So you can see you can change the play on awake,
the pitch, the MUX using cold.
And this is an entire course all by its own.
If you want to go into the Audio Source,
how to manipulate the music,
how to manipulate different sound effects
and all of that good stuff.
But for now, let's focus on the play clip. At the point.
This is how you call it.
You need a position,
a volume if you need to clip.
And you can see in the description that
it plays an audio clip
at a given position in the world space.
So this is what we need.
But you can see right here that
the position in world space,
well, where do you think is
the best position to play our music?
Well, I'm going to tell you
right now that the best place to
play that is exactly on the main camera,
exactly on the listener.
So we can hear it the best way.
Because I click on the 2D up here,
we can see that we have this 3D.
And on the audio listener,
if I had to see how that works,
how do I show you?
Maybe on the Audio Manager,
on some kind of, well,
okay, for now, I'm not going to show you,
or at least we can see this right here.
So this is the range at which we play our music.
And for now we are playing on the audio source that,
so that is not bad.
But if we play the clip off point and it's
somewhere far away from the listener,
we won't be able to hear it properly.
So that's why we are going to play
the sound on the main camera.
Was that out of the way?
Let's go back into our code right here.
And we are going to go ahead and create a public void,
play as a fax.
And this time we're going to give it an integer which
is the fax number.
And in here I'm going to
go ahead and open up the brackets.
And I'm going to say that the audio source,
dot play, clip, play,
all know the actual,
not the audio source that we have a reference to,
but the actual audio source.
So the one which has
the a as capital that play clip at a point.
And in here we are going to give it
a sound effect or an audio clip,
which is going to be the effects number.
Okay, so save that.
And what is the problem in here?
Well, obviously we need to
add the position and what's the position?
It's the main camera.
So remember that we can access the camera dot domain,
dot transform the position.
And honestly, I'm not sure if we've covered this.
So I'm just going to tell you that if we write camera,
that main, we immediately
get a reference to the main camera.
And from there we can use the transform that position.
If we didn't cover this,
I'm sorry, I said, Well,
we already know, but maybe we already did.
In any case, this is how we access the main camera.
So now let's go ahead and try to break some boxes or not
break the boxes actually tried to break the Roberta's.
So back in our game,
we can see that if we go to the breakable,
so breakable, or is it the receivables?
There we go In the breakable,
if we are dashing,
we said the trigger to break.
So in here we can either immediately
call or we can go ahead and create a method.
And let's say for example,
the method is going to be a public void.
Play, break as affects.
And in here if you want.
Now, there is two different types
of maybe thinking about this way.
If you want, we can just have one
breaking sound for all of our breakable.
Or if you want, you can go
ahead and create in here, for example,
a public integer or not
public actually I serialized field is something
butter integer and it's going to be
the ASA fax number to play, for example.
And then you can choose which one you
play and you can send it right here.
So access the audio manager dot instance,
dot play as effects and sent to the fax number to play.
Close that. And then when we do break in here,
after we trigger the animation,
we can go ahead and play break as effects.
And there we go.
That's all that we need to do.
Or if you want, there is
a another way and that is playing
the sound effects using the animation event if you want.
So if we go in the bot towards the bot, no,
bought, the breakable bought if we go to the animations,
we can choose in here to have some kind of event system.
For example, here we destroy,
we can play this right here.
So as soon as it breaks bam in here,
we add this animation event.
Maybe move it a little bit to the left.
We play the animation event.
We play the break as effects.
And on the breakable bot will
notice that we have the fax number that we want to play.
And if we go to the audio manager,
okay, so we have added the sound effects yet.
So click in here all your sound effects,
select all of these and
drag them and to the sound effects.
As you can see, we have 12 sound effects.
One of them is the bottle breaks,
so this is what we are going to be using.
So let's hear that.
Okay, So I think that's good enough.
A bottle break as a outbreak.
So what is the position on the Audio Manager?
It's the 0.
So I go to the breakable.
I set it 0 exactly.
So it was waiting for us.
Excellent. And 0, save that. Run the game.
And we should see,
oops, there we go.
And you can slightly here how that bought really broke.
So with that out of the way,
with all of these interesting ways
that we can play sound effects.
I'm going to issue you a challenge.
And your challenge is to add all of the sound effects.
So find all the sound effects you need for your games.
All the shooting, the blood, the shotgun,
the pistil, the gun, whatever you want,
and call the method and all of the appropriate places
and play the sound facts so well that out of the way,
pause the video right now.
I would not do all of these sound effects.
I'm just going to a few and leave the rest to you.
So with that out of the way,
pause the video right now and go to the challenge.
Oh, okay, welcome back.
So what I'm going to do is I'm going to
go into the weapons system.
And in here I'm going to go ahead and
create a serialized field,
which is going to be int or integer.
And whereas the breakable, breakable,
I'm just going to use the same right here,
same variable based on in here.
And we're going to create a
public or public whatever it is,
we can make it private. It doesn't matter.
Public void and play play.
Weapon sound effects actually.
And in here we are going to access the old
your manager dot instance dot as effects.
And we're going to send to it the fax number to play,
save that back in Unity.
Let's look at our audio manager and what we have.
So AudioManager, let's see.
The pistol will play the running rifle,
pistol shot, which is four.
So pistol as fax number for save that We can
even maybe apply the changes
to the pistol weapon and everything.
Apply the changes.
Now if I go into the Prefabs,
if I go into the where are we?
Weapons? Rifle, and let's see what we have in here.
So I'm going to check the audio manager.
The rifle sound is the seven.
So seven, the shotgun sound is the shotgun.
99.
Save that.
And I'm going to make our music
just a little bit less noisy.
So the volume, Let's set it to 0 to run the game.
And now we should see that when we shoot with the pistol,
we don't have any sound for some reason because obviously
all obviously we forgot to even call it.
So in here after we instantiate the bullet,
also going to play the weapon as effects.
There we go, save that back in our game.
We run the game.
And now when we How cool was that?
So now we have an actual pistol being shot.
Let's see what we get in here. The rifle.
So you can see if we tried to shoot
around because we are on the other side of the camera.
So this is the main camera if we're on the right.
The further we go away.
This last weekend here the
shots because we are playing at
the main camera position and
the main camera needs a bit of
time to catch up with the player.
So let's try this again.
Maybe we get a shotgun this time.
How cool is that?
So now we have sounds for our guns.
There is a lot more sound effects that you can add.
I'm not going to add all of them.
I'm just going to keep it as it is.
But for now, let's go ahead and the
Prefab, the Audio Manager.
And let's just add it in here next to the player.
Maybe we can even create a folder for it on its own.
So I hope you enjoy it.
I hope you are committing your changes.
And I'll see you in the next video where we will be
maybe adding some kind of audio mixer.
So see you then.