WEBVTT

00:00.200 --> 00:00.740
Hello guys.

00:00.740 --> 00:01.730
Welcome to the video.

00:01.730 --> 00:04.460
And finally we have to set up movement of the player.

00:04.460 --> 00:05.990
I'm very happy about it.

00:05.990 --> 00:08.330
Let's do it right now.

00:08.330 --> 00:12.830
It's going to be not that difficult because actually we did something similar in the first section of

00:12.830 --> 00:13.670
this course.

00:13.670 --> 00:15.380
Let's go to player now.

00:17.810 --> 00:24.950
And here we need to save input that we have from a keyboard and then apply it to the movement together

00:24.950 --> 00:26.000
with the move speed.

00:26.030 --> 00:28.160
You can edit input before.

00:28.160 --> 00:32.000
If you remember we typed input dot get axis horizontal.

00:32.000 --> 00:38.510
But now I want to store this input in a variable so I can use it across different places in the script.

00:38.690 --> 00:44.690
Let's make uh, let's make public float first because I want to see the while I'm testing it.

00:44.690 --> 00:46.070
And then we're going to make it private.

00:46.070 --> 00:48.920
So it's going to be private float X input.

00:48.920 --> 00:50.660
And then in the update.

00:51.480 --> 00:54.510
We can do x input equals to.

00:55.280 --> 00:56.090
Input.

00:57.250 --> 00:58.720
Get access.

00:59.870 --> 01:01.340
Horizontal.

01:03.810 --> 01:05.190
Now let's save this.

01:07.110 --> 01:08.550
Go back to unity.

01:10.220 --> 01:18.440
We have x input which is zero, and if we press x input like add or arrow keys, we can see that value

01:18.440 --> 01:22.790
is changing from 0 to 1 or to minus one.

01:22.790 --> 01:26.810
And we can see it slowly grows or slowly decreases to the needed value.

01:26.810 --> 01:30.290
So it's like does not become 1 or -1 immediately.

01:30.290 --> 01:36.890
This will uh, result a movement where player is speeding up slowly, which I don't really want to have.

01:36.890 --> 01:40.190
I want to have quick, snappy, responsive movement.

01:40.340 --> 01:44.810
And because of that we're going to use get axis row over here.

01:44.810 --> 01:46.130
We're going to type row.

01:46.130 --> 01:55.130
And now if you save this and try this again you will see that x input is changing to the -1 or 1 immediately.

01:55.130 --> 01:58.490
Without that gradual change you can see one minus one.

01:58.490 --> 02:04.130
This is very good for us because I want to have snappy, responsive controls on this platformer.

02:04.130 --> 02:06.260
Now let's go back to player.

02:07.110 --> 02:10.590
And use this input to move the character.

02:10.590 --> 02:14.430
I'll just have to make it private because I don't need to see it anymore.

02:14.430 --> 02:15.690
We know how it works.

02:16.050 --> 02:24.870
And in the update we're going to use RB dot velocity equals to new vector two x input.

02:24.870 --> 02:28.890
And for the y we're going to use rb velocity dot y itself.

02:28.890 --> 02:30.390
Let's just try it first.

02:30.390 --> 02:32.400
And then I'm going to explain what we did here.

02:32.400 --> 02:33.660
Let's go to play mode.

02:40.000 --> 02:46.390
And I can move to the left or to the right very slowly, but I'm moving, which is very cool.

02:46.420 --> 02:48.820
Now what actually just happened?

02:49.590 --> 02:53.730
Um, RB is a rigid body component, which we discussed before.

02:53.760 --> 03:01.350
So by getting a component over here, we basically say that this RB is this rigid body over here.

03:01.350 --> 03:07.260
And then when we type in RB in the update, we basically say, hey you rigid body, listen to me.

03:07.260 --> 03:12.210
And then we set dot velocity and we control velocity of that rigid body.

03:12.240 --> 03:15.420
Now why do we set it to vector two.

03:15.450 --> 03:18.780
That is a very simple question with a very simple answer.

03:18.810 --> 03:22.290
Everything in 2D world works with a vector two.

03:22.320 --> 03:26.220
I actually have a good video that explains how vector two is working.

03:26.220 --> 03:30.210
So maybe I will attach link to this lecture and you can just check it.

03:30.210 --> 03:32.040
It's a really nice one I think.

03:32.340 --> 03:38.400
But shortly I'll explain that you can see only two vectors y and x.

03:39.240 --> 03:40.710
And the rule is simple.

03:40.920 --> 03:43.140
You want to move, object to the right.

03:43.140 --> 03:46.050
Change his position on the X, make it bigger.

03:46.050 --> 03:47.340
It will move to the right.

03:47.340 --> 03:48.720
Make it lower.

03:48.720 --> 03:49.740
It will move to the left.

03:49.740 --> 03:52.530
Because this is how vector two is working, right?

03:52.530 --> 03:55.800
We have x y and it goes that side.

03:55.800 --> 04:01.770
So 123 minus one minus two minus three.

04:01.770 --> 04:06.180
Same for the Y123 minus one minus two minus three.

04:06.210 --> 04:07.200
You want character.

04:07.200 --> 04:08.160
Move to the right.

04:08.160 --> 04:13.620
Change his velocity or position and make it x something like a positive value.

04:13.620 --> 04:14.880
You want to move it to the left.

04:14.880 --> 04:18.630
Make it minus x, something like minus four, minus three and so on.

