WEBVTT

00:00.020 --> 00:00.560
Hello guys.

00:00.560 --> 00:01.550
Welcome to the video.

00:01.550 --> 00:04.190
In this one I want to add a bit of a functionality.

00:04.190 --> 00:06.590
And what I want to do is something simple.

00:06.590 --> 00:09.560
It is a double jump ability for the player.

00:09.560 --> 00:14.210
First of all, let's balance our gravity scale and jump force just a bit.

00:14.210 --> 00:19.460
Actually, I know what values I want to have and you can just take my values and continue with development.

00:19.460 --> 00:23.390
Or you can try your values and it's going to be just okay as well.

00:23.390 --> 00:28.490
It's all about, you know, balancing the values and finding the right feeling that you want to have.

00:28.490 --> 00:33.800
It's quite difficult to find the sweet spot for it, but I'm pretty satisfied with what I have here.

00:33.800 --> 00:36.590
So I'm going to use Gravity Scale 4.5.

00:36.800 --> 00:40.820
And over here jump four is going to be 14.

00:40.820 --> 00:42.380
Now let me try to jump.

00:42.380 --> 00:43.790
And this is how it feels.

00:43.790 --> 00:45.290
And I think I like it.

00:46.330 --> 00:47.080
Okay.

00:47.080 --> 00:47.710
Very good.

00:47.740 --> 00:50.200
There is also one parameter I want to tell you about.

00:50.200 --> 00:57.610
It is a linear drag, and this one is basically responsible for the air resistance.

00:57.610 --> 01:02.860
If you increase this value, it's going to feel like character is resisting something.

01:02.860 --> 01:04.720
Let's just try to put one over here.

01:04.720 --> 01:09.400
You can see movement feels a bit different now and walking as well.

01:09.400 --> 01:12.340
So here it is up to you if you want to use it or not.

01:12.340 --> 01:13.990
By the way, I like how it feels.

01:13.990 --> 01:15.430
Yeah, I think I like it.

01:15.430 --> 01:16.600
I'm going to keep it.

01:17.020 --> 01:17.860
Why not?

01:20.660 --> 01:21.920
So he can jump.

01:21.920 --> 01:24.200
And I wish we would have a double jump right now.

01:24.200 --> 01:25.640
So that's what we're going to do.

01:25.640 --> 01:34.250
First, let's go out of a play mode, set gravity scale to 4.5, linear drag to one, and jump force

01:34.250 --> 01:35.570
to 14.

01:37.440 --> 01:38.100
Nice.

01:38.100 --> 01:40.500
Now let's go ahead and add double jump.

01:40.530 --> 01:42.510
It's going to be not that difficult to do.

01:43.280 --> 01:43.610
The first.

01:43.610 --> 01:45.590
I want to show you how we're going to make the functionality.

01:45.590 --> 01:46.820
Then we're going to clean it up.

01:46.820 --> 01:50.330
Let's just make a public bool.

01:51.290 --> 01:53.690
Can double jump.

01:53.720 --> 01:55.700
For now, that's all what we're going to have.

01:55.700 --> 02:01.070
And then we're going to go all the way here where we have jump method and handle input.

02:01.070 --> 02:03.410
And I want to change how it's written just a bit.

02:03.410 --> 02:09.230
So as of now when we press on the key, we also check if player is grounded and if he is grounded and

02:09.230 --> 02:10.850
we press the key, we jump.

02:10.850 --> 02:17.240
However, later on we're going to have a double jump and also a wall jump, and I don't want to do conditions

02:17.240 --> 02:19.520
for all of that in one if statement.

02:19.520 --> 02:22.100
That would be a very cluttered if statement.

02:22.100 --> 02:27.380
So instead, I want to make a method that would be something like a jump button, which you're going

02:27.380 --> 02:29.780
to be called every time you press the space key.

02:29.780 --> 02:34.190
And then any checks we want to do for the jump, we're going to do inside of this jump button.

02:34.190 --> 02:35.360
So let's make.

