WEBVTT

00:00.000 --> 00:00.600
Hello guys.

00:00.600 --> 00:01.680
Welcome to the video.

00:01.680 --> 00:05.460
In this one I want to show you how to set up jump for the character.

00:05.460 --> 00:06.930
And we're not going to just set up jump.

00:06.930 --> 00:09.570
We also going to set up collision detection.

00:09.570 --> 00:14.460
So player knows when he's on the ground and when he can jump and when he cannot jump.

00:14.460 --> 00:17.190
Okay, the jump functionality itself is very simple.

00:17.190 --> 00:20.100
It's going to be done with the help of input from a keyboard.

00:20.100 --> 00:25.680
And this is probably a good chance to discuss different, uh, type of inputs.

00:26.370 --> 00:30.030
Uh, let's type here if input get key.

00:30.600 --> 00:33.330
Key code space.

00:34.270 --> 00:35.530
Debug log.

00:36.270 --> 00:38.820
Um, holding space.

00:39.850 --> 00:43.780
Then here we're going to type if input get key down.

00:44.380 --> 00:45.580
Uh yeah.

00:45.580 --> 00:46.270
Down.

00:46.270 --> 00:50.200
Key code space we're going to type Debug.log.

00:50.350 --> 00:55.510
I pressed space and then if input.

00:56.210 --> 00:56.930
Get.

00:58.140 --> 00:59.040
If input.

00:59.040 --> 01:00.960
Get key up.

01:01.770 --> 01:02.760
Key code.

01:04.280 --> 01:05.180
Space.

01:06.070 --> 01:07.000
We're going to type.

01:07.000 --> 01:07.780
I'm sorry.

01:09.350 --> 01:10.490
Debug log.

01:11.380 --> 01:13.570
I released space.

01:13.570 --> 01:17.770
So these three ways of doing input have a simple difference.

01:17.770 --> 01:19.780
Get key is called value.

01:19.780 --> 01:24.820
Holding the key get key down is when you press the key.

01:24.820 --> 01:29.080
And then there is a get key up which is called when you release the button.

01:29.080 --> 01:30.790
Let's go ahead and try this.

01:30.790 --> 01:34.000
Just so you know different ways and how it can affect the input.

01:34.000 --> 01:35.500
But we're going to use only one of them.

01:37.080 --> 01:39.300
So in the console I'm going to.

01:40.640 --> 01:42.680
Press the key and I'm going to hold it.

01:42.680 --> 01:44.810
And we can see we have I'm holding space.

01:44.810 --> 01:50.030
I pressed space when I'm releasing the button, I released space.

01:50.030 --> 01:51.020
This is what it does.

01:51.020 --> 01:52.670
Simply simple as that.

01:53.060 --> 01:53.600
Yeah.

01:53.600 --> 02:01.100
Um, now that we know that, let's just use one of them and we're going to use get key down space and

02:01.100 --> 02:04.310
as a functionality in result, we're going to make a jump.

02:05.070 --> 02:06.810
Jump is very simple to do.

02:06.810 --> 02:10.770
First of all, we need to know the jump force how high character can jump.

02:12.230 --> 02:14.210
So I think it can be done here.

02:14.210 --> 02:17.960
Let's do serialized field, private float, jump force.

02:17.960 --> 02:20.750
And I think it would be a good idea to make a header.

02:20.750 --> 02:23.540
So it's a bit more recognizable in the inspector.

02:23.540 --> 02:25.430
Let's type here header.

02:25.670 --> 02:31.130
And in quotation marks we can type uh movement or something like that.

02:31.370 --> 02:34.550
Now if we save this and go to unity.

02:35.720 --> 02:36.470
We're going to see.

02:36.470 --> 02:37.430
We've got.

02:38.440 --> 02:40.420
A new variable and a header.

02:41.270 --> 02:42.260
Movement.

02:42.290 --> 02:42.740
Move!

02:42.740 --> 02:43.130
Speed!

