Welcome back my fellow game developers
to one of the more exciting parts.
Maybe it's a bit smaller
when it comes to the length of videos.
I'm in the length of the whole section,
but it really makes much bigger difference in our game.
So now when we run the game, There you go.
You can see that we have in music,
we walk around, we shoot our enemies.
We tried to kill the skeleton,
and for some reason we die.
All my god.
Oh my god. How much do you feel the pressure now that
we've applied fields so scary that we died.
Not only do we have the black screen for you died,
but we also have the music changing on us,
which adds a very scary atmosphere.
Okay, maybe not that much,
but you feel the gravity
of your death much more right now.
So without any further ado,
let's not waste any more time,
and let's get started.
Okay, so time to add a bit of music and sound effects.
Of course, I love this part of the game because
this is where we add the magic to the game,
where we make it just a little bit more amazing.
So first thing, first,
I'm going to go into
the Prefabs and I'm going to Prefab the weapons
just and let's just add it to the Sure.
Let's add it to the pickups. Why not?
Why not?
Or level objects,
maybe level objects would
have been a better place to put it.
So I'm going to move these to the level objects.
Okay, great, move that out of the way.
Let's go into the assets.
Let's right-click in here and create an audio.
Just all you double-click in here.
And I'm going to create two folders,
one for the music and one for the sound effects.
Just call it as f x.
For some fact, I'm going to go into the resources.
And here you can see that we have these four musics.
Let's go ahead and add all of them.
I'm not sure which one we're going to be using,
so I'll add the all of them into the music.
Wait for that. If it takes too long,
I'm going to use the magic of editing 0.
And now let's go into the sound effects.
So as a fox, I will select
all of them except for these two.
I'm not sure why they are still there,
but I'm going to go ahead and add them until
the sound effects and wait for
them to be added. Okay, great.
So let's double-click on the music
and let's see what we have in here.
So if I click on the fly and if
I tried to turn on the volume of it on my computer,
I can listen to it.
Listen to a preview of it.
Okay.
So obviously you can hear it.
So now what we'll do is we are
going to play that music and our game.
And it's pretty simple.
Whenever you want to play music,
you can simply drag it in here.
There we go. You can see that
now Let's make this smaller.
You can see that I have the fly in here.
And one of the cool things that we can do
is we can simply just from the game right now.
And you can see we have an option here which
is play on awake and the loop.
So I'm going to put it to
loop because we don't want the music
to actually end when the actual song finishes.
So now when I run the game,
and obviously nothing is happening.
Why is that?
Well, because the music isn't actually playing on awake.
Oh, okay.
So it took me about
three to four minutes to figure out what the problem is.
I was so confused and I had the mood of your own.
So now when I turn it off,
when we run the game, There we go.
You can see that we start playing the music.
I'm just going to turn this down.
And how are we playing this music?
I want you to pay attention to what we have in here.
We use something called the audio source.
And the audio source,
you can see that it has an audio clip on it.
So the clip that we are going to play an output
which we'll talk about later when we add an audio mixer,
we have the priority,
the volume, the beach, and etc.
So and we also have 3D
sounds that you can play around with.
So as you can see,
as soon as we add any kind of music to our game,
we can immediately play it.
But obviously this is not how we want things to work.
We want to be able to control all of that.
So in here we can see that we have
the multiple flights or let's try
the fly one and see how that sounds.
Or the industry.
And I think I'm going to use
this fly and I'm going to call this
the level music and use the fly as the death music.
So music, There we go.
You can see now it's the level music.
It's still called fly.
And the fly one will be the death music hit Enter.
And now we have these two sounds.
So before we go into the sound effects,
Let's go ahead and create
something called the Audio Manager.
So I'm going to first of all remove the fly right here.
I'm going to right-click create an empty.
And this will be a game object.
And I'm going to call it the,
I call it the gameObject seriously, audio manager.
I'm going to reset its position.
It doesn't matter where it is,
but I'm going to reset it in any case.
And in the scripts right here,
I'm going to go into
the managers do have
a folder for the managers. Nope, we don't.
So I'm going to right-click create a C-sharp script
called the Audio Manager.
Hit Enter and right-click,
create a folder for the managers.
What did it not change?
Managers, please. Manager.
So oh, okay. We already
have a folder optima managers delete that.
So managers.
Oh, there was a folder for the managers.
Why did I not get in any case?
Let's open up the managers lets the Audio Manager to
the audio manager and let's
double-click and see what we are going to do in here.
Now, the AudioManager, something that
we are going to be calling out a lot.
So I'm going to create an instance out of it.
So public, static or your manager instance.
And I'm going to create the awake and
the incidence is equal to this.
Great, So now we can access
the Audio Manager whenever we need to.
Second, we need a variable in here
which is going to represent all the music that
will have a serialized field, the audio clip.
So why is it audio clip?
Because as we saw right here, where the unity,
when we added the audio source which plays our music,
the music in here,
let's say we add the level music.
You'll see that this is the audio clips,
or we need a reference to
the actual audio clips that we are going to play.
So back and hear
the audio clips and this will be our music.
Just in this case, we will make
the first letter of the variable capital.
And then we are going to do a couple of things.
So what we'll do, as you saw in here,
if he finishes compiling any day now,
you'll remember that when we
added the music to our scene,
we created the audio source.
So for that, we are going to actually create or
add a component which is the audio source,
but something that I did not mention,
and that is how do we actually listen?
So obviously, if you want to
listen to music, you need ears.
You need a listener.
And where is our listener?
While our listener is
usually positioned on the main camera,
you can see right here,
this is a small component of
the camera called the audio listener.
And I can actually turn it off and on
and you can see that it has no properties.
It's just an audio listener.
It's extremely simple.
It's always on the camera by default, so keep it there.
It works.
So back in the Audio Manager,
now we have an Audio Source on it.
And what we'll do as we are going to be
adding the music that we have in here,
the death and the level.
And we are going to be adding them in here.
And every time, for example, we start a level.
We will play the level of music and every time we die,
we will instead remove the clip from
here and add the deaf news.
So we'll need a reference to the audio source.
So private audio source,
and it's going to be called the audio source,
simply like that source.
There we go. And now and start we are going
to get the reference to the audio source is
equal to get Component Audio Source. There we go.
Very simple.
So now let's create a method that will allow us to go
ahead and actually play any kind of level music.
So what we'll do is I'm
going to go in here and I'm going to
create a public void, play level music.
And I'm going to open up the right here.
These two, I always forget what they're called.
And then in here I'm going to say that
the audio source dot the clip.
So you can see the default audio clip to play right here.
Clip is equal to the music.
Open up the two brackets.
And in here we can choose which music we want to play.
So we can see right here that the music one,
the first element is the death,
the second one is the love of music.
So I'm going to choose one in here,
and this will play the music.
But one more thing that we need
to do is we need to actually go ahead
and access the audio source and
actually go ahead and play, save that.
So now we will actually start playing this music.
Let's save that.
And let's see what shall we be doing.
We also need to make sure that as
soon as we start the game right Here,
we go ahead and call
the player that I call this the player level music.
I meant play level music.
So play level, lay level music.
Save that. Back in our game.
So now if we look at the Audio Manager,
you'll see that we don't have any clip,
but when we start the game, There we go.
You can see that now we have
this audio clip and we can run around.
There we go. Now obviously
this is not the most exciting game music.
I believe you will find a much better song to play,
but I think it's good enough and
exciting enough to play around and explore.
So, but that out of the way, let me stop this.
And you can, for example here change the priority.
So what's the priority
between this audio source and another?
This is too detailed.
We won't get into that.
We can change the volume.
So for example, set it at 0.68.
If you think that the music
is too high, change the pitch,
change the stereo pan,
and so on and so forth.
But now, for example, when we died,
if I play around and our life is cubic,
let me just select
the layer and remove this one right here.
Whereas they don't play us on Macs health.
Let's set it to 50.
Run the game, and now we should die immediately.
So there we go. Kill me, please.
There we go, we die.
But the music keeps blink.
So we want to find a solution to this.
I'm going to show it to you as a challenge.
So your challenge is to play the game over music.
So look for music to play
for when the player dies from open game art.
And you can also find a lot of other levels.
And I didn't mention this because I really
didn't bother going through a lot of music.
I just want to make sure we
understand the concept and not
really go and have a lot of songs and play around.
You can, for example,
go in here and instead of just playing the music,
you can add an integer in here which will represent
the music we should play, for example,
and then play that music and for every level
have some kind of music that we play.
Later on you'll see how in
Level Manager we can manage all of that.
But for now, look for some music to play,
especially when the player dies or you
can use the one I've provided you with.
Create a method that plays the game over
song and call the method when we die.
So with that information,
pause the video right now and go do the shallow end.
Okay, welcome back.
How did you get all of that?
So in here, I'm going to go ahead and
play sorry, public void.
Play game.
Oh my God.
Game Over.
Music.
Open up the do whatever they called.
And then in here the audio source,
that clip is equal to music 0.
And in here I'm going to tell the audio source to play.
So save that.
And there is one more thing that we need to do,
and we need to call this whenever the player dice.
So if I go to the player health handler,
if I look through in here and find where we die.
So in here you can see turn on the death screen.
And we said the game object to inactive.
And we also get the audio manager,
dot instance, dot, play game over music.
Save that.
Back end our game.
And here, wait for this to compile.
Run the game for fuel source.
Now, when we die.
Yeah, How cool is that?
Seriously?
Seriously, take a moment to appreciate how amazing
what we've just created
doesn't add more depth to the game,
doesn't add a more feeling of dread doing actually die.
You feel yourself, oh my god, I just died.
That is so bad because of the slight change in the music.
I never really felt
this feeling when I previously died in this game.
Now, what was the music
changing with the atmosphere changing?
I feel like I've just then destroy it
because I really like the music change.
I hope you felt This also.
I hope you enjoyed and something that we need to
make sure we do in here when we commit our changes.
And the day now,
if you please can show me.
So come on any day now,
let's see in here we have
the committed changes that we want to do.
But we need to make sure to ignore
everything under the all your assets.
So if I ignore everything on the assets,
all DO because we don't want to save the audio.
It just takes up a lot of space and not that practical.
So now we stage all,
commit our changes and created
and all your manager that plays
music or plays level music
and music using all of your source.
There we go, commit our changes.
I hope you enjoy it and I'll see you in
the next video where we will
be playing the sound effects,
which is a bit more fun maybe than music.
So I'll see you.