Let's add audio to our game.
But before you add audio to your game, make sure you select your main camera in the hierarchy and audio
listener is a component is attached to your main camera.
If it's not, then click on this add component and find the component by typing name of the component,
which is in our case Audio listener.
So attach that.
So I'm not gonna do it again.
I already have attached audio listener component as you can see inside this main camera.
So why this component is important.
Basically this component just gonna work as a microphone.
It's going to take sound from source and then it's gonna play our sound.
So that's why we need to uh make sure audio listener component is it's attached to this main camera
or not.
So that's why this audio listener component is by default attached to attached to this main camera.
Always.
So so let's uh, add audio to our game.
So firstly I'm going to create an empty game object for that.
So let's move over to this hierarchy.
right click or hit this plus icon.
And we're going to create an empty game object.
And we will call this game object something like audio or audio or audio Manager.
So I'm going to call it audio.
Let's go to transform.
Let's reset it.
And I'm going to select my audio GameObject in the hierarchy.
And I'm going to drag it in the top so I can see all the time.
And in order to play an audio clip we need to add a source called audio was sorry.
We need to attach a component called audio source.
So basically what you have to do, you need to select the game object first in the hierarchy.
Then click on this add component.
And we're going to add a component called audio source.
So we're going to type the component.
We're going to find the component by typing name of the component.
So in our case audio source.
And what we have to do now in this audio source component we need to drag the clip.
Which clip we just want to play in in inside this audio resource slot.
So we need to drag the clip over here.
So I have in my project app.
So I will move over to my project tab assets and then audio folder.
I have all of this audio as you can see.
Of course I will have a link in the resources of all of this.
So you can go and check that out.
So let's suppose I want to play this drum sound.
I hope you are able to hear it.
So I just want to play the drum sound.
So I'm going to select my audio GameObject in the hierarchy.
And let's select the jump clip from that folder.
And let's drag it over to this audio resource slot.
Basically the clip you want to play, you need to drag it over here.
And if we just click on this play button in the top by now, you will be able to hear that sound right
after the game start.
Why?
Because this play on awake is checked, so that's why.
So just wait a little moment to start our game.
So as you hear, as soon as the game started, that jump sound just gets played.
So I'm gonna get out of play mode.
So that's why we need to uncheck this play on awake.
So the sound.
Not gonna play right after the game start.
And if you want your sound to be low then you can just check this loop over here.
It's gonna play the sound in looping.
And you can just play around with these properties like volume pitch, studio pen and as well spatial
blend.
So if you just drag this spatial plane to 3D, the sound just gonna work as 3D.
So if the sound source is far from our main camera or this audio listener component which is attached
to our main camera, we do know and it's work as a microphone.
So if you drag any audio source far from your audio listener component, the sound just gonna the volume
of that sound will be down because it's too much, uh, too much far away from this audio listener component
on from this main camera.
A in this camera we do no audio listener component is added.
We which work like as a microphone.
So that is the thing.
I hope you get the idea.
But we don't want to play this audio uh, so or sorry this dumb claim whenever the game starts.
So that's why we just uncheck the loop time.
And we want to play this jump sound.
Once we just play, uh, play the jump animation clip.
Once our player game object jumps.
At that moment we're gonna play this jump sound or this jump clip.
So we're going to create a script for that in order to make our jump sound.
When we're jumping for that, let's create a script.
So we will move over to our project tab, then assets, and then in script folder let's right click
there, then create Monobehaviour script.
And you can name that script whatever you like.
I already have created a script called Sound Manager.
As you can see.
You can name it to something like Audio Manager is as well.
It's totally up to you.
So I'm going to select my audio GameObject in the hierarchy, and let's select that Sound Manager script
and drag it over to this Add Component section.
And we're going to double click on this to open it up in Visual Studio.
And what I'm gonna do I'm going to remove this start and update function I don't need.
Instead we need a reference of this audio source.
So if we move over to unity we need a reference of this audio source component.
Then we can play this jump clip.
So we need a reference of this audio source.
So let's grab the reference first.
So let's move to our scale and let's create a field for that.
So we're going to make a serialized field.
Or you can just leave it public as well.
It's not complicated at all.
So I'm going to make public.
And the component we're looking for is the audio source.
So we're going to type audio source audio source.
And we have to name the source what we want to name it.
So I'm going to call something like jump audio source jump audio source source.
And let's close that out semicolon.
And let's press down Ctrl S to save our skip.
And let's move to unity.
So underneath our sound manager Skip we will see an empty field of jam audio source just gonna pop up.
So what we have to do there, we need to drag this audio source, uh, reference into that slot.
So we're gonna have the reference, and then we can just play the sound that we really want to play
once we're making jumping our player game object.
So as you can see, as soon as our sound manager script gets compiled, we can see a field just pop
up underneath our sound manager script jump audio source and it says none audio source.
So what we have to do over here, we need to take this audio source and let's drag it into that field.
And now we can see we have the audio source reference basically this one.
And now we are ready to play.
So let's open up our script quickly.
And for that we're gonna do we're gonna make a function in order to play the jump uh, audio clip.
So we're gonna make this function public.
Why?
Because we're gonna call this function from another class or another C sharp script.
So that's why we need to make this function as a public.
And for function we're going to type void and we have to name the function.
So I'm going to call it something like play jump jump audio sound or something like that.
So play jump sound as jump sound.
And let's make first bracket and some curly brackets.
And what we want to do inside this function, we just want to play the jump clip.
So basically what we need to do, we need to move over to this audio source.
And we're going to use play function in order to play this jump audio clip.
So we need to use that.
So let's move over to first or that audio source component.
So in our case audio source and we do know we have the reference.
So let's move over to jump audio source.
Basically we just simply moving over to that audio source component.
And we're gonna use this dot play function in order to play the clip and then make first bracket and
close that up with semicolon.
And we're going to press down Ctrl S to save our skip.
And let's move to unity.
So if we call that play jump audio source function or that play jump sound, it's gonna what it's gonna
do.
It's gonna move over to that audio source, which in our case, dumb audio source or that audio source
component.
And we're using this play function in order to play our jump clip.
So we also need to call the function.
But when we shoot once this player game object just making our jump when it's jumping, we're gonna
play that function.
Uh, sorry.
We're gonna call that play jump sound function and that function just moving over to this audio source
and then using play function in order to play this jump clip as you seen.
So let's call the function.
So we're gonna call the function inside our player skip.
So let's select the player game object in the hierarchy.
And let's open up the player skip.
And let's move over to that line of code where we're jumping.
So we can see we're jumping over here as you can see.
So we're just making our jump, then setting his ground then making our jump animation clip.
So also we just want to play the jump audio source uh or that jump Jam sound.
So what we will do, we will use this.
Find any object, uh, by type and then make these arrows.
So if you have used singleton then you can simply move over to that script and you can just pass the
name of the script instead of doing like this.
So you can use the singleton pattern, which I have explained in my, uh, clean code, uh, lecture.
So you can check that out in order to use singleton pattern.
So this we need to use this find any object by type and into this arrows.
We need to pass the script name that we are looking for.
So simply we're gonna move over to that sound manager script.
And we do know inside that script we have created that some, uh, some sound, uh, play play, jump
sound, uh function.
So we're going to call that.
So let's head over to our sound manager script first, then make first bracket.
And from there we're going to call the play jump sound function.
So let's call it.
So we're going to use this dot play jump sum function.
And uh make first bracket and then close that up with semicolon.
And we're going to press down Ctrl S to save our scope and save our script.
So once we're making our jump then what we're doing we're just moving over to our sound manager script.
Basically we're just going over to this Sound Manager C class and then calling this jump play jump sound
function.
And what this function is doing is just going over to that audio source component, which is in our
case, jump audio clip, uh, audio source.
And then using this dot play function in order to play the sound.
And let's move to unity by now.
Uh, firstly make sure you just press down Ctrl s to save your script.
And after that just move over to unity.
So I'm going to press Ctrl S to save my script.
And let's move to unity.
And it should be fine.
So I'm going to select my audio GameObject in the hierarchy.
And let's click on this play button in the top.
And you will see as soon as we're gonna make jump our player, jump the jump sound.
Just gonna play right away.
So if I jump, as you can see, as soon as we jump in, we just are making our player jump sound.
So this is, uh, cool.
I hope you get the idea that how you can do it and let's, uh, play as well.
Explosion sound.
Basically, once this player game object dies, it should play the explosion sound once it's died.
So let's get out of play mode and let's do that thing as well.
Uh, so for that, we're going to add a another audio source component, uh, for that explosion sound.
Once this player game object dies, we're gonna play that sound.
So that's why we're gonna add a another audio source component for it.
So let's select the audio GameObject in the hierarchy.
And let's click on this add component.
And we're gonna add a another audio source.
And we need to drag the clip over here that we want to play inside this audio resource slot.
So let's move to project app Assets and an audio folder.
And I'm going to select my this explosion audio clip, and I'm gonna drag it over to inside this audio
resource slot slot.
And I'm gonna uncheck this play on awake.
Basically, I don't want to play this explosion sound clip as soon as the game started.
So that's why I just uncheck and let's, uh, make a another function in order to play this explosion
sound clip as well.
So let's open up our player script.
But before we do I'm gonna move this audio source component up direction.
So I'm going to right click on it and let's click on this move up.
In order to move this component in top of this Sound Manager script.
And let's open up our script first.
So we also need a reference of this audio source of this explosion sound clip as well.
Explosion sound clip audio source.
We need reference.
So let's go to script and let's make another uh, another parameter for that.
So let's make this one public so we can see or we can adjust underneath our sound manager script.
And that is audio source that the component we're looking for and we have to name the component.
So I'm going to call this one something like explosion.
Explosion audio source.
And let's close that up with semicolon.
And let's press down Ctrl S to save our script.
And let's move to unity, uh, in order to drag the reference in that slot.
And after that, we're going to make the function in order to play the explosion sound as well.
So just wait for to compile.
And once it's done, we are ready to do that.
We need to drag in, uh, inside our sound manager script.
So now we can see another explosion audio source component just appear.
So we're going to take this audio source.
But make sure you just drag the explosion audio source component into that slot instead of this jump.
So we're going to take this audio source and let's drag it right there.
Make sure you before you drag it's a correspond to a correspondingly match up.
So make sure you just do that.
And let's open up our script by now.
And let's make another function in order to play that explosion sound effect as well.
I'm gonna make it in the bottom.
You can make it anywhere you want.
It doesn't need to be or in order.
So let's make another.
So we're making this, uh, public, this function public because we're going to call this function
as well from another C sharp class or another C sharp shape.
And for function we will type, uh white.
And we have to name the function what we would like to go with.
So I'm going to call it something like uh play explosion explosion sound effect explosion sound something
like that.
Make first bracket and some curly brackets.
And what we want to do inside this function, we basically move.
We need to move over to this audio source component.
And then we need to use the play function in order to play the play the explosion sound effect.
So let's use that.
So firstly we're going to move over to explosion audio source, and let's use this dot play function
in order to play the sound.
And let's make first bracket and then close that up with semicolon and we will press down space.
Sorry Ctrl s to save our script.
And we're going to call the function once our player game object dies as well as we're going to call
the function once our enemy game object dies.
Why.
Because we do know once our enemy and this player just dies, we need to play the explosion sound and
it's gonna make cool.
So that is the thing.
We're gonna call it multi, um, multiple times.
So let's select our player game object first.
So we're going to select our player game object in the hierarchy.
And let's open up the player scape.
And let's move over to die function where we destroying our player game object.
And we can see we're destroying our player game object here.
And I'm gonna make it in the bottom somewhere before we destroy our player game object.
So let's use this.
Find any object by type in order to call the function, or in order to head over to that script.
So into arrows.
We need to pass the script name.
So in our case sound manager let's make first bracket.
And inside that sound manager C sharp class or that script.
We just created that play uh play explosion sound.
So let's call it here.
So we're gonna call Dot play explosion sound.
And let's make first bracket and some enclose that up with semicolon.
And let's press down Ctrl S to save our script.
And I'm gonna copy this line of code by Ctrl C, and I'm gonna paste inside my anime one as well as
anime two, where basically anime is dying.
So let's head over to this anime script.
And anime is dying here, as you can see.
So we also want to play once our anime GameObject dies, we just want to play the explosion sound.
So I'm gonna paste over here before we die.
So I'm gonna paste it here as well.
And let's press down Ctrl S to save our script one more time.
And let's move to this anime two script where our anime two GameObject dying.
So here we're just taking our camera.
And after that I'm going to paste this line of code and let's press down Ctrl S to save our script and
let's move to unity.
So basically we're just calling the play explosion sound whenever our enemy as well as our player game
object dying.
We're just calling that function.
And that function basically moving over to that audio source, which in our case our explosion audio
source component, and then using that dot play function in order to play the clip that we want to play.
So if I select my audio GameObject in the hierarchy, you can see we have to reference.
And now it's fine.
So let's click on this play button in order to play our game.
And let's have a look on this.
So just wait and we're gonna see it.
So let's die.
I'm gonna.
So let's die.
And we're not dying at all.
So we still able to jump.
So, as you can see, as soon as we died, that sound just clip that explosion sound.
Just play a place right away.
And I'm gonna click on this retry button in order to, uh, see it one more time.
So let's take damage from this enemy or this enemy one as well, or we're going to destroy this enemy
game object as well, in order to see or to hear that, uh, jump sound clip, which is we doing?
As you can see, as soon as enemy one as well as this enemy two is dying.
That sound just playing right away, which is the explosion sound that we want to play.
And you can just ignore these errors in the console tab, and we can see as soon as our player GameObject
dies as well.
That explosion sound clip just plays right away.
So basically that is the thing, and I'm gonna get out of play mode and let's make this one audio game
object as well, a prefab.
So we're gonna have the copy and we can use or reuse this audio game object in order to play in multiple
levels as well.
So let's select our audio GameObject in the hierarchy, and let's drag it over to this prefabs folder
in our assets folder in order to turn it into a prefab.
Uh, so I hope you get the idea.
And let's suppose you just want to get it in more depth about audio manager and you don't want to use
sorry, you don't want to use, uh, in this way.
Then of course I will have a video, another video in the resources.
You can go and check that out if you just want to learn about more about audio in depth deeply, then
you can check that out and you can use that audio manager script or the sound manager script there,
which is pretty much fine instead of using this way.
So this basically is a beginner tutorial or this beginner course.
So that's why I'm just explaining in depth way.
I hope you get the idea.
And now we need to play the theme that we want to play or the music we just want to play.
So we're going to select our audio game object in the hierarchy.
And let's click on this add component.
And we will add a another audio source component in order to play uh the theme.
And let's click over here in order to expand.
And we need to drag the clip which clip we want to play.
So I'm going to head over to my audio folder.
Let's suppose I want to play this one.
So this uh theme as you here heard.
So I'm going to select that and drag it over to sorry I'm going to drag over to this audio resource
loss slot.
Uh slot.
So let's select that clip and drag it here.
And I'm going to leave this play on awake.
Why.
Because I want this theme or the music as soon as the game starts.
And I want this theme or the music to be looped.
So that's why I'm gonna also check this loop time or this loop box as well.
And I'm gonna leave this play on awake because I want to play the music or the theme as soon as the
game starts.
So that's why I just leave this.
And also we just check this loop in order to loop our music.
And let's click on this play button by now, and you will hear the sound as soon as the game's gonna
start, the theme or the music will be play right away as well, so have a look on it.
As you can hear, as soon as the game started, I'm gonna get out of play mode.
Uh, the that music or the theme just plays right away because we just, uh, check that play on awake
as well as, uh, that, uh, basically loop time and I'm gonna move over to my overrides and let's
click on this apply all and make sure you just make this audio game object as well a prefab.
So we're going to have the copy and we will use this audio game object or all of these things as well
in our mano scene.
Because in mano scene we also want our theme to be play.
So that's why.
So let's head over to our mano scene.
So I'm going to move over to my project tab First Assets and then since folder.
And let's double click on this mano in order to move to that scene.
And let's click on that save button.
So whatever I have done so far inside my sample scene or in which scene we're just playing our game,
it's going to be saved.
So I just want to save.
So that's why I'm going to click on this save button.
And as soon as we click on save, we can see we just headed over to our scene.
And whatever we have done so far inside that sample scene, it just saves a really apologize for, uh,
the car just, uh, passing by next to my home.
I'm really apologize.
And it's just making noises too much.
And let's move over to project tape one more time than assets and then prefabs folder.
Basically I'm going to move over to that folder where I just turned that audio game object as a prefab.
And I can see it over here.
So I'm going to select that one and let's drag it over to this hierarchy as well as you can drag it
in the scene view as well.
I'm going to drag it over to my hierarchy.
And let's click on this play button by now.
So it should be fine.
So as soon as our menu is going to load up that theme or the music just going to play right away because
we just have done the thing there.
So let's have a look on it.
And we can see as soon as we just, uh, the menu just loaded up that theme or that sound just plays
right away.
I hope you get the idea that how you can add audio to your game, and we need to press down control
S in order to save our project.
So I'm going to press down control S to save my project.
And it's done.