Welcome back, my fellow game developers.
In this video, when we run the game,
when we had the peaky,
you can see that we have a balls menu.
We can either go to the main menu at works,
trust me, or we can resume the game.
If we resume the game, We are back and again,
we can also pause this again,
and instead of clicking on the one of the buttons,
we can hit the peaky.
But we are that this is something
that we'll also cover in the next video.
But for now, let's go ahead and create our pause menu.
Oh, okay, so now we want to
add the ability for our player to pause the game.
Our player, I mean,
the guy that, or the
girl that's actually playing our game.
So how are we going to do this?
Well, it's going to be fairly simple.
We are going to duplicate the depth screen.
And this will be the pause screen or pause menu.
So pause menu and hit Enter.
And here we are going to
have everything the same except for
the let me turn this on except for the lot.
So first of all, the blood,
we don't want the blood and the pause menu,
it will be let me set it on the one.
So we have the image in here,
but we want to make it just a little bit
faded so we can see the background.
So let's say 0.5, okay?
So we will have it at 0.5.
And then we are going to move the restart button.
And you just make the smaller move
the restart button here,
move the main menu here.
And instead of restarting
the level which you can keep if you want,
but we're going to make this one Resume button.
So Resume button and in the text in here
will just resume game.
So there we go.
Now we have these two buttons.
Obviously we'll need to change
the functionalities and here.
But that's something and I couple of minutes.
First of all, let's open up the UI Manager.
And in here we are going to do a couple of things.
First of all, obviously we need a reference.
So I'm going to create
a serialized field of the game object,
which is the pause menu.
Save that.
But the thing here is,
as we've said in the previous video, UI Manager, okay,
maybe he does turn on and off the pause menu,
but he is not responsible for actually pausing the game.
The level manager is the script
that is responsible for doing that.
So in the level manager,
we are going to create a public bool.
And we're going to call this,
the game is paused.
And obviously I don't want to make it public,
but we are going to be using it a lot.
You know what? Let's not make it public.
Let's make it a private variable,
and we'll create a way to access.
So a public bool is game paused,
and we'll just return.
So these are sometimes a bit cumbersome.
Maybe you're thinking, Well,
I'm creating a lot of gold.
But trust me, when you get to the higher levels,
to the professional level,
these are essential things to have.
Don't want your game to be
easily corruptible through the code.
So now that we have the game is paused until I manager we
have the pause menu we
obviously want to access in some way,
change the pause menu.
So I'm going to create a method in here that is going to
say that public void turn,
pause, menu on, off.
And it will take in a boolean which is
going to be the on, off.
And I know this is not
the best way to represent the variable,
but anyways, let's continue on. You'll see what I mean.
So in here we'll just going
to go ahead and get the pause menu,
dots directive to on, off.
So now we go back into the level manager.
And in here we are going to create a public void.
Pause, Resume game.
And what we'll do in here is we're going to check
if we are not in the game paused.
So we haven't paused yet.
Then we'll access the UI Manager.
Dot instance, dot, pause,
turn, pause menu on and off.
And what shall we set it?
Well, we will just set it to true.
Because if we are not unopposed game,
when we press some kind of key,
then we want to turn on the pause menu.
And else.
That means if we haven't paused the game.
So if we or excuse me,
if we are in a paused game.
Then we are going to do the same in here,
but instead we are going to turn it off.
And this will go to the UI Manager axis,
this one and turn it off.
So with that out of the way,
there is something that we still
need to do and that is actually
turn or change this game
as opposed using some kind of button.
And this is going to be your challenge to
resume the game using some kind of key.
So check for inputs and when pressed,
we should call the method and resume gain or pause
the game and make sure to change the status of the game.
So if it's paused or if it's not anymore,
so pause the video right now and go create an input.
Okay, welcome back.
So where do you want to put it?
I think up here.
So an update, we are going to check
if input dot get key down
and a key code for p.
Then we are going to pause, resume, the game.
It's that simple. And then end here.
If we haven't paused the game,
so the gain is not paused.
We click the peaky, we turned on.
So the turn on,
turn off is true.
So in here we are going to say that the game is paused,
is equal to true.
And duplicate this and move this down here.
False.
Save that.
So let's test this out.
I hope I didn't make a mistake with the false and true.
Why I believe it's correct.
So in the Resume button
and we're going to add something in here,
which is the UI canvas.
And inhere we are going to go to the UI Manager.
We are going to find turn,
pause menu on, off.
And as you can see, we have the option
to add either true or false.
And this is the actual boolean right
here that turns on all of the paths.
So back in our game and here,
let's first of all turn of the pause menu, run the game.
So and here we have the peaky.
We can go back to the main menu.
We can also hit the peaky than had the peaky back.
And then we turn off the pause menu.
There we go. But there is still one thing
which is the Resume button.
Now I'm going to go ahead
and add the onclick event in here.
And I'm going to break my own rule of adding
the level manager from
the manager's everybody right now is like,
Oh, don't break the rules.
But yes I am.
I'm going to go into the restored pause resume game.
So run the game.
And now we are going to press the key,
resume the game, and everything works fine.
But as you can see, if I pause the game,
I can still walk around.
I can still shoot enemies and I can still die.
Now, if you want to create a dark souls ask
type of pausing in
the game where the game continues in the background.
And you just sit there helplessly trying to
figure out what's happening in the menu. Be my guest.
If not, if you want the world stopped,
then continue on to the next video.
But before we do that, obviously
will need to commit our changes.
So see you then.