02:43.130 --> 02:43.430
Jump!

02:43.430 --> 02:44.090
Force!

02:44.120 --> 02:45.110
Nice.

02:45.110 --> 02:47.180
Now let's go and apply this jump force.

02:47.180 --> 02:52.250
In the update we can type RB velocity equals to new vector two.

02:52.250 --> 02:54.110
And we don't want to change the x.

02:54.110 --> 02:56.360
So it's going to be rb velocity dot x.

02:56.930 --> 03:00.230
And for the y it's going to be jump force.

03:00.740 --> 03:01.820
Let's go back.

03:06.420 --> 03:09.930
And we're gonna give it a jump force of maybe six.

03:09.930 --> 03:12.630
For now, we just want to test and make sure it's working.

03:13.340 --> 03:16.490
And when I'm pressing space key, I'm jumping.

03:16.520 --> 03:17.570
Yay!

03:17.960 --> 03:19.100
Nice.

03:19.130 --> 03:21.140
Now there is an issue.

03:21.140 --> 03:24.050
If I jump a lot, I'm going to fly.

03:26.240 --> 03:27.860
This is not what we need, right?

03:27.860 --> 03:31.040
We want to jump only once when we are on the ground.

03:31.340 --> 03:35.570
For that, we need to be sure we know when player is grounded.

03:35.750 --> 03:36.800
How do we do that?

03:36.830 --> 03:42.380
Well, first of all we need to define what is a ground in the game world.

03:42.770 --> 03:47.000
For that we can go to layers over here and click Add Layer.

03:47.330 --> 03:49.700
We have an empty field over here.

03:49.700 --> 03:52.040
So I'm going to type here ground.

03:52.340 --> 03:53.450
Very nice.

03:53.510 --> 03:59.090
Now let's select the platform itself and change layer to ground.

03:59.120 --> 04:05.810
Now that we know that this object is a ground and now we just need to detect it and be sure that player

04:05.810 --> 04:08.840
is standing on the ground when we're trying to jump.

04:08.840 --> 04:10.130
How are we going to do this?

04:10.160 --> 04:13.610
Well there is such thing as a raycast.

04:13.910 --> 04:19.100
Raycast is basically a line that drawing from one point to another.

04:19.100 --> 04:25.760
And if this line is detecting anything, then we can say that, well, we detected a ground, for example,

04:25.760 --> 04:26.720
or something like that.

04:26.720 --> 04:33.590
So basically we just can use this raycast which is a simple line to detect if player is above ground.

04:33.920 --> 04:34.700
And.

04:36.870 --> 04:38.700
Let's go.

04:41.030 --> 04:43.490
Over here and make a header.

04:45.050 --> 04:45.800
Header.

04:46.560 --> 04:47.700
Collision.

04:48.930 --> 04:49.920
Uh, info.

04:50.160 --> 04:52.890
And then we're going to make private.

04:52.890 --> 04:56.190
Bool is rounded.

04:56.810 --> 04:57.770
Very good.

04:57.800 --> 05:02.630
Now we're gonna go to update, and in the update we need to type something.

05:02.630 --> 05:05.900
We need to type is grounded equals to.

05:05.900 --> 05:08.390
And here how you make a recast.

05:08.390 --> 05:10.850
It might be a bit confusing especially if you are new.

05:10.850 --> 05:11.840
Don't worry about it.

05:11.840 --> 05:14.630
Just type with me and learn how to use it.

05:14.630 --> 05:19.730
And if you don't understand it quite well right now, you will understand it later for sure.

05:19.730 --> 05:23.360
So let's type here physics 2D dot raycast.

05:23.360 --> 05:26.630
And this will say that we want to do a physics 2D raycast.

05:26.630 --> 05:32.210
And then in the parentheses you can see it says what do we need here to make it work.

05:32.360 --> 05:38.900
So we want to draw a raycast from original position of the character from the center.

