We can jump as many times as we want.
So let me show you.
Let's click on this play button in the top in order to play our game.
And let's have a look on this.
Just wait for a little moment.
So if I press down space key, as you can see, we jump it a lot and we can jump as many times as we
want.
Let this player game object come to this ground.
And if I just press down space key.
As you can see, it's jumping a lot and it's gonna jump as much as we want.
So I'm going to get out of play mode and let's me, uh, let me show you one more time.
So I'm going to select my player game object in the hierarchy.
And I'm going to make the jump height a little bit less something like four.
And if I just click on this play button in the top to play our game, we're going to take a look on
that.
Uh, we can jump as many times we want, so just wait for it to come play our game.
So if I just press down space key, as you can see, we can jump even in the air.
So we need to stop doing that.
So let's get out of play mode.
What are we going to do.
We're going to create a parameter a bool parameter which can be true or false to just keep track of
if this player game object is standing on this ground or not.
If this player game object is standing on this ground, then we're going to make our player jump.
And if this player game object in the air, we're not going to make this player game object to jump
in the air.
So I'm going to drag it on top of this ground, because in the start of the game this player just gonna
be top of our ground.
So that's why.
So let's select our player GameObject in the hierarchy and open up the player script in the Inspector
by double clicking on it.
And let's go to the top of our class, something over here.
And we're going to make a parameter or a variable to just keep track of if this player game object is
standing on this ground or not.
And we're going to make a bool parameter, which can be true or false.
So I'm going to make a private so it doesn't need to be public.
So we just made it private.
So we're not going to able to see that parameter.
And in it our player skip in the unity.
We're not going to able to see that because as you can see we just made it private.
And that will be pulled which can be true or false.
And we have to name the parameter or the variable which uh, what we want to name it.
So I'm going to call it something like is crown is crown.
And let's start or close that off with semicolon.
And we're going to move over to this start function.
And we do know that start function get call in the start of the game.
So in this start of the game we're going to set that is crown parameter to true.
Why?
Because uh, this uh player game object in the start of the game just gonna be top of this crown.
So that's why it is Crown.
We're gonna set in the start of the game, uh, to true, because we're gonna be top of our crown,
so that's why.
So let's go to script.
So in this start of the game we're going to set is brown, we will set equal to true.
And let's close that up with semicolon.
So in this start of the game we set a second parameter to true.
Because in the start of the game, this player game object is just going to be top of this round.
So that's why.
And once we make this player game object jump so it's going to be in the air.
Then we're going to set that is ground parameter to false.
Uh y we're gonna set it to false because once this player game object jumps uh, it's not going to be
top of this ground.
It will be in the air.
So that's why we're going to set this ground parameter to false.
So we just created that parameter in order to check if this player game object is standing on top of
this ground or not.
So let's go to script now.
And over here.
As you can see we're checking if input dot get key Keycode dot space basically means if we press down
space key on our keyboard, then what we're doing we just making our player jump.
So at that same time, I want to check if this player object standing on this ground or not.
So if we press down space key at the same time, I'm going to check if this player game object standing
on top of this or crown or not.
If it's standing on top of this crown, then we're going to make this player jump.
Else we don't want this player game object to jump.
If this player game object not not in top of this crown.
So somewhere in the air, then we don't want this player game object to jump at all.
So that's why in over here if we press down space key on our keyboard or not.
At the same time, I'm going to check if our player game object is standing on that ground top of that
crown or not.
So we're going to check.
So we need to use this n in order to check with a second condition.
And we're going to check if is crown is equals equals to true means.
So we using this equals equals to equals equals sign in order to check.
And what we're checking we're checking if is crown equals equals to true basically means if we're standing
on top of this ground or not, because we created that ground parameter.
In order to check this player game object is standing on top of this ground or not, as you can see.
So what we're doing, we're checking if input dot get key Keycode dot space basically means if we press
down space key on our keyboard at the same time, we're checking his ground is equals equals to true
basically means if we're standing on that ground or not, so is ground is equals equals to true basically
means we're standing on that ground.
Then what we're doing, we're just making our player jump, as you can see.
And after jumping we're going to set this is gone.
This is ground parameter to false.
Why we're going to set it to false.
Because once we make this player game object jump.
So it's going to be in the air.
So that's why this game object not going to be on top of this ground.
So that's why we're going to set this ground parameter to false.
After making this player game object jump in the y axis.
So we're going to set after jumping.
So we're just making our player jump over here, as you can see.
So after jumping we're gonna set his crown.
So we will set is crown equal to false.
Uh, false.
And let's close that up with semicolon.
And we're gonna press down control S in order to save our script.
And now let's move over to unity.
So, uh, we're checking.
So we're checking, uh, if input dot get key keycode dot space basically means if we press down space
key on our keyboard at the same time, we're checking his crown is true, as you can see.
Means we just standing on that crown.
Then what are we doing?
We're just making our player jump.
And after that, we're just setting his crown to false.
As you can see, as soon as we just leave our crown.
So let's go to unity now.
And as you can see, we're not able to see that private parameter which is bool we recently created.
And let's suppose you still want to see that private parameter.
Then what you have to do you need to move over to inspector.
In the top right you can see three dots.
So just click there and then click on this debug.
And as soon as we have chosen debug underneath our player script by now we are able to see this is ground.
So let's click on this play button in the top to play our game.
You will see this ground in the start of the game is just going to be checked.
Basically means true.
And once we jump this is just going to be unchecked means false.
So if I just press down a space key on my keyboard, as you can see we just made our player GameObject
jump.
But the problem is that for the next time, if we just press the space key on our our keyboard, we
not making our player jump at all for the next time.
Even we just pressing down space key multiple times on our keyboard.
Why?
Because this is ground is false, as you can see underneath our player script.
So we're going to get out of play mode and I want to go back in normal mode.
So what we have to do we need to go this top right one more time.
Uh just click on that three dot, and then click on this normal in order to go back in normal mode.
So why are we not able to jump for the next time.
So let's move over to scrape because this is ground is false.
So once we make our player jump, as you can see we jumping and after jumping we just setting a is ground
parameter is false.
And once we press done space key on our keyboard.
As you can see we're checking if is ground is equals equals to true means we standing on that ground
or not.
So we do know that once we make, uh, once we set this is ground equal to false.
So this over here, the second condition is false basically means, uh, this one is false.
So once we make our player jump, we just setting is ground parameter to false.
And we're checking for the next time as well.
If we press down space key on our keyboard at the same time is ground is true.
And we do know that once we make our player game object first, uh, first jump, then we just setting
back, uh, sorry, we just setting is ground to defaults.
As you can see for the next time, this is Crown parameter is not true at all.
So this one is false.
So that's why we're not able to jump for the next time.
Because this n n both of this condition should be true.
Means if we press down space key at the same time is crown need to be true.
But we do know once we make our player game object for this, uh, once we make our player game object
jump for the first time, uh, after jumping, we do know which setting is crown parameter to false.
So this one is not true at all.
So we also need to set back this parameter to true once we uh uh.
So we also need to set back is crown parameter to true.
Once we collide with this crown, once we collide with this crown then we're going to make a parameter
to true.
And we do know once we're gonna set this is crown true parameter to true.
We're going to able to jump for the next time as well.
Because if we set it to true and we're checking over here.
If we press down space key on our keyboard at the same time, it's ground is true.
So if we set it to true, then we're going to able to make our player jump.
And after jumping we're just setting back a.
This is ground parameter to false as you can see.
So once when we need to set this parameter true parameter to true.
So once this player GameObject collide with this ground.
So let's suppose this player GameObject make for the first jump.
So it's going to be over here in the air.
And once this player GameObject collide with this ground then we're going to set this ground parameter
to true.
And we do know once we set back its ground parameter to true, we will able to jump for the next time
as well.
So we're going to use on collision enter 2D function.
So basically that function just gonna get called once we collide with something.
So let's suppose we collide with this ground.
So that's the that function just gonna trigger.
So let's move over to script.
And we're going to use on collision enter 2D function.
So we're going to make it in the bottom.
You can make it anywhere you like.
It doesn't need to be in order.
So and we're going to use this Oncollisionenter 2D function.
Make sure you adjust it as I.
So make sure you did as I did over here.
Make sure you just do the same thing.
So this Oncollisionenter 2D function, this function gets called when we collide with something.
So this is unity.
Callback function basically means this function will automatically get called by unity once we collide
with something.
So let's suppose this player GameObject collide with this ground.
Then this function over here get called and we can extract info what we collided with.
By going over to this collision in a parameter we can extract info like name of the game object.
We collided with the tag of the GameObject, anything like that.
But before we do, let's assign this ground as a ground tag.
So how we can assign this ground as a ground tag.
So you need to select the GameObject first in the hierarchy.
So in our case this ground we're going to select in our hierarchy.
So select the crown.
Then go over to Inspector and Inspector and you can see tag.
It says untag.
So just click there and then click on this add tag.
And then click on this plus icon in Inspector.
And you have to name the tag what you want to name it.
So I'm going to call it something like Crown.
And after doing that make sure you just click on this save.
And also you have to make sure select the Crown GameObject in the hierarchy.
So we're going to select our crown in the hierarchy.
And then go over to Inspector one more time and tag you can see untag.
So just click there and we're going to assign it with Crown.
So whatever we just recently name it.
Now we can see.
We assign it this crown as a ground tag.
As you can see in Inspector.
So we assign it this ground as a ground tag.
So now what we're going to do, we're going to move over to our script.
So this function get called when we collide with something.
And we can extract info what we collided with by going over to this collision parameter.
So let's suppose we just want to print out the game object we collided with.
So if this player game object collides with this ground then we're gonna extract the tag of that game
object.
Something like that.
So we're going to move over to script.
And in order to print out a message, uh, we do know we're going to use this debug dot log function.
And this debug dot log log function.
It's going to take the message in a string.
Or you have to pass the string.
So what we're going to do we're going to extract info the name of the game object or the tag.
So we uh we have to move over to this collision parameter.
So we're going to use this collision parameter in order to print out the name of the game object we
collided with.
So we're going to use this collision parameter.
So we're going to move over to collision.
Then we're going to add the game object as you can see.
And now we're going to access the name of the game object.
So we're going to use this dot and then name.
And then close that up with semicolon.
So this function get called by unity automatically once we collide with something.
So let's suppose this player can collide with this ground.
Then this function is just gonna get called.
And what are we doing inside that function?
We just extracting the name of the game object we colliding with by going over to this collision parameter.
As you can see firstly we just going over to this, we just using this collision parameter.
And by this collision parameter we just accessing the game object as you can see.
So collision dot GameObject basically means the game object we collided with.
And after just getting the game object we just extracting the name, as you can see, and we're going
to press down Ctrl S to save our script.
Now let's move over to unity.
So as soon as this player GameObject just wanna collide with this ground, the name of the game object
just gonna print out in our console tab.
So just let me show you.
So wait for a little moment to compile our script.
So we're going to select our console tab from here.
And let's click on this play button in the top.
So as soon as this player came up, it's just going to collide with this crown.
You will see the name of the game object just gonna print out in our console tab.
So let me show you.
So as soon as the game player just collide with this crown, the message in the console gets pop up
says Crown.
So if I just collide with second time, as you can see, as soon as this player GameObject collide with
this crown, the name of the game object just getting a print out in our console tab.
So let's get out of play mode.
Instead of printing out message what we want to do, we just want to set this parameter to true.
So we do know once we set back is Crown parameter to true.
We're going to able to jump for the next time as well because this is crown just going to be true.
And so both of these condition just getting true.
So that's why we will able to jump for the next time as well.
And after jumping as you can see which setting is crown parameter to false.
And we also need to set back is crown parameter to true.
So instead of printing out message, once we collide with this ground, we're going to set this ground
parameter to true.
And we do know once we set back is ground parameter to true, we will able to jump uh for the next time
as well.
So let's double click on that on this message in order to go that line of code.
Or you can simply move over to your Visual Studio.
So what we're doing, we're just printing out the name of the game object.
Instead of doing this thing.
We're just gonna check a if if we collide with this ground game object.
So we're going to extract the tag of that game object.
And we're going to check if this ground has a tag called ground.
Uh, so at that moment we're going to set its ground parameter to true.
Why.
Uh, because we need to check only with ground.
And we do know that, uh, and we can see if we select our ground GameObject in the hierarchy, we can
see in Inspector, we assigned this ground as a ground tag as you can see.
So what we're going to do there.
So let's go to script and we're going to check.
So we will use this if condition.
So make first bracket and some curly brackets.
So what we want to check over here.
We want to check whatever we uh just collided with.
If that game object has a tag called crown tag.
So if we collide with this crown as crown that we do know that we assign it this ground as a ground
tag.
As you can see in the inspector.
Then what we're going to do, we're going to set back is ground parameter to true.
So firstly we will move over to this collision.
So we're going to use this collision parameter in order to extract info the game object we collided
with.
So we're going to use this collision.
So let's use this collision.
So collision dot game object.
And then we're going to access the tag of that game object.
So we're going to use this dot and then tag.
And we're going to check if that tag is equals equals to ground.
So make sure you just make this quotation.
And in in between this quotation you need and you need to pass the tag name.
So we're going to check foreground.
So that's why we're going to pass this round.
And we do know we assigned that ground as a ground tag.
It's basically means we collided with this ground.
So what we need to do in that moment we'll set this ground parameter to true.
And we do know once we set back sorry is ground parameter to true, we will able to jump for the next
time as well because we're checking over here.
As you can see, if we press down space key on our keyboard at the same time is ground parameter is
true.
So both of these condition getting true.
So that's why we're going to make we're going to able to make our player jump.
And after jumping we're just sitting back is ground parameter to false as you can see.
So we colliding with this ground over here.
And it's basically mean what we need to do right now.
We need to set back.
So we're gonna set is ground equal to true.
And then close that up with semicolon.
So let me explain one more time.
So collision dot game object basically means the ground game object.
So let's suppose this player game object collide with this ground.
So Collision's GameObject basically means our ground GameObject by now.
So Collision's GameObject basically means the GameObject we collided with.
So we're gonna collide with this ground.
So that's why this collision GameObject basically means this ground GameObject.
And after going over to that GameObject, what we're doing, we just accessing the tag as you can see.
So we're gonna collide with this ground game object.
So we basically just going over to this ground game object.
And then we accessing the tag of this ground game object.
And we can see we assign it this ground as a ground tag.
As you can see in the inspector.
So basically means this over here just gonna be ground.
And we're checking if that ground is equals.
Equals to ground basically means the game object we collided with, if the tag is equals equals to ground,
as you can see.
Then what we're doing, we're just setting a bag ground parameter to true means we are setting it to
true means we're gonna able to jump for the next time as well.
So we're just accessing and we checking if the game object has a tag called ground.
So we're going to collide with this ground.
Then what we're doing we're just sitting back is ground parameter to true as you can see.
And uh so we will able to jump for the next time.
Uh, and then after making our player jump, uh, we just setting back is ground parameter to false.
And as soon as we go colliding with the ground, then we're setting uh is ground parameter to true.
So as soon as this player GameObject gonna collide with this ground, then we're just setting his ground
parameter to true.
And once we making our player game object jump for the next time, then we setting A is ground to false.
And as soon as this player game object is gonna collide with this ground for the next time, that is
ground parameter.
Just gonna be true.
Means we're gonna able to jump as soon as we're gonna collide with this ground.
Basically, that is the thing, and we're going to press down Ctrl s in order to save our script.
And let's move to unity by now.
So as soon as this player came out to collide with this ground?
You will able to see this player GameObject gonna just gonna be able to jump uh, as well for the next
time.
So let's click on this play button in the top in order to see that.
So as soon as this player GameObject just going to collide with this ground, then this player GameObject
just gonna able to jump.
As soon as it jump.
We just sending back is ground to false.
So as you can see we are only able to jump once this player GameObject just uh, standing on this ground.
So I'm going to click on this pause button in the top in order to pause my game.
And I want to make our player jump a little bit high.
So I'm going to select my player GameObject in the hierarchy.
And I'm going to set the jump height in our player script, something like uh ten.
And now if I just click on this pause button in order to just unpause the game, we can see player is
jumping a lot, but we're not able to jump in the air.
We only able to jump as soon as this player GameObject just colliding with this ground.
As you can see.
So I'm going to get out of play mode.
So I hope you get the idea that how you can make your player jump by using a bool parameter and collision
function in unity in with C sharp script, and we can see the jump height just set back to four.
Uh, and we just set it in ten while we, we are in play mode.
So make sure you just autoplay mode and then you can just paste the value.
So it's going to be saved.
So whatever you're going to do inside play mode while you are while you're just playing your game,
it's not going to be safe.
So make sure you just autoplay mode and then you just paste the number once again there.
So in my case I'm going to make the player jump something like ten and make sure you also go to overrides
in top of Inspector.
Then click on this apply all.
So the changes you made in in in this player shape in hierarchy it's just going to be save in this player
GameObject prefab as well.