Let's make the jumping to do so.
We will open up our player script.
So let's select the player GameObject in the hierarchy.
And let's open up the player script by double clicking on it.
And for that we're going to move over to this update function.
So let's go there and we're going to do it over here.
So what we will do uh we're going to create a variable in order to uh keep track of how many jumps we
want.
Since I want a double jump.
So I'm going to create two variables for it.
So let's go to the top of our class and let's make one.
Uh sorry.
We're going to make a one serialized field.
So this will be serialized field private.
And we're going to call this one this will be float.
Or we can call this uh should be integer as well.
But let's leave it with float variable.
And we're going to call it extra jump.
So let's call it extra jumps jumps.
And let's create one more variable and we're going to make this one private.
Uh, it doesn't need to be public.
So we're just creating two these two variables to just keep track of how many jumps we want.
So we're going to make private, uh, private float.
And we can call this one total jumps.
Total, uh, jumps.
Let's call it total jumps.
And let's close that up with semicolon and extra jump.
By default I'm going to set equal to two f.
Set equal to 2FYI set equal to two f since I want two jumps.
So that's why.
And I'm going to move over to this start function.
So this start function get called in the start of the game.
So in the start of the game what we want to do we're going to set our total jumps equal to our extra
jumps.
Extra jumps.
So this total jumps right over here.
By default it's going to be zero.
But in the start of the game what we're doing we're just setting extra total jumps equal to our extra
jumps, which is two.
As you can notice.
So this total jumps going to be two since this extra jumps is two.
So now what.
So what we're gonna do now we're going to move over to this update function.
And over here we're going to check.
So we're going to make this if condition.
Let's make first bracket and some curly brackets.
And what we want to check over here.
We basically want to check if input if input dot get key down.
And let's make first bracket.
And we're going to pass Keycode key code.
You can pass any key that you want to check with.
So I'm going to pass key code dot space.
My it's basically means if input dot get key down key code dot space.
It basically means that if we press down space key on our keyboard at the same time we're going to check
a, an an a, our total jumps if it's greater than zero.
Basically we have total jumps.
If that is the case, then we're going to make our player jump.
So to make our player jump we can create a function for it.
So let's make it over here.
And that function uh we're going to call it later through a button.
So that's why we're going to mark this function as public void for function.
And we're going to call this function to something like jump.
Let's make first bracket and some curly brackets.
And what we want to do over here.
We basically want to make our player jump.
But before we make we need a reference of this rigid body 2D of this player game object.
Because this rigid body 2D is responsible for velocity, adding force and all of those things.
So that's why we need a reference of this rigid body 2D, uh, so we can make our player jump.
So let's open up our player skip, and let's go to the top of our class.
And let's grab the reference first.
and it doesn't need to be public.
So we're going to make it private.
And the component we're looking for is a rigid body 2D.
And let's call it something like RB.
And let's move over to this start function.
Because in the start of the game we want to grab the reference since we made this field private.
So we're not going to notice underneath our player skip right over here.
So that's why we're gonna do with start function to keep our code a little bit clean.
So what we will do we will use this this dot get component.
And the component we're looking for is a rigid body 2D.
So we're going to pass this rigid body 2D and make this first bracket.
So what we simply doing we just taking our rigid body 2D basically this rigid body 2D of this player
game object.
And now what we're gonna do, we're gonna store this rigid body 2D into this RB field, this rigid body
RB.
So we basically just taking that and now we're going to store it.
So let's store into this RB equal to this.
Basically we're taking this rigid body 2D.
If I move over to unity editor we basically taking this rigid body 2D.
This basically is just referencing itself.
And we're just taking this rigid body 2D component which is dot get component.
And we storing this rigid body 2D into this RB as you can see.
And now we're going to move over to this jump function.
So over here what we're gonna do we're going to create a vector to a vector two.
And we can call this one something like velocity.
And let's set equal to rv dot linear velocity.
We need to set this linear velocity.
So uh before it was something like velocity.
But in unity six we have to type this RB dot linear velocity.
And now what we're going to do we're going to make a uh sorry.
We will uh go to velocity.
And then we're going to access the Y.
And we will set equal to some, uh, some around something like ten F.
And after setting that we're going to set RV dot linear velocity equal to our velocity.
So let me explain.
Firstly we just setting uh this we just created this vector to velocity which gonna have x axis and
y axis.
So we setting equal to rb dot linear velocity.
Basically we just going over to this rigid body 2D of this player uh game object.
And we taking the linear velocity of it and we storing into this vector to velocity.
And after that we basically want to jump in the y axis.
So if I select this move tool we only want to jump in the y axis.
So that's why we just only access the y.
As you can see velocity dot y.
And you can access the x axis as well like velocity dot x.
Since we jump over the uh, since we want our jump in the y axis only.
So that's why we access the velocity dot y and we set equal to ten.
And after that once again we just, uh, going over to this linear velocity of this rigid body 2D of
this player game object, and we say, uh, we just setting equal to velocity after only modifying the
y axis with ten unit, as you can see.
So we're going to jump in the y axis with ten unit.
Firstly we created the variable which is vector to velocity.
And after modifying the y axis we again setting velocity are basically rigid body velocity.
Linear velocity equal to this velocity after modifying the y axis only.
And now what we're going to do we're going to call this jump function over here.
So once we press down space key on our keyboard at the same time total jumps is greater than zero.
And we do know that in the start of the games this total jumps just gonna be two, since we're setting
equal to extra jump, jam.
As you can see and extra jams, by default we set two.
So this one over here is two.
So that's why this total jump is going to be two.
So total jumps will be greater than zero.
Basically means this condition is true.
And also we press down space key.
Then what we want we want jump.
So we're going to call this jump function and jump function.
Just just going to do these things.
It's basically just going to make our player jump in the y axis with ten unit as you can see.
So let's call the jump function over here.
And let's make a first bracket.
And then close that up with semicolon.
And after jumping what we want we want to subtract from this total jumps since we made our jump.
So we can subtract by one unit.
So we will set our total jumps minus equal one.
Basically we just subtracting.
Or you can simply do minus minus.
It's basically means the same thing which is subtracting one.
So if we press down space key on our keyboard at the same time, total jumps is zero.
So if we press done for the first time, then we're going to make jump.
So this total jumps is two.
So two minus one is just going to be one.
And if we press down second time on our keyboard space key then this right over here total jumps is
one.
So which will be still greater than zero or greater than zero means we will be able to jump for the
next time as well since this total jumps is one.
So we jumping and after jumping once again we subtracting.
So one minus one gonna be zero.
And if we press down space key third time and and now this total jumps is zero.
And zero is not greater than zero.
Zero is equals equals to zero.
Basically this condition over here becomes false means we're not going to able to jump since the in
this end condition put this condition should be true.
Else we're not gonna basically we're not gonna able to make our player jump for the third time.
So I hope you get the idea that what we're doing over here, and now we're gonna press down Ctrl s to
save our skip.
And now let's move over to unity.
So we will be able to notice our player.
Just gonna make two jumps two jumps.
And it's not going to make the third jumps because we set our total jumps to extra jumps, which is
by default we set equal to two since we want to jump.
But if you want triple jumps then you can set a extra jumps to three.
So that's why we mark that variable as serialized field.
So we can notice underneath our player skip.
So just wait a little moment.
Once it's compiled we can see uh, we can notice underneath our player skip.
As you can see, as soon as our player skip gets compiled extra jumps big as by default we set two.
And now if we click on this play button in the top in order to play our game and press down space key
on our keyboard.
You will see our player just jump in the Y axis with ten units, as we said, and we're just gonna able
to jump two times.
So if I press run first, then second, as you can see.
But if I try to press down space key on my keyboard, it's not jumping at all.
Why?
Because if we move over to unity, uh, sorry, uh, code, uh, this total jumps became zero.
We just loses all of our jumps after jumping.
We just subtracting, as you can see.
So this total jumps is now something like zero.
So once it is zero then we're gonna set back again to our total jumps to extra jump.
So we can able to jump for the next time as well.
So what we need to do we're going to create a variable, uh, something like scrum.
And that will be a bool variable to just keep track of if this player is standing on this ground or
not.
So we're gonna to do that.
So if it's standing on this ground then we're going to set our uh this total jumps to our extra jumps
one more time.
So this condition just going to be true.
So we are just going to make, uh, our player jump, uh, one more time.
One more time, basically two times.
Uh, we can able to jump once we are standing on that ground.
So we're going to create a variable for it.
So let's get out of play mode and let's move over to our script.
And for that we will go to top of our class.
And it doesn't need to be public.
So we can simply make it private.
So this will be private and that will be pool which can be true or false.
And uh, this variable just going to keep track of if we are standing on that ground or not.
So we can say we're going to say something like is ground.
And uh, and let's close that up with semicolon.
So basically what we want to do, we basically want to uh, create a, we will create a point somewhere
around here.
And from that point we're going to create a circle.
And if that circle collide with this ground, then we're going to set our total jumps to our extra jump.
Basically, we just want our player to jump if that circle collides with this ground.
So we're going to create the circle.
So let's create an tile game object.
And from that point we will create our circle.
And if that circle collides with this ground then we're going to make our player jump.
Else we not gonna make it because the player will be in the air.
So that's why.
So let's create a point.
So we will select our player GameObject in the hierarchy.
Let's right click on it.
And let's create an empty game object.
And we will call this GameObject to something like Round Check.
So let's call it Ground Check.
And let's select the move tool from here.
And let's move it in the bottom somewhere around here.
And we're going to choose an gizmos for it.
So let's choose this one.
And this seems fine.
And so from this point we want to create the circle.
So let's open up our script and we're going to do it over here inside this update function.
So let's do it here.
Let's do it before this if condition.
So what we're going to do to create circle we need to use this physics 2D physics 2D dot overlap circle
overlap circle sorry physics 2D overlap circle.
And this function over here as a first argument, it's going to take the point from where we want to
create the circle or the position.
So we're going to create the circle from this ground check point.
And the second thing it's going to take the radius.
And the third argument it's going to take the layer mask basically which game object we want to target
since we want to target this ground.
So we need to assign this ground as a ground layer.
But before we do so, let's make this comment and comment and don't interrupt our quotes.
So basically just type, uh, just uh, type two forward slashes and let's go to top of our class because
we need to, uh, grab those, uh, things.
So first thing, what we need, uh, we need a point.
So let's make a serialize field private.
And for point or position, we're going to type transform, which is responsible for position or point.
And uh, and we can call this one ground check ground check point, something like that.
And the second thing we need the radius.
So let's make a another class field private, uh, float.
And we can call this one check radius radius.
And by default I'm going to set something like point two.
Uh, if a smaller number and the third argument, it's going to take the layer mask or which game object
we want to target.
So in our case we're gonna target this crown.
So as a layer mask we're gonna choose that crown.
But before we do let's assign so serialize fill private layer mask.
And we're going to call this one target layer or let's call it what is Crown as simply what is Crown.
And let's close that up with semicolon.
Let's press down Ctrl S to save our script.
And now let's move over to unity.
So now let's hope it's gonna work.
Basically underneath our player skip we will able to notice those variables.
So the first thing will be our crown check point.
So we need to select our crown check.
And we need to drag it in that slot.
So we will have the point.
So as you can see as soon as our player skip compiles crown check point.
And it says none transform basically empty.
So we're going to take our crown check underneath our player game object.
Let's drag it there and check radius.
By default we just given 0.2.
You can just go with any number that you like, but make sure you go with smaller.
And what is ground?
We're gonna choose our ground, but before we choose our ground, we need to assign this ground as a
ground layer.
So how we can do that?
Firstly, we need to select our GameObject in the hierarchy first.
Then go over to Inspector.
In the top right you can see layer and it says default.
So just click there and then click on Add Layer.
And we're gonna add a new layer.
And you can name the layer whatever you want.
So I'm going to call it ground.
So let's call it ground.
And let's select our player script one more time.
And what is ground.
As you can see.
Sorry.
We're gonna choose our ground.
And we need to assign this ground as a ground layer.
So we already created.
So we're going to choose our GameObject one more time in the hierarchy.
Then go over to Inspector.
As you can see layers is default.
so we will choose ground.
Now we assign it this ground as a ground layer.
Now we will select our player skip.
And what is ground.
We're going to choose our ground.
Now the circle only going to check for with this ground.
And we just assign it all of the variables.
And we are ready to create that.
So let's open up our player script by double clicking on it.
And let's remove these two slashes.
And let's use this function.
So this overlap as a first argument.
It's going to take the point from where we want to create the circle.
So we're going to pass our ground checkpoint dot position and ground checkpoint dot position.
We do know that ground check.
We just pass our ground checkpoint as you seen.
And the second thing it's going to take the radius.
So we will pass our check radius.
And the third argument it's going to take the layer mask.
So we will pass our what is ground and what is ground.
We have chosen our ground.
And let's close that up with semicolon.
So this function right over here, it's gonna create the circle around this ground check point with
this much of radius and just going to target our ground.
So it's gonna create a somewhere around here.
And if that circle really collide with this ground, then this function over here, it's going to give
a true value.
Else it's going to give a false value.
So what we will do we're going to store that result into this ground parameter.
Basically we created this parameter to just keep track of if this player is standing on this ground
or not.
If it's uh, then uh, then we want we gonna make jump.
So what we will do, we will set our is crown is crown equal to this.
So this, uh, basically this physics 2D dot overlap circle can be true or false depending upon if the
circle is really colliding with this crown or not.
Let's suppose the circle collide with this ground.
So this function over here, it's gonna be true.
So And which setting is ground equal to this as you can see.
So that's why this is ground going to be true and is ground going to be true.
And let's suppose the circle don't collide with this round.
And then this function over here it's going to give us a false value.
So that's why this is ground right over here.
It's going to be false.
So what we will do.
And we created this parameter to just keep track of in in order to standing on this ground or not.
And if this ground is true basically means this player is standing on this ground.
So what we need to do, we need to set our extra jumps.
Sorry, total jumps to our extra jumps.
Uh, since we are standing on this ground.
So that's why.
So what we're gonna do now, we're going to check, uh, so we will use this if condition.
Let's make first bracket and some curly brackets and what we want to check over here, we're going to
check if is ground is equals Equals to true basically means that that player is standing on this ground
or the circle really colliding with this ground.
So what we need to do now, we basically need to set our, uh, total jumps to our extra jumps.
And we do know once we're going to set back, uh, so we will able to jump, uh, for the next time
as well, we can make double jump for the next time, or you can just leave it like this, uh, is ground.
It's also basically means true means we are standing on this ground.
So now what we're going to do, we're going to set our total jumps equal to our extra jumps.
And let's close that up with semicolon.
And now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
And we will able to notice as soon as this player just gonna stand on this ground or the circle collide
with this, uh, ground, then we just setting extra total jumps to our extra jumps.
Basically, we simply just making jump.
Uh, we just created that parameter to just keep track of if we're standing on this ground or not.
Uh, but it would be nice if we able to see our circle, uh, to the C circle or to draw the circle,
we need to use another inbuilt function of unity.
So let's open up our player.
Skip one more time and let's do it in the bottom.
You can do it anywhere you want.
And make sure you just do as I as I have done over here.
It's a void Ondraw Cosmo selected function.
So this function gets called when, uh, sorry, it's called by unity itself.
And this function just gonna draw whatever you want to draw.
So what we're going to do, we're going to draw the circle.
So that's why we're going to use this gizmos dot draw where sphere function and this function just gonna
draw the circle.
So first thing it's going to take the point from where we want to create the circle.
So we will pass our ground checkpoint position.
From there.
We want to create the circle.
And the second thing it's going to take the radius.
So we will pass our check radius.
And let's close that up with semicolon.
But before we draw the circle we need to check.
Let's suppose if you haven't dragged this ground check to this ground check field basically means this
ground check point is empty means none.
So over here, as you can see, we still are using this function.
And we're just trying to finding that position.
And we're drawing the circle with this much of radius.
And if you haven't assigned so this function not going to able to find this ground check point.
So that's why it's gonna throw us an error.
So before we draw the circle we need to check if we assign it our ground check point or not.
So that's why before we draw, we will check if our ground checkpoint, if it's not equal to null,
not equal to null.
Let's make this a curly brackets.
Then we're going to draw the circle.
So we're gonna cut this by control X and we're going to paste it inside the control V.
So what we simply have done over here, we basically checking if our ground check point is not equal
to null basically means that we just drag this ground check to this ground check.
And this field is not empty.
Then what we're doing then we're just drawing the circle, as you can see.
So before you draw the circle make sure you assign it.
So that's why we're using this if condition.
And if you want to choose another color you want to draw with.
So before you draw you can select the color.
And in order to select the color you can use this gizmos dot color equal to color that you like.
So I'm going to set color dot yellow.
And let's close that up with semicolon.
And now we're going to press down Ctrl S to save our script.
And this function automatically gonna call by unity itself.
And it's going to draw the circle Uh, from our from checkpoint.
And now we're going to able to notice that circle, and we can play around with our, uh, check radius,
uh, in order to adjust the radius of that circle.
So just wait a little moment.
As you can see, as soon as our script compiles, we can notice, uh, our, uh, circle from this ground
check point.
And we can make this a little bit small by playing around with or just messing with this check radius.
We can make it even more smaller, but I'm going to leave with 0.2.
Basically, choose a smaller number.
And now we should able to see.
So if I just press uh click play button in the top you will see uh, sorry.
Uh, you will see that as soon as we're gonna collide with this, uh, ground or just gonna stand on
this ground, we will able to notice this player will jump.
Else it's not gonna make jump at all.
So if I press down space key.
As you can see, we only able to make jump once the circle is colliding with this ground and we only
just are making two jumps.
As you can see, not more than that.
One, two and not more than that.
But as soon as this player just colliding with this ground or this circle collide with this ground,
we able to make jump because we just setting this total jumps to our extra jumps, which is two.
And let's suppose you set it to one extra jumps.
So if I set to one we just gonna only able to make one jump.
As you can see we not making more than that.
Sorry.
We sometimes making because the circle is too much big.
And let's suppose we want to make three jumps.
So if we put our extra jumps to three so we can make three jumps as you one two, three as you can see.
So I hope you get the idea that how you can make the jump, and it would be nice if we are able to notice
or adjust our jump height in underneath our player skip, and we don't need to go to that skip and change
the, uh, change the jump height, uh, or basically that thing.
So we're going to get out of play mode and we will open up our player skip.
And instead of setting directly or hard coding this like over here, as you can see, velocity dot y
we just set equal to ten f.
Instead of doing that, let's make a variable to uh, and we can just gonna make that public or say
less fill so we can adjust underneath our player skip.
So we don't need to come to the script and change this value over again and again to match up the correct
jump height.
So we will go to top of our class and let's make another serialized field, serialized field a private.
And we're going to call this one something.
This will be float.
And let's call this one jump height.
So let's call it jump height.
And by default I'm going to set equal to 12.
And let's close that up with semicolon.
And now we're going to replace this with our jump height.
So let's pass our jump height sorry.
Jump height and jump height by default set with 12.
So we will able to jump in the y axis with 12 unit.
And now we're going to press down Ctrl S to save our skip.
And now let's move over to unity.
So in the Y we will able to make jump with a jump height parameter.
And by default we set it to 12.
But we just made it a serialized field.
So we can notice underneath our player skip right over here.
So as you can notice right over here jump height which is by default we set 12.
So I'm going to make it a little bit bigger something like 15.
And I want to decrease the radius of this circle.
So I'm going to go with something like .1.15 something like that.
And now this seems perfect.
And also extra jumps.
I want to so I'm going to leave it extra jumps to two.
And let's go to overrides.
And then make sure you click on this apply all.
If you turn it your player game object into a prefab.
And if we just go ahead and then click on this play button in the top.
By now it would be fine.
It's just gonna be fine.
So whenever we're touching our ground or this player just touching our ground, then we're just setting
our total jumps to extra jumps.
As you can see.
So we're making too much.
So I'm going to make jump height to ten.
And we're making so as you can see.
So this seems pretty much cool.
So I hope you get the idea.
And let me explain one more time.
So I'm gonna get out of play mode.
So if I move over to my script.
So so this function over here, just creating that circle from that ground check point with this much
of radius and targeting our ground.
So this can be true or false.
And if the circle is really colliding with this ground, then this over here gonna be true.
And that's why this is ground, uh, just gonna be true.
And we created this variable to just keep track of if we're standing on this ground or not.
And if it's then what we're doing if this ground is true, basically means that circle is colliding
with that ground, then we're setting total jumps to our extra jumps and to extra jumps.
By default we set two.
So this total jumps gonna be two.
And so this an end condition basically this condition getting true once again because total jump is
uh is true once our s ground parameter is true.
Basically standing on that ground.
And we do know that two is greater than zero.
So that's why we're making jump.
And after jumping we just subtracting from their total jumps one unit, as you can see.
So it's going to be one and once it's one.
And if we press down space key on our keyboard and total jumps is greater than zero.
So this total jumps is one.
So one is greater than zero.
So the condition the condition is still true.
So that's why we jumping.
And once again we subtracting.
So one minus one is just going to be zero.
And now if we jump this total jumps is zero.
And zero is not greater than zero.
So that's why we are not able to jump for the third time or that we're making a jumping.
But as soon as we colliding with this ground, as you can see, we setting total jumps equal to our
extra jumps, which is two.
So this total jumps becomes one more time to.
So this end condition right over here becomes one more time to true to true.
So that's why we able to make our player jump.
So I hope you get the idea that how you can make jam and you can make jam as many times as you want,
and it would be nice if we just give it a title somewhere over here, uh, underneath our player, skip
over here a title.
And in order to give a title, we need to use header.
So we will open up our player skip.
And we will go to top of our class.
And over here, uh, so this right over here I'm going to put this line of code, this one uh, by hold
down alt on your keyboard.
And you can use up and down arrow in order to move your, uh, line of code.
And so this, uh, all of these things right over here is jumping.
Uh, so that's why I'm gonna give a title.
To give a title, we need to make this square brackets, and we need to type this header, and we need
to make this brackets.
And inside this bracket in quotation, you can just give the title that you want.
So so since this this all of this variable is for jumping, so I'm going to call it player jump section.
Or simply we can call it jump as well.
And now if we press down space sorry Ctrl s on our keyboard in order to save our skip and then move
over to unity underneath our player skip, we will notice a title, a title, something says player
jump as we have given.
So.
So you need to use a square brackets then header.
And inside that brackets you basically need to put quotation and then the title.
As you can see underneath our player skip we can say player jump title.
And underneath we can see all of the things variables for our jumping.
And we can it's just make it cool.
So that's why.
And we will go to overrides and then apply all if it's we change something around with this player game
object.
So it's gonna be save.
And now I'm gonna press Ctrl s on my keyboard in order to save my project.