05:38.900 --> 05:42.410
So I'm going to do origin transform position.

05:43.660 --> 05:51.070
Then we need direction, and we want to cast a ray from a center of the character down like that.

05:51.070 --> 05:52.330
This is what we want to do, right?

05:52.330 --> 05:54.490
We want to see what is below the character.

05:54.490 --> 06:00.280
If we want to cast it down, we can just simply type here vector two dot down.

06:00.280 --> 06:06.550
And if you see this for the first time, vector two dot down is equals to vector two zero minus one.

06:06.880 --> 06:07.930
Simple as that.

06:07.930 --> 06:11.680
Now if you put your mouse over here it says that that's good.

06:11.680 --> 06:13.180
That's probably everything that we need.

06:13.180 --> 06:17.710
But we can move further and apply more settings to this raycast.

06:17.710 --> 06:20.260
For example we can know the distance.

06:20.260 --> 06:22.120
How far should we do the check?

06:22.120 --> 06:27.730
For that we need to make a serialized field private float.

06:29.340 --> 06:30.930
Ground check.

06:30.960 --> 06:33.210
Distance over here.

06:33.210 --> 06:40.590
Please make sure that you put this line above the boolean, because header will not show itself if private

06:40.590 --> 06:42.540
value is after the header.

06:42.930 --> 06:47.190
Uh, something visible should be after the header for header to work.

06:47.220 --> 06:51.270
Okay, so just make sure you have ground check and then is grounded.

06:51.390 --> 06:56.640
So now that we have this value we can go here and type ground check distance.

06:57.540 --> 07:02.190
If we put a comma over here, we can see that we can use layer mask as well.

07:03.530 --> 07:04.130
Right.

07:04.130 --> 07:07.430
So for Layer Mask here we need to make one.

07:07.430 --> 07:11.690
So let's go back here again and make serialized field private.

07:11.690 --> 07:15.020
And this time we're going to use Layer Mask as a data type.

07:17.190 --> 07:18.300
Layer Mask.

07:19.110 --> 07:24.540
And then we're going to just name it as what is ground.

07:24.570 --> 07:25.860
Simple as that.

07:25.860 --> 07:29.640
And then over here we're going to type what is ground.

07:29.700 --> 07:32.790
So this is how you detect things around the objects.

07:32.790 --> 07:35.610
There are many ways to do detection around the character.

07:35.610 --> 07:37.470
But this one is the simplest for now.

07:37.470 --> 07:41.100
So let's just save it and it will work.

07:41.100 --> 07:42.720
But there is a problem.

07:42.720 --> 07:45.090
Raycast is working there.

07:45.090 --> 07:45.900
It is there.

07:45.900 --> 07:52.680
But we cannot see it because you know it's not visible to human eye, it's only visible to the Unity's

07:52.680 --> 07:52.890
eye.

07:52.920 --> 08:00.000
So if we want to see it, we just need to draw a line that has same parameters as the one that we just

08:00.000 --> 08:00.870
described.

08:01.260 --> 08:03.810
For that, we can use on draw gizmos.

08:04.850 --> 08:11.930
On drawer gizmos does not do a raycast check on drawer gizmos only draws a line itself.

08:11.930 --> 08:17.060
It only shows you something, but it is not the same as this one over here.

08:17.060 --> 08:24.230
It's just that we can draw it in a very similar way, so both of them will represent the same info.

08:24.230 --> 08:25.490
Anyway, let's just type here.

08:25.490 --> 08:33.980
Gizmos draw a line from Transform.position and as an endpoint we need another vector and we cannot just

08:33.980 --> 08:35.930
type it like it was before.

08:36.050 --> 08:38.150
Over here it would be a bit different.

08:38.150 --> 08:39.800
So we need to type here.

08:40.720 --> 08:46.090
Uh, new vector two for the X is going to be not changed at all.

08:46.090 --> 08:48.130
So we're going to type transform position dot.

08:49.020 --> 08:49.560
X.

