Welcome back, My favorite game developers.
As you can see, we've become a DJ in this video,
we have our mixers.
Now when we run the game,
you'll notice that we can play around with the music,
make it higher, make it lower,
and we can also control how loud sound effects are.
So okay, that's barely.
You can see that when we
increase the sound, we can hear it better.
When we decrease the sound, we hear it less.
So let's not waste any more time.
And let's get to mixing 0 case.
So let's add audio mixer and mix things up.
Yet it, Let's do the challenge or something.
Anyways, audio, audio mixer,
so Window audio, audio mixer.
There we go. We get the audio mixer here.
Let's sell it in the scene.
And as you can see,
this is some kind of object.
And if we look at the audio source on the Audio Manager,
you'll notice that it has an output criteria
and this is an object or a prefab of the Audio Manager,
but you didn't see this,
but my Unity crashed after I finished the video.
So there is our lot of weird things.
If you see something weird,
it's probably because of the crash.
Anyways, audio mixer, what do we have in here?
This is a way you can read all about the mixers.
But generally speaking, this is a way to control
the sound of each and every single audio
that we have in our world.
Now, we have the music,
we have the sound effects.
So we're going to create,
first of all, a mixer.
And this mixer will be the main mixer.
Very simply hit Enter.
There we go.
We've created a main mixer,
and you can see right here in the assets,
we've created a main mixer game object
or whatever you wanna call it,
not a game object and audio mixer controller.
So just like we have an animator controller,
we also have the main mixer controller.
And we can add it into the Prefabs right here.
So you can see it clearly.
Next thing we have is some groups, views,
and snapshots are not going to go
into details and attenuation and all of that.
I barely know what all of these are.
All I know that I can create a group
right here under the masters and call it the music.
And then select the masters.
Again. Make sure not to select
the music selecting masters.
And then create in here the ASA facts.
Can you please get out of the way? There we go.
So the sound effect,
so you can see the master
controls both the music and the sound effect.
And then we can change the music and
sound effects level as we please.
So if I go into the Audio Manager in here,
you can see that I can change
the output and choose one of these two.
So because we are using this audio source 40 music,
I'm going to choose the music. And there we go.
We can see that now the audio clips
that play using this audio source
will be using the music.
Let's demonstrate how that works.
So I'm going to go into the, where is it?
The volume in here?
I'm going to set it back to one.
And now when I run the game any day,
now, there you go.
You can see how loud that is,
but I can reduce the sound at it in play mode.
There you go. You can see that I can reduce
the sound using this mixer right here.
Even make it higher than it should be.
So keep in mind,
this is in the play mode,
so when I go back, I need to change it.
But as you can see now,
because it plays through
the audio mixer of the group and music in here,
that means that I can change things
around and added them as I want.
So I'm going to stop this. And there we go.
You can see that because it had this red button,
that means it was recording and it kept it at a very low,
20 or whatever, 28 decibels.
Okay, great, that's great.
But now we want to add sound effects.
But how are the sound effects produced?
While if I run the game,
this is something that we did
not handle in the last video.
I should, I pause?
Okay, I didn't do that faster,
nor should pause. And there you go.
You can see that this was a one shot all you,
and it has the Audio Source on its
own and it plays, and it works.
But there is no way for us to access the audio source.
I mean, sorry, the output of this audio source,
trust me, I've tried a lot to try to access it.
There is absolutely no way to do this,
but I did find some kind of way around it.
I'm sure nobody has done this before ever.
This is the first time that I've seen this,
but I tried it and it works. So let's do it.
So the first thing or the first understanding
that we need to change about this is instead
of just having the audio clips.
We are going to convert them into gain objects.
And then every single time we shoot,
we are going to instantiate that game object.
And then we are going to
change obviously the output of that,
or change the mixer on
that game object which will have the audio source.
I think nobody understood anything.
So let's demonstrate how this works and you will see it.
So back to the sound effects.
I'm going to select all of my sound effects.
I'm going to drag them into my hierarchy.
So now you can see each one of those is
a game object with an audio source.
So what I'll do is I'll select all of these.
So from the bottle break to
the weapon equipped and select
the output here and make it under
the sound effect main mixer.
Then we'll go to the prefab.
Right-click, create a folder in here which will
be the effects had entered, open this up.
And then one by one,
we will make all of them a prefer.
Excuse me.
There we go.
Still player that player getting hit, rifle shot,
running, shotgun, sniper shot, weapon equipped.
And now we can delete all of those and we
all have all of them as game objects.
Let's make the Audio Manager also a game object.
Save that.
And now we should change the way
our sound effects are played in the Audio Manager.
So let's do that.
I'm going to open up the audio manager in here.
And because we are already using
this kind of system where we get the parameter,
we can keep it as it is.
But instead of having these sound effects as audio clips,
I'm going to change this into
a game object array and save that.
Down here we will have an error.
Let's just comment this out, save this.
And now back in Unity,
you'll notice that we
have the sound effects which are game objects.
And I'm going to set this to 0 Enter.
So our list is empty.
I'm going to go into the sound effects folder,
and I'm going to lock this down,
select all of these,
and drag them into the sound effect.
So as you can see, they still have the same arrangement.
So we can change whatever we want in the breakable,
for example, or the pistol,
and everything will stay the same.
The implementation of the method will stay the same.
So now that we have this out of the way,
let's apply our changes.
And let's go back into
the code and see what we're going to do.
Now, I'm going to issue you a challenge.
I think it's a pretty solid
eight out of ten on the difficulty level.
So I'm going to give you a tiny hint.
This is not a big hint,
or just create a private game object,
which is going to be V as a fax.
And this is because we will be hoops.
Later on, we will be deleting
this game object and we need a reference to it.
So I'm going to save this and
I'm going to take you to your challenge.
Your challenge is to instantiate the sound effects.
So convert the method play as Effects.
You'll need to instantiate VV.
The proper as effects gameObject.
You'll need to make sure the parent is
the main camera so we listen properly.
Why the main camera?
Well, obviously because we said we want to have
the sound effect instantiate at the closest listener.
You need to destroy
the sound effect after a certain amount of time.
And we are going to use something called the invoke.
So we've done coroutines,
we haven't done invokes yet.
And I'm just going to show you
a little bit of what our invokes.
Anyways, this is the invoke right here,
which is a mono behavior.
And you can see, you can use the invoke
using the method name string and use a flow time.
And if the time is set to 0, this is in book.
Anyways, for better performance,
you can use coroutines instead,
but I just wanted to show you This invoke works.
You can see right here and the example how it works.
And finally, I'm going to give you a small hint.
Use the clip planks for the amount of time to wait.
So with that information,
I know this is one of the harder videos.
Challenges, I mean, if you find involve very tricky,
and even I had to do it multiple
times in order to get it 100% right.
You can still use coroutines, it's perfectly fine.
I just want you to know or have this tool in
your own arsenal just in
case maybe sometimes you do need it.
So that out of the way,
pause the video right now and go to the challenge.
Okay, welcome back.
How did you get on with that?
I hope it wasn't too hot.
So in here, what we're going to do is first of all,
we're going to go ahead and instantiate.
And what are we instantiating?
We're instantiating the FX
at the fax number that we have been sent.
And we need to choose the parent.
And this parent is going to be the camera,
but Maine, and it's going to be transformed.
So I'm not sure if we've ever access the camera dot main.
Maybe we did, maybe we didn't.
I hope it wasn't too hard of a challenge, but anyways.
This is how we make it as a child of the game camera,
you have the main camera.
And then after we do this,
we are going to create a method in here,
which is going to be a private void.
Destroy as affects gain object.
And in here we are going to destroy,
but what are we going to destroy?
You want to destroy these sound effects right here.
So I'm going to create a game object Via or no,
sorry, sorry for that.
I meant not create the game object.
I've already created it.
So v as f x will be equal
to the game object that we've instantiated.
And we are going to destroy the US effects.
And that's all that we need to do.
Then we have the invoke.
So invoke or no,
this is not the one that we need and invoke,
this is the one that we need.
In here.
We need to give it the method name,
so I'm going to copy the method name.
But in between these two little quotes,
and then we need the time and as I've said,
we need to use the time of the eclipse.
So how are we going to do this?
Well, we can use the effects.
Then we can get the component on it,
which is the audio source or the o force.
And then from there we can access the clip.
And using the clip,
we can access the length of that clip.
So I'll save that though,
what is happening in here?
First of all, we are instantiating
the sound effects based on
the sound effect number that we get.
We then get the camera or we make it a child of
the main camera by accessing camera
dot main taught transform.
I believe we went through this.
I just can't remember where.
But anyways, we make it a child of the main camera.
Why? Because we want the sound to
be exactly on the main camera.
Then we invoke a method that will
destroy the sound effect after a certain amount of time.
Because remember,
the objects once instantiated need to be destroyed.
And we don't want to have our hierarchy filled with
sound effects that we are no longer using or even
game objects that we are no longer using.
And then we've created
this destroy sound effect game object that
destroys the v sound effect
that we've instantiated up here.
So that although the way save that back in Unity,
open this up, let's run the game.
Save all of that because I'm
afraid of this crashing again.
So now when we run the game,
Let's open up the camera.
We can see right here the main cameras.
So when we should pause this, look at this.
So now we have the pistol shot. There we go.
So let's continue.
You can see that we have 1234 and all of them.
And you can see appear in the sound effects that
it's being affected by the sound effects appear.
Let's try this on. Okay, so we got the same gun.
No worries.
Let's try this again.
Maybe we get again.
We get again the pistol, please.
Can I have a shotgun,
please? Or a rifle?
That's good enough.
So now when we shoot, there you go.
You can see that all of the shots have been
instantiated in here and they are being played.
So I hope you enjoy it.
I hope you are committing
your changes and I'll see you in
the next video as you can see
our gain as much better now with sound effects.
So that done, I'll see you in the next one.
If you haven't already lever view, I'll see you.
Then.