02:36.260 --> 02:39.590
Private void jump button here.

02:39.920 --> 02:46.190
And inside of this method we're going to check if is grounded then jump.

02:46.860 --> 02:54.510
So now, because of that, we can remove this check over here and instead of jump we can call jump button.

02:55.220 --> 02:58.010
So basically it's just like space key is a jump button.

02:58.010 --> 03:02.000
Just call the method and whatever checks we need to do, do it inside of the method.

03:02.030 --> 03:08.180
Now we're going to type here elseif which basically adds another if check if condition for the first

03:08.180 --> 03:11.270
one was not met, then we're going to move to another if check.

03:11.270 --> 03:18.230
And over here we want to check if can double jump true then we're going to do jump.

03:19.270 --> 03:22.240
And we're going to make can double jump equals to false.

03:22.270 --> 03:24.430
And maybe let's open curly brackets over here.

03:24.430 --> 03:25.570
So it's pretty.

03:26.200 --> 03:26.740
Okay.

03:27.100 --> 03:32.170
So this is what we have now when we press in the button we check first if we ground it and we can jump.

03:32.170 --> 03:36.610
If we're not grounded we're going to be in the air and we're going to check the boolean itself.

03:36.970 --> 03:40.720
Let's go ahead back to unity and try this out.

03:41.890 --> 03:43.510
We're going to go to play mode.

03:45.070 --> 03:47.710
And we can see that over here.

03:47.710 --> 03:48.880
Boolean is false.

03:48.880 --> 03:49.810
So I'm going to click it.

03:49.810 --> 03:50.860
So it's true.

03:50.860 --> 03:53.320
And I'm going to jump once and again.

03:53.320 --> 03:55.360
And now I cannot do a double jump.

03:55.360 --> 03:57.070
And we need to fix it right.

03:57.070 --> 03:58.270
That's what we need to do.

03:58.270 --> 03:59.740
How are we going to fix it.

03:59.920 --> 04:05.950
Uh somehow we need to know when player touched the ground so we can reset, can double jump value and

04:05.950 --> 04:06.850
make it true again.

04:06.850 --> 04:11.440
So double jump is available for the next, you know, player's action.

04:11.530 --> 04:13.570
And we're going to go to player script.

04:13.570 --> 04:17.770
And we're going to make some sort of check if player is in the air.

04:17.770 --> 04:22.900
Once he's in the air and he touches the ground, we're going to say, hey, we just landed and we're

04:22.900 --> 04:26.020
going to do some functionality like resetting, double jump and so on.

04:26.020 --> 04:31.540
Let's go above over here and let's add private boolean.

04:33.170 --> 04:36.470
Uays airborne, which means in the air.

04:36.620 --> 04:40.220
And then in the update we're going to do a simple check.

04:40.220 --> 04:47.960
If not is grounded, which says we are not on the ground and Boolean is airborne is not true, then

04:47.960 --> 04:48.980
we're going to make.

04:49.750 --> 04:51.760
Is airborne equals to true.

04:51.760 --> 04:54.370
And this will make character an airborne.

04:54.370 --> 05:00.820
And just before that we want to check if is airborne and.

05:01.910 --> 05:06.200
Is grounded, then is airborne equals to false.

05:06.200 --> 05:10.910
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.

05:10.910 --> 05:14.630
Okay, let's save this and let's go to unity and see it.

05:20.060 --> 05:26.120
I'm going to go to debug mode, and in the play mode we can see his airborne is false.

05:26.120 --> 05:31.010
When I jump becomes true and when I'm on the ground it becomes false again.

05:31.010 --> 05:35.390
Basically we just switching them between is grounded and is airborne.

05:35.600 --> 05:39.710
Of course we could use only one boolean, but we need to do some sort of reset.

05:39.710 --> 05:42.170
And that's why we use the second boolean.

05:42.260 --> 05:44.990
Let's go back to normal mode and play script.

05:44.990 --> 05:50.150
And I'll just try to explain it real quick in case it's confusing, because I think it might be.