08:49.560 --> 08:59.040
And for the y we're going to type transform position dot y minus ground check distance.

08:59.610 --> 09:00.570
Very well.

09:00.570 --> 09:01.980
Let's save this.

09:01.980 --> 09:03.900
And let's go back to the unity.

09:09.590 --> 09:16.310
So now if we increase this value over here, we can see a line is drawn and this whole raycast will

09:16.310 --> 09:17.420
work as well.

09:17.720 --> 09:22.310
So we just need to be sure that this line extends outside of a capsule collider.

09:22.310 --> 09:23.690
And that would be enough.

09:23.690 --> 09:28.610
So I think 0.8 is actually a good value here for me.

09:28.610 --> 09:32.120
You can try a different one if you need to, but 0.5 should be fine.

09:32.120 --> 09:35.630
And also we need to choose what is ground.

09:35.630 --> 09:37.730
And that would be ground layer.

09:37.760 --> 09:38.720
Very nice.

09:38.720 --> 09:40.760
Now if we go to play mode.

09:43.540 --> 09:46.060
And go to debug over here.

09:46.150 --> 09:46.720
Sorry.

09:46.720 --> 09:48.070
Let's go to debug.

09:49.200 --> 09:50.790
We can see that is grounded.

09:50.790 --> 09:51.420
True.

09:51.450 --> 09:54.270
If I jump, it will become false.

09:54.270 --> 09:56.130
True, false.

09:56.130 --> 09:57.900
Because we're not detecting ground anymore.

09:57.900 --> 09:59.520
Because we are in the air.

09:59.640 --> 10:00.840
How can we use it?

10:00.840 --> 10:02.550
I think you know how we can use it.

10:02.550 --> 10:04.140
It's a very simple thing.

10:06.840 --> 10:14.190
We go over here where we're trying to jump and we can type here and is grounded.

10:15.160 --> 10:21.520
So if we pressing the key down and if character is grounded, we perform the jump.

10:21.550 --> 10:25.300
Let's save this and let's go back to unity and try this out.

10:33.870 --> 10:34.980
Go to play mode.

10:37.000 --> 10:40.000
And now we jump in.

10:40.000 --> 10:42.760
But if we try to jump while we are in the air.

10:45.480 --> 10:46.560
Doesn't work.

10:46.680 --> 10:47.670
Isn't it cool?

10:47.730 --> 10:48.750
It's very cool, man.

10:48.750 --> 10:49.920
I love it so much.

10:50.520 --> 10:52.740
Now there are some things that could improve.

10:52.740 --> 10:53.910
Feeling of a jump.

10:54.180 --> 10:56.730
Um, that would be very easy to adjust.

10:56.730 --> 11:00.450
We can go to rigidbody and then we go to play mode.

11:01.270 --> 11:05.980
And over here you need to play with the gravity scale and jump force.

11:05.980 --> 11:08.770
Gravity scale makes character heavier.

11:08.770 --> 11:13.990
So if you type here four and try to jump again, look what is going to happen.

11:14.990 --> 11:16.730
He barely jumps, right?

11:16.730 --> 11:19.100
So now we want him to jump higher.

11:19.100 --> 11:25.130
And personally, me, I want to have a feeling where character feels quick and heavy, like he is just

11:25.130 --> 11:26.390
jumping really high.

11:26.390 --> 11:28.340
But it's not that easy to do.

11:28.340 --> 11:32.450
So on the jump force, I'll try to set value to 14.

11:32.450 --> 11:34.850
And actually I know the value because I tried before.

11:34.850 --> 11:38.780
So jump force 14 and Gravity Scale 4.5.

11:38.780 --> 11:41.540
And if we try to jump again, this is how it feels.

11:41.540 --> 11:45.500
And I'm really satisfied with the this feeling for you.

11:45.500 --> 11:51.650
I would suggest to use these values for now and wait for the later setup of the character, because