04:18.630 --> 04:24.090
And velocity of the rigid body always wants to know in which direction it's supposed to move.

04:24.210 --> 04:30.840
And by saying that we change only x in the vector two, it knows that we applying.

04:31.840 --> 04:34.330
Vector one over here.

04:34.330 --> 04:37.660
And that's why it moves to the right or minus one over here.

04:37.660 --> 04:39.220
And that's why it moves to the left.

04:39.670 --> 04:45.430
Regarding the Y over here, even though we don't want to change velocity on the Y, we only want to

04:45.430 --> 04:47.050
have horizontal movement.

04:47.080 --> 04:53.920
We cannot type zero here because if you type zero here, it means you're trying to set velocity of the

04:53.920 --> 04:56.500
y to the zero, which we don't really need.

04:56.500 --> 04:59.140
We only want to change one velocity on the x.

04:59.140 --> 05:05.860
Because of that, we type in our velocity dot y because in this way it will take an x input as the x

05:05.860 --> 05:06.610
velocity.

05:06.610 --> 05:11.350
But y velocity will be unchanged because it will take its own velocity.

05:11.380 --> 05:17.290
You can say we python just hey, take the same velocity, don't change anything for the y, but for

05:17.290 --> 05:20.200
the x input use this value over here.

05:20.940 --> 05:22.260
Simple as that.

05:22.410 --> 05:23.160
Yeah.

05:24.540 --> 05:26.910
Well, that's actually was faster than I expected.

05:26.940 --> 05:30.390
Now we want to control speed of the character.

05:30.390 --> 05:35.850
And because of that, we can just take this x input and multiply it by move speed.

05:35.850 --> 05:36.840
We have over there.

05:36.840 --> 05:39.660
Let's do multiply move speed.

05:42.440 --> 05:45.230
Now we can go back to unity and try this out.

05:48.820 --> 05:51.340
Let's just go to play mode.

05:51.340 --> 05:58.120
And now, if you'll try to move, nothing will happen because your move speed is zero and we are multiplying

05:58.120 --> 06:01.900
x input by zero, which results a zero.

06:01.930 --> 06:06.820
Let's go back and change move speed to something like four and try to move now.

06:06.820 --> 06:08.710
And he moves faster.

06:08.710 --> 06:09.640
He's responsible.

06:09.640 --> 06:12.610
He's moving which I really, really like.

06:12.610 --> 06:13.630
Very cool.

06:14.730 --> 06:15.390
Yay!

06:15.480 --> 06:16.710
We can make him faster.

06:16.710 --> 06:19.710
Maybe eight, and this will work as well.

06:20.760 --> 06:21.480
Very nice.

06:21.480 --> 06:27.450
Now there are other things we need to change for the movement, but I think this will be enough for

06:27.450 --> 06:27.960
this video.

06:27.960 --> 06:30.210
I know it was very short and we didn't do much.

06:30.210 --> 06:33.360
Maybe let's do a bit of a clean up and then we're going to jump to the next one.

06:33.360 --> 06:35.400
We're going to set up visuals for the movement.

06:35.400 --> 06:40.830
We can make a method to make our script cleaner from the start.

06:43.340 --> 06:44.900
Okay, so let's make a method.

06:44.900 --> 06:46.460
And this is how you make a method.

06:46.460 --> 06:49.550
You just go outside of a curly brackets of another method.

06:49.550 --> 06:53.270
But be sure you are within curly brackets of the class.

06:53.270 --> 06:56.390
And then you type here private void.

06:56.390 --> 06:57.680
Let's give it a name.

06:57.680 --> 07:00.860
For example handle movement.

07:01.410 --> 07:05.310
And press shift enter and it will open curly brackets for you.

07:05.340 --> 07:07.740
Now we can take this line of code.

07:07.740 --> 07:10.440
Just cut it out and paste it in here.

07:11.010 --> 07:15.810
And this method will now be responsible for the movement of the player.

07:15.840 --> 07:19.200
This will execute this line of code.

07:19.200 --> 07:22.140
But as you can see this method is not highlighted.

07:22.140 --> 07:24.090
So it is not working now.

07:24.090 --> 07:25.290
It will do nothing.

07:25.440 --> 07:26.850
Actually we can try that.

07:26.850 --> 07:28.470
We can go back to player.

07:28.470 --> 07:30.150
We can go back to play mode.

07:30.990 --> 07:35.550
Let's change the mode speed to four and in the play mode.

07:36.520 --> 07:43.510
You'll see he is not moving and we have more speed for that is happening because this function is not

07:43.510 --> 07:44.500
called anywhere.

07:44.500 --> 07:49.870
And to call this function, uh, all the time, because we want to apply movement all the time, we

07:49.870 --> 07:53.560
need to type it in the update like that.

07:53.560 --> 07:55.120
Handle movement.

07:55.120 --> 07:56.650
Now it will work.

07:56.650 --> 07:58.030
Let's go and try again.

08:06.410 --> 08:08.660
And today we can move.

08:08.660 --> 08:11.150
Super cool right now.

08:11.150 --> 08:14.240
Let's go back and see if we can do anything else.

08:14.240 --> 08:16.610
We actually can do something else.

08:16.610 --> 08:22.460
We could make another method for the input to make it even cleaner, but as of now it's not that important.

08:22.460 --> 08:24.650
So let's just leave it as it is.

08:24.650 --> 08:26.690
We're going to take care of it later on.

08:26.690 --> 08:30.410
Let's save the file and let's go to the next video.