05:50.150 --> 05:57.020
And actually maybe it will be a bit easier to understand if we do it like so is grounded and is airborne.

05:57.890 --> 06:02.150
So we update the second boolean according to the first boolean we have.

06:02.150 --> 06:05.090
If we are grounded, then airborne should be false.

06:05.090 --> 06:08.090
If we are not grounded, then airborne should be true.

06:08.090 --> 06:13.340
And the second boolean in this if statement is just there to make it work only once, because we need

06:13.340 --> 06:14.840
to do reset only once.

06:14.840 --> 06:18.170
Simple as that is grounded, then airborne should be false.

06:18.170 --> 06:21.860
If it's a true, then make it false if not grounded.

06:22.830 --> 06:24.630
Then airborne should be true.

06:24.630 --> 06:26.580
If it's not true, then make it true.

06:26.610 --> 06:27.540
This is the logic.

06:27.540 --> 06:30.390
Now I want to take these four lines of code.

06:30.390 --> 06:38.040
Click right button Quick Actions and choose Extract Method and I'm going to name it as update.

06:39.610 --> 06:42.010
Airborne status.

06:43.350 --> 06:45.210
Yeah, this is very useful for us.

06:45.210 --> 06:48.930
We just want to, uh, add a bit more meaning to this.

06:49.440 --> 06:51.720
We're going to do that by making another method.

06:51.720 --> 06:59.670
Let's take this line of code, press alt enter extract method and call it handle Lending.

07:00.470 --> 07:04.580
Now, whenever you need to do something, whenever you need to do something, when character lands on

07:04.580 --> 07:05.720
the ground, you can do it.

07:05.720 --> 07:06.800
Inside of this method.

07:06.800 --> 07:10.550
For example, you can say can double jump equals to true.

07:12.360 --> 07:15.780
Uh, let's go ahead and try this again.

07:22.320 --> 07:23.310
I'm going to jump.

07:23.310 --> 07:24.900
Double jump and I'm landing.

07:24.900 --> 07:26.880
Jump, double jump and I'm landing.

07:29.480 --> 07:30.170
Call rate.

07:31.680 --> 07:35.820
Very simple and very clean logic, I believe.

07:36.060 --> 07:39.030
Although it might be a bit confusing if you see it for the first time.

07:39.030 --> 07:44.580
Also, for better understanding, let's change this method from is airborne true to.

07:46.210 --> 07:47.890
Let's do extract method.

07:48.920 --> 07:51.170
Become airborne.

07:54.500 --> 08:00.170
So whenever you need to do something, when you go off the ground, you can do it over here whenever

08:00.170 --> 08:01.010
you need to do something.

08:01.010 --> 08:03.980
When you're lending, you can do it in this method.

08:03.980 --> 08:04.730
Very nice.

08:04.730 --> 08:06.830
Now let's improve the double jump.

08:07.910 --> 08:08.990
There's going to be closed for now.

08:08.990 --> 08:13.040
We're not going to touch it for a while on the jump button over here.

08:13.040 --> 08:15.590
We're just doing a simple jump just as before.

08:15.590 --> 08:21.050
But actually I want to have a double jump method that will use different jump force.

08:21.050 --> 08:24.170
This will help us to achieve better feeling for the game.

08:24.470 --> 08:29.870
And that's why I want to go over here and make Private Void double jump.

08:30.770 --> 08:37.010
In which we're going to take our velocity equals to a new vector two rb velocity dot x.

08:37.010 --> 08:41.060
And for the jump force we actually need to make a variable that I forgot to do.

08:41.540 --> 08:44.120
Uh, let's go above over here.

08:46.200 --> 08:46.920
Over here.

08:46.920 --> 08:48.690
We can do header.

08:49.520 --> 08:53.180
Double jump and then sterilize field.

08:53.720 --> 08:54.770
Private float.

08:54.770 --> 08:57.680
Double jump horse.

08:58.100 --> 08:58.970
Nice.