11:51.650 --> 11:57.080
we still need to set up jump animations, and also we need to make him flip when he goes to the left.

11:57.080 --> 12:03.200
And once we do that, it's going to be a bit easier to adjust and see what the jump force and gravity

12:03.200 --> 12:05.600
scale you should have and how it should work better.

12:05.600 --> 12:08.270
So as of now, let's just leave it as it is.

12:08.270 --> 12:10.160
Maybe let's just keep one and six.

12:10.160 --> 12:10.700
It's okay.

12:10.700 --> 12:14.180
Once we have entire setup, it will be a bit easier to balance.

12:14.180 --> 12:18.770
I think, because you're going to not only see how it feels, but but also you're going to see how it

12:18.770 --> 12:19.160
looks.

12:19.160 --> 12:21.200
And that is very important as well, right?

12:22.190 --> 12:22.670
Yeah.

12:22.670 --> 12:25.790
So probably let's just keep one on the gravity scale.

12:25.790 --> 12:27.950
Jump four six should be fine.

12:27.950 --> 12:32.480
Now, uh, before we end this video, I want to do some changes here.

12:32.480 --> 12:37.430
This feels a bit messy and it can get out of hand very quickly.

12:37.430 --> 12:38.960
So I want to clean this up.

12:38.960 --> 12:39.260
How?

12:39.260 --> 12:40.280
I want to clean this up.

12:40.280 --> 12:45.140
Well, maybe, just maybe, let's make a method.

12:45.140 --> 12:47.870
And this time I want to show you another way of making a method.

12:47.870 --> 12:52.430
You can type here handle collision as it would be a method.

12:52.430 --> 12:55.940
And now we can see it is underlined because there is no such method.

12:55.940 --> 13:02.450
But if you press alt Enter or Ctrl plus dot you will call uh Quick Actions menu.

13:02.450 --> 13:04.880
And here you can choose Generate method.

13:05.120 --> 13:06.050
Very nice.

13:06.050 --> 13:08.900
Now we need to know what will be inside of this method.

13:08.900 --> 13:12.050
And inside of this method I want to place this collision.

13:12.050 --> 13:14.600
So I'm just going to cut it out.

13:15.760 --> 13:17.920
And paste it in here.

13:18.700 --> 13:19.600
Very good.

13:19.600 --> 13:22.690
Now it is a bit cleaner and I like it more.

13:22.780 --> 13:24.400
There is something else I want to do.

13:24.400 --> 13:28.870
I think it would be a good idea to take all of this.

13:28.870 --> 13:37.330
Click alt, enter, extract method, handle input and here we're going to take care of the input.

13:37.840 --> 13:39.940
So now we have everything clean and nice.

13:39.940 --> 13:44.530
The only suggestion I would do here is to take handle collision and move it above.

13:44.530 --> 13:49.630
Because we want to have collision information updated first, then we want to have the input.

13:49.630 --> 13:53.470
Then we want to update the animations and then we want to apply movement.

13:53.620 --> 13:58.870
Actually, I think animations can be after movement because we change velocity and then we apply animation

13:58.870 --> 13:59.440
to that.

13:59.590 --> 14:01.270
So yeah this is how it should be.

14:01.270 --> 14:07.000
I think this is the execution order in the update collision input movement animations.

14:07.750 --> 14:14.740
Um, for better understanding of the code, we could also take this line of code and just wrap it into

14:14.740 --> 14:17.410
a method that would say just jump.

14:18.190 --> 14:20.380
So let's do extract method.

14:21.290 --> 14:21.920
Jump.

14:22.810 --> 14:25.300
It's a bit better and easier to read.

14:25.330 --> 14:28.300
Um, let me see if we have a couple of minutes now.

14:28.300 --> 14:30.250
I think that's good for now.

14:30.250 --> 14:31.390
That's good for this video.

14:31.390 --> 14:32.680
I'll see you in the next one.

14:32.680 --> 14:33.010
Okay.
