Hello guys.
Welcome to the video.
In this one I want to add a bit of a functionality.
And what I want to do is something simple.
It is a double jump ability for the player.
First of all, let's balance our gravity scale and jump force just a bit.
Actually, I know what values I want to have and you can just take my values and continue with development.
Or you can try your values and it's going to be just okay as well.
It's all about, you know, balancing the values and finding the right feeling that you want to have.
It's quite difficult to find the sweet spot for it, but I'm pretty satisfied with what I have here.
So I'm going to use Gravity Scale 4.5.
And over here jump four is going to be 14.
Now let me try to jump.
And this is how it feels.
And I think I like it.
Okay.
Very good.
There is also one parameter I want to tell you about.
It is a linear drag, and this one is basically responsible for the air resistance.
If you increase this value, it's going to feel like character is resisting something.
Let's just try to put one over here.
You can see movement feels a bit different now and walking as well.
So here it is up to you if you want to use it or not.
By the way, I like how it feels.
Yeah, I think I like it.
I'm going to keep it.
Why not?
So he can jump.
And I wish we would have a double jump right now.
So that's what we're going to do.
First, let's go out of a play mode, set gravity scale to 4.5, linear drag to one, and jump force
to 14.
Nice.
Now let's go ahead and add double jump.
It's going to be not that difficult to do.
The first.
I want to show you how we're going to make the functionality.
Then we're going to clean it up.
Let's just make a public bool.
Can double jump.
For now, that's all what we're going to have.
And then we're going to go all the way here where we have jump method and handle input.
And I want to change how it's written just a bit.
So as of now when we press on the key, we also check if player is grounded and if he is grounded and
we press the key, we jump.
However, later on we're going to have a double jump and also a wall jump, and I don't want to do conditions
for all of that in one if statement.
That would be a very cluttered if statement.
So instead, I want to make a method that would be something like a jump button, which you're going
to be called every time you press the space key.
And then any checks we want to do for the jump, we're going to do inside of this jump button.
So let's make.
Private void jump button here.
And inside of this method we're going to check if is grounded then jump.
So now, because of that, we can remove this check over here and instead of jump we can call jump button.
So basically it's just like space key is a jump button.
Just call the method and whatever checks we need to do, do it inside of the method.
Now we're going to type here elseif which basically adds another if check if condition for the first
one was not met, then we're going to move to another if check.
And over here we want to check if can double jump true then we're going to do jump.
And we're going to make can double jump equals to false.
And maybe let's open curly brackets over here.
So it's pretty.
Okay.
So this is what we have now when we press in the button we check first if we ground it and we can jump.
If we're not grounded we're going to be in the air and we're going to check the boolean itself.
Let's go ahead back to unity and try this out.
We're going to go to play mode.
And we can see that over here.
Boolean is false.
So I'm going to click it.
So it's true.
And I'm going to jump once and again.
And now I cannot do a double jump.
And we need to fix it right.
That's what we need to do.
How are we going to fix it.
Uh somehow we need to know when player touched the ground so we can reset, can double jump value and
make it true again.
So double jump is available for the next, you know, player's action.
And we're going to go to player script.
And we're going to make some sort of check if player is in the air.
Once he's in the air and he touches the ground, we're going to say, hey, we just landed and we're
going to do some functionality like resetting, double jump and so on.
Let's go above over here and let's add private boolean.
Uays airborne, which means in the air.
And then in the update we're going to do a simple check.
If not is grounded, which says we are not on the ground and Boolean is airborne is not true, then
we're going to make.
Is airborne equals to true.
And this will make character an airborne.
And just before that we want to check if is airborne and.
Is grounded, then is airborne equals to false.
I know it's kind of confusing, but let's just do it and I'll try to explain it as much as I can.
Okay, let's save this and let's go to unity and see it.
I'm going to go to debug mode, and in the play mode we can see his airborne is false.
When I jump becomes true and when I'm on the ground it becomes false again.
Basically we just switching them between is grounded and is airborne.
Of course we could use only one boolean, but we need to do some sort of reset.
And that's why we use the second boolean.
Let's go back to normal mode and play script.
And I'll just try to explain it real quick in case it's confusing, because I think it might be.
And actually maybe it will be a bit easier to understand if we do it like so is grounded and is airborne.
So we update the second boolean according to the first boolean we have.
If we are grounded, then airborne should be false.
If we are not grounded, then airborne should be true.
And the second boolean in this if statement is just there to make it work only once, because we need
to do reset only once.
Simple as that is grounded, then airborne should be false.
If it's a true, then make it false if not grounded.
Then airborne should be true.
If it's not true, then make it true.
This is the logic.
Now I want to take these four lines of code.
Click right button Quick Actions and choose Extract Method and I'm going to name it as update.
Airborne status.
Yeah, this is very useful for us.
We just want to, uh, add a bit more meaning to this.
We're going to do that by making another method.
Let's take this line of code, press alt enter extract method and call it handle Lending.
Now, whenever you need to do something, whenever you need to do something, when character lands on
the ground, you can do it.
Inside of this method.
For example, you can say can double jump equals to true.
Uh, let's go ahead and try this again.
I'm going to jump.
Double jump and I'm landing.
Jump, double jump and I'm landing.
Call rate.
Very simple and very clean logic, I believe.
Although it might be a bit confusing if you see it for the first time.
Also, for better understanding, let's change this method from is airborne true to.
Let's do extract method.
Become airborne.
So whenever you need to do something, when you go off the ground, you can do it over here whenever
you need to do something.
When you're lending, you can do it in this method.
Very nice.
Now let's improve the double jump.
There's going to be closed for now.
We're not going to touch it for a while on the jump button over here.
We're just doing a simple jump just as before.
But actually I want to have a double jump method that will use different jump force.
This will help us to achieve better feeling for the game.
And that's why I want to go over here and make Private Void double jump.
In which we're going to take our velocity equals to a new vector two rb velocity dot x.
And for the jump force we actually need to make a variable that I forgot to do.
Uh, let's go above over here.
Over here.
We can do header.
Double jump and then sterilize field.
Private float.
Double jump horse.
Nice.
Let's save this and let's go back.
And use double jump force for the Y.
And also when we're doing this jump we want to set can double jump to false now because we need it here.
We don't need to type this here anymore.
We can just replace it with a double jump.
Just like that.
Very cool rate.
Yeah, I think so.
Well, I hope you do think so.
Um.
Let's go ahead and see this in the action.
And actually I think I want to remove the linear drag here.
I'm sorry.
Let's make it zero.
Now on the player script, we have jump force and double jump force, and I believe the second jump
should not be as powerful as the first one.
So I'm going to make it maybe 12 or 10.
It should be sort of an extra hop, but not the double jump that you can abuse.
It's just going to be there to save yourself in a tricky situation.
Okay, let's go to play mode.
And I'm going to make another platform.
Can I jump here?
Yes, I can now let me bring this up.
Like so.
Can I jump here now I cannot, but what if I do double jump?
And I'll jump there.
Yay!
Nice.
So I sort of like it.
And of course we can rebalance it later on.
But as of now, I think this is good.
I would only increase the move speed for the player.
Let's make it eight and maybe let's do jump force for double jump 11.
Let me try again.
Yeah, this feels nice, I like it.
Uh, don't worry about player being sticky with the walls.
We're going to fix it later on.
Okay.
Very good.
Now let's go to the player script and do something here.
First of all, I think we can hide this boolean with the need it anymore.
We know it works and I think we can do something like, like double jump details over here and.
Movement details over here.
And.
Should we actually just make one header for this?
Because there's just only one variable I think.
Yeah.
Let's just do it like so let's remove this header.
And yeah, that's probably it for now.
Let me save it.
Let me check these things over here.
We could name it as handle airborne status, but I want to keep the update word because we updating
the value.
So that's my crazy logic.
All right this is good.
Thank you so much for paying attention.
And I'll see you in the next video.