08:58.970 --> 09:01.850
Let's save this and let's go back.

09:04.980 --> 09:07.440
And use double jump force for the Y.

09:09.380 --> 09:15.710
And also when we're doing this jump we want to set can double jump to false now because we need it here.

09:15.710 --> 09:17.930
We don't need to type this here anymore.

09:17.930 --> 09:21.500
We can just replace it with a double jump.

09:24.450 --> 09:25.500
Just like that.

09:25.950 --> 09:27.120
Very cool rate.

09:27.150 --> 09:28.410
Yeah, I think so.

09:30.150 --> 09:31.680
Well, I hope you do think so.

09:32.220 --> 09:32.760
Um.

09:34.220 --> 09:36.830
Let's go ahead and see this in the action.

09:36.830 --> 09:40.370
And actually I think I want to remove the linear drag here.

09:40.370 --> 09:41.030
I'm sorry.

09:41.030 --> 09:42.440
Let's make it zero.

09:43.980 --> 09:49.290
Now on the player script, we have jump force and double jump force, and I believe the second jump

09:49.290 --> 09:51.300
should not be as powerful as the first one.

09:51.300 --> 09:53.910
So I'm going to make it maybe 12 or 10.

09:53.910 --> 09:58.710
It should be sort of an extra hop, but not the double jump that you can abuse.

09:58.710 --> 10:02.670
It's just going to be there to save yourself in a tricky situation.

10:02.670 --> 10:04.140
Okay, let's go to play mode.

10:05.290 --> 10:07.540
And I'm going to make another platform.

10:08.910 --> 10:10.110
Can I jump here?

10:10.110 --> 10:13.320
Yes, I can now let me bring this up.

10:13.320 --> 10:14.280
Like so.

10:14.340 --> 10:17.820
Can I jump here now I cannot, but what if I do double jump?

10:19.090 --> 10:20.080
And I'll jump there.

10:21.410 --> 10:22.280
Yay!

10:22.970 --> 10:23.600
Nice.

10:23.600 --> 10:25.400
So I sort of like it.

10:25.400 --> 10:27.530
And of course we can rebalance it later on.

10:27.530 --> 10:29.240
But as of now, I think this is good.

10:29.270 --> 10:32.690
I would only increase the move speed for the player.

10:32.690 --> 10:37.580
Let's make it eight and maybe let's do jump force for double jump 11.

10:39.420 --> 10:40.740
Let me try again.

10:42.030 --> 10:44.370
Yeah, this feels nice, I like it.

10:49.870 --> 10:52.540
Uh, don't worry about player being sticky with the walls.

10:52.540 --> 10:53.890
We're going to fix it later on.

10:55.720 --> 10:56.350
Okay.

10:56.350 --> 10:57.430
Very good.

10:57.580 --> 11:02.440
Now let's go to the player script and do something here.

11:03.950 --> 11:08.240
First of all, I think we can hide this boolean with the need it anymore.

11:08.240 --> 11:15.260
We know it works and I think we can do something like, like double jump details over here and.

11:15.930 --> 11:18.210
Movement details over here.

11:18.210 --> 11:19.260
And.

11:19.830 --> 11:22.110
Should we actually just make one header for this?

11:22.110 --> 11:25.350
Because there's just only one variable I think.

11:25.350 --> 11:25.710
Yeah.

11:25.710 --> 11:29.490
Let's just do it like so let's remove this header.

11:31.380 --> 11:34.500
And yeah, that's probably it for now.

11:34.500 --> 11:35.610
Let me save it.

11:35.610 --> 11:38.460
Let me check these things over here.

11:40.990 --> 11:46.450
We could name it as handle airborne status, but I want to keep the update word because we updating

11:46.450 --> 11:46.840
the value.

11:46.840 --> 11:49.510
So that's my crazy logic.

11:49.540 --> 11:51.190
All right this is good.

11:51.190 --> 11:53.290
Thank you so much for paying attention.

11:53.290 --> 11:55.030
And I'll see you in the next video.
