WEBVTT

00:00.110 --> 00:00.590
Hello guys.

00:00.590 --> 00:03.710
Welcome to the video and to a new section.

00:03.710 --> 00:04.940
Again, congratulations.

00:04.940 --> 00:07.460
We are very close to the finish of the game.

00:07.460 --> 00:09.290
I mean finish of the development.

00:09.290 --> 00:11.990
There are some things that can be done to improve the game.

00:11.990 --> 00:14.930
Like in this section we're going to add gamepad support.

00:14.930 --> 00:18.500
And in the next couple of sections we're going to add a bit of the enemies.

00:18.500 --> 00:20.060
But overall game is ready.

00:20.060 --> 00:22.310
And you don't really need to do that if you don't want to.

00:22.310 --> 00:28.580
But I suggest to stick with it, and I suggest to learn how we're going to use new input system to add

00:28.580 --> 00:29.660
a gamepad support.

00:29.660 --> 00:30.020
Okay.

00:30.020 --> 00:35.090
So, uh, regarding the new input system, it is a powerful tool.

00:35.090 --> 00:40.610
It allows you to use different input devices or even two of them at the same time and easily switch

00:40.610 --> 00:41.210
between them.

00:41.210 --> 00:43.160
But it takes some time to set up.

00:43.160 --> 00:48.260
And when you don't have any experience with a new input system, it may be super confusing.

00:48.260 --> 00:54.680
That's why I did not use it in the beginning of this course, because mostly this course is oriented

00:54.740 --> 00:58.490
on beginners, and I don't want to confuse anyone at the start.

00:58.490 --> 01:02.810
Like, I know it's super frustrating and super complicated, but it's very powerful.

01:02.810 --> 01:08.120
It's a good trade, honestly, if you just make yourself comfortable with the new input system, you

01:08.120 --> 01:10.730
will have no bounds in the development anyway.

01:10.730 --> 01:12.170
Let's begin by simple.

01:12.170 --> 01:16.880
Let's begin by going to um Package manager.

01:17.030 --> 01:21.770
And in unity registry we need to find new right.

01:21.770 --> 01:23.270
Where is it input.

01:25.050 --> 01:25.410
Yeah.

01:25.410 --> 01:26.370
Input system.

01:26.370 --> 01:27.990
Let's install it.

01:31.760 --> 01:32.930
So you need to build.

01:32.930 --> 01:37.100
Ask us to restart the unity so we can switch to a new input system.

01:37.100 --> 01:38.600
And we're going to click yes.

01:38.960 --> 01:41.630
It will take a second but it will be very fast.

01:41.630 --> 01:42.320
All right.

01:42.320 --> 01:43.550
We are here.

01:44.060 --> 01:45.200
Very cool.

01:47.010 --> 01:49.860
And now, now, now, now, now we've got it.

01:49.860 --> 01:53.460
We just need to check if new input system is enabled.

01:53.460 --> 02:01.860
We need to go to build profiles player settings, and then over here all the way below we have.

02:03.060 --> 02:03.960
Where is it?

02:05.060 --> 02:06.590
Active input handling.

02:08.720 --> 02:10.580
So we can enable both.

02:10.580 --> 02:15.410
I think it's fine because if we need to set up something with a gamepad and other controls, we can

02:15.410 --> 02:16.370
use new input system.

02:16.370 --> 02:20.210
But if we need to quickly check something, we can use all the input system.

02:20.210 --> 02:21.590
So I'm going to keep it both.

02:21.590 --> 02:22.370
It's fine.

02:22.370 --> 02:27.920
Now let's go and create the asset for the input system.

02:27.920 --> 02:29.510
We need to make a folder.

02:31.280 --> 02:34.550
Let's name it as Input Manager.

02:35.820 --> 02:36.780
Let's go inside.

02:36.780 --> 02:38.280
Let's click right button.

02:38.550 --> 02:44.640
Create input actions and I'm going to call it player input.

02:45.600 --> 02:50.700
Now when you open this, it will open a window where you can have different control schemes, different

02:50.700 --> 02:53.070
action maps and different actions.

02:53.520 --> 02:57.720
Again, if you are new to this, it can be confusing, but please don't worry, we're going to do a

02:57.720 --> 03:02.850
very simple setup and this simple setup will show you how you can manage basically any input you want

03:02.850 --> 03:03.540
to have.

03:03.810 --> 03:06.420
Let's create control scheme.

03:06.420 --> 03:09.060
First we want to do add control scheme.

03:09.300 --> 03:11.550
This will be named as keyboard.

03:12.060 --> 03:15.780
We're going to add device keyboard.

03:15.780 --> 03:16.860
Keyboard.

03:17.450 --> 03:18.410
All right.

03:20.560 --> 03:21.520
Let's, uh.

03:21.520 --> 03:22.060
Sorry.

03:22.060 --> 03:23.350
Why the name was changed again.

03:23.350 --> 03:27.190
Let's do keyboard and we're going to save.

03:27.310 --> 03:30.160
Then we're going to add one more add control scheme.

03:30.950 --> 03:32.900
Game pod we're going to do.

03:32.900 --> 03:35.900
Plus, uh, game pod.

03:35.900 --> 03:37.010
Game pod.

03:37.010 --> 03:37.550
Okay.

03:37.550 --> 03:38.570
Name again?

03:38.990 --> 03:39.920
Game pad.

03:40.990 --> 03:42.220
Now let's save it.

03:43.700 --> 03:48.530
And let's go back and choose all control schemes by default so we can see them here.

03:48.560 --> 03:55.520
Now we need an action map and usually action map used for different type of action and inputs.

03:55.550 --> 03:58.340
Good example is my top down shooter course.

03:58.340 --> 04:04.130
We have a robot who can run around and shoot, and also a robot can get into a car and when he's in

04:04.130 --> 04:06.290
the car, buttons doing different things.

04:06.290 --> 04:08.600
So that's what you need action maps for.

04:08.600 --> 04:12.350
But in this game we have only player controls, so we don't need much.

04:12.350 --> 04:14.570
I'm just going to name it as player.

04:14.570 --> 04:19.010
And then on the actions over here we need to take it and rename it.

04:19.940 --> 04:21.980
To movement.

04:22.520 --> 04:24.740
If you did not have anything here.

04:26.510 --> 04:29.450
You can just click plus and name it as.

04:30.570 --> 04:31.470
Movement.

04:31.470 --> 04:32.490
All right.

04:32.490 --> 04:38.700
Now this movement should be switched from button to value because we need a vector two value.

04:38.700 --> 04:39.060
Right.

04:39.060 --> 04:40.920
Horizontal and vertical input.

04:43.580 --> 04:46.100
Control type going to be vector two.

04:46.800 --> 04:48.990
And then let's go to bindings.

04:48.990 --> 04:51.390
And over here we're going to add.

04:52.330 --> 04:53.380
Wait a second.

04:55.790 --> 04:56.540
Wait a second.

04:56.780 --> 04:57.920
I kind of forgot.

04:57.950 --> 04:59.840
Let's delete this and let's add.

04:59.840 --> 05:04.670
Yeah, we need to click plus over here and choose add up down left right composite.

05:05.180 --> 05:05.750
Very nice.

05:05.750 --> 05:08.300
So this one has vector two digital normalized.

05:08.300 --> 05:11.720
And here we're going to have movements up down left and right.

05:11.870 --> 05:14.330
For now let's do this only for the keyboard.

05:14.330 --> 05:15.920
So we're going to go to path.

05:16.250 --> 05:17.630
We're going to click listen.

05:18.620 --> 05:23.840
Press w w then down his s.

05:24.580 --> 05:25.330
Left.

05:25.330 --> 05:34.390
Lesson A and right listen D if you want to have arrows as a controls on your keyboard, you need to

05:34.390 --> 05:42.040
add arrows as well so you can add to the vector and then go here click path listen and press the arrow

05:42.040 --> 05:42.460
key.

05:42.460 --> 05:45.610
But I'm not going to have arrows I don't need them okay.

05:45.610 --> 05:47.680
Now this is going to be a value vector.

05:47.680 --> 05:50.500
And we also need another action for the jump.

05:50.500 --> 05:52.750
So I'm going to add action here.

05:53.110 --> 05:55.150
Going to be called jump.

05:55.150 --> 05:57.910
And this one going to be action type button.

05:57.910 --> 06:01.690
Then we need to go and click plus and add the binding.

06:01.690 --> 06:03.220
And this one will be.

06:04.350 --> 06:06.540
And let's do listen space key.

06:06.540 --> 06:08.730
So this is the jump button.

06:08.730 --> 06:10.260
Let me delete the extra.

06:10.260 --> 06:11.850
And now we have jump.

06:11.850 --> 06:17.460
And let's select these buttons over here and choose keyboard on a control scheme.

06:18.510 --> 06:19.260
Like that.

06:21.020 --> 06:24.830
All right, we need one more button that would open UI.

06:24.860 --> 06:27.770
So I'm going to click New Action.

06:27.770 --> 06:31.760
It's going to be pause and for.

06:32.620 --> 06:33.430
This over here.

06:33.430 --> 06:38.110
We're going to do lesson escape and didn't work.

06:38.320 --> 06:43.390
Let's type escape, then escape on keyboard and keyboard control scheme.

06:43.840 --> 06:44.560
Nice.

06:44.560 --> 06:46.570
So that's all what we need for now.

06:46.720 --> 06:47.800
Uh, let's.

06:49.580 --> 06:50.870
Let's save this.

06:50.870 --> 06:53.660
Let's do a save asset and do not click auto save.

06:53.660 --> 06:57.560
Because if you do, every time you change something, it's going to compile everything.

06:57.560 --> 06:58.520
It's going to take long.

06:58.550 --> 07:01.310
Better change all together and then save as at once.

07:01.340 --> 07:05.600
Now we're going to close this and we need to generate C sharp class.

07:05.600 --> 07:10.700
So we can use it for the inputs so we can get access to things that are in here.

07:10.700 --> 07:13.250
Oh I'm sorry the things that are in here.

07:13.250 --> 07:16.760
And to do that we need to click generate C sharp class.

07:17.090 --> 07:18.680
You can change the name if you want.

07:18.710 --> 07:20.540
Let's type player input.

07:22.250 --> 07:24.440
And we're going to click apply.

07:24.470 --> 07:24.920
Now.

07:24.920 --> 07:32.030
It will create the player input C sharp script that has all of the functionality that we made right

07:32.030 --> 07:33.620
now in that UI.

07:35.430 --> 07:35.760
Right.

07:35.760 --> 07:40.050
You can see it's here written player ID movement value vector two.

07:40.050 --> 07:43.980
Everything that we just made is here in a form of a script.

07:43.980 --> 07:46.080
And don't get scared.

07:46.080 --> 07:47.430
We don't need to deal with this.

07:47.430 --> 07:48.840
We can close it easily.

07:48.840 --> 07:51.990
And as of now we can just close everything but player.

07:52.020 --> 07:55.890
Now on the player we have all the input system working.

07:56.220 --> 07:57.180
Where is it?

07:57.420 --> 07:58.620
Handle.

07:59.790 --> 08:01.860
Handle input this one.

08:02.750 --> 08:05.120
And we're not going to use it any longer.

08:05.120 --> 08:08.840
So it is safe to comment out this method.

08:09.230 --> 08:13.760
Let's comment the functionality inside of it, just in case you want to keep it.

08:13.760 --> 08:17.900
Maybe you want to check something or bring it back, so I'm going to comment it out.

08:17.900 --> 08:19.100
I will not delete it.

08:21.870 --> 08:27.360
Then in the update over here, we're gonna comment out handleinput.

08:28.030 --> 08:28.840
Nice.

08:28.840 --> 08:34.270
Now to use new input, we need to make the input and we need to get access to it.

08:34.270 --> 08:35.770
And then we need to use it.

08:35.770 --> 08:41.860
This is how are we going to do that all the way above over here I'm going to do private player input.

08:42.750 --> 08:45.090
Player input just like that.

08:45.090 --> 08:49.980
So we created a variable of the player input that we just generated in unity.

08:50.400 --> 08:51.360
Then.

08:52.370 --> 08:54.080
Then we need to go to the awake.

08:54.080 --> 08:59.810
And over here we're going to type player input equals to new player input.

08:59.810 --> 09:02.930
So we create a new instance of a player input that we have.

09:02.930 --> 09:05.000
And we're going to use that inside of this script.

09:05.000 --> 09:07.370
So we don't need to assign anything and variables.

09:07.370 --> 09:08.810
We don't need to do anything else.

09:08.810 --> 09:09.800
Very nice.

09:09.980 --> 09:15.440
And then we need an enable method to enable the inputs and assign them.

09:15.440 --> 09:16.610
This is a common practice.

09:16.610 --> 09:18.020
This is how it's usually done.

09:18.020 --> 09:20.240
Let's do private void on disable.

09:20.240 --> 09:23.150
So on enable we're going to enable actions on disable.

09:23.150 --> 09:24.890
We're going to disable actions.

09:24.890 --> 09:26.570
That's how it's supposed to be done.

09:26.570 --> 09:27.140
All right.

09:27.140 --> 09:30.770
So let's go to enable and do player input dot enable.

09:30.770 --> 09:32.300
So we enable the actions.

09:32.300 --> 09:38.180
So in the new input system you can make different actions just like we did in the player input manager.

09:38.180 --> 09:40.580
If you remember just now we made movement.

09:40.580 --> 09:42.830
We made jump and we made pose actions.

09:42.830 --> 09:47.240
And you can check and subscribe to different phases of those actions.

09:47.240 --> 09:51.380
It can be started, performed or canceled for the next line of code.

09:51.380 --> 09:55.670
For the jump we're going to use performed because we want to check when action was completed and then

09:55.670 --> 09:58.850
when action was completed, we're going to make character jump.

09:58.850 --> 10:02.990
So how it's all working together, let's just type first player input.

10:02.990 --> 10:04.400
So this is the input.

10:04.400 --> 10:08.360
Then we need to get access to the action map which is player.

10:08.360 --> 10:11.930
Then we need to get access to the action itself which is jump.

10:11.930 --> 10:15.890
Then we need to see what happened to the action if it was performed or canceled.

10:15.890 --> 10:17.240
And we need performed.

10:17.240 --> 10:21.710
So when action is performed and then we're going to type plus equals CT.

10:22.070 --> 10:26.150
And CT is a basically short for context context.

10:26.150 --> 10:31.220
Then we're going to do lambda expression and type here jump button.

10:31.220 --> 10:34.580
And I know we're going to use the new input system.

10:34.580 --> 10:37.250
That's why I made jump button method over here.

10:37.250 --> 10:39.410
That has all of the jump functionality.

10:39.500 --> 10:43.490
Before we go any further let's try to understand CT over there.

10:43.490 --> 10:46.160
So CT stands for context.

10:46.160 --> 10:49.460
And basically it is a callback from the input action.

10:49.460 --> 10:55.670
This one contains information about the input event, such as the values of the input or the time when

10:55.670 --> 10:56.390
it occurred.

10:56.390 --> 10:59.600
In this case, exactly, we do not use context.

10:59.600 --> 11:04.880
We're going to use it later on for the movement, but I just prefer to type it because later on when

11:04.880 --> 11:08.270
we have entire code, it all looks the same and pretty.

11:08.270 --> 11:13.070
And besides, maybe you need the context from it later on, so I just prefer to type it this way.

11:13.070 --> 11:14.450
So basically that's what it is.

11:14.450 --> 11:19.280
Context contains information about the input such as the value or time.

11:19.280 --> 11:25.490
And also it has information about what we used to do the input like button or joystick or anything else.

11:25.490 --> 11:30.590
Now let's take a look at this line again and let's try to understand what it does all together, how

11:30.590 --> 11:31.130
it works.

11:31.130 --> 11:38.570
Basically, we subscribe this method to the event of button being pressed, and every time button pressed

11:38.570 --> 11:40.730
this method will be called as well.

11:40.730 --> 11:42.260
That's the basic of it.

11:42.810 --> 11:49.650
And over here on disable we want to do player input input disable and we want to unsubscribe from the

11:49.650 --> 11:50.010
event.

11:50.010 --> 11:58.140
So we're going to do player input player jump performed minus equals CTCs jump button.

12:00.360 --> 12:02.220
Okay, now let's save this.

12:02.220 --> 12:05.010
Let's go back to unity and see if it works.

12:05.520 --> 12:06.510
We're going to close it.

12:06.510 --> 12:08.520
We're going to go to one of the scenes.

12:08.520 --> 12:09.780
Let's do level one.

12:09.930 --> 12:11.490
Let's go to play mode.

12:13.670 --> 12:19.880
And the old input system will not work because we commented the functionality in the update.

12:19.970 --> 12:22.430
Right, I cannot walk, but if I jump.

12:23.080 --> 12:24.100
I'm going to jump.

12:25.060 --> 12:26.020
Yeah.

12:26.020 --> 12:27.670
Jump with a new input system.

12:27.670 --> 12:32.260
Now let's go back to the script and do the movement functionality.

12:32.290 --> 12:34.690
So in the player input.

12:35.520 --> 12:36.000
Sorry.

12:36.000 --> 12:36.540
Give me a second.

12:36.540 --> 12:37.890
I'm going to explain this.

12:37.890 --> 12:42.780
Let's open this and let's attach it over here so we can always open it again.

12:43.050 --> 12:43.590
All right.

12:43.590 --> 12:50.430
So on the player input over here we have movement and it has 2D vector value which is basically x value

12:50.430 --> 12:52.560
and y value that we need for movement.

12:53.200 --> 12:56.290
And it's going to be very similar to what we had with the old input system.

12:56.290 --> 13:00.940
We only need to replace this with one vector that would be easier for us.

13:00.940 --> 13:04.660
So instead of having float, let's make private vector two.

13:05.480 --> 13:08.210
Move input just like that.

13:08.750 --> 13:12.890
And maybe it is a good idea to take it away from here and.

13:13.870 --> 13:17.140
Place it closer to the player input like that.

13:18.080 --> 13:20.240
So it's like all inputs are together.

13:21.240 --> 13:21.660
Now.

13:21.660 --> 13:23.220
Let's go.

13:24.560 --> 13:26.840
And fixed a bunch of errors that we have.

13:27.560 --> 13:30.890
Uh, one of them is here because we're trying to use y input.

13:30.890 --> 13:33.890
We need to do move input dot y.

13:34.810 --> 13:37.990
Then we're going to have to fix X input over here.

13:37.990 --> 13:41.500
We're going to do move input dot x.

13:42.450 --> 13:44.310
And in the handle flip as well.

13:44.310 --> 13:46.110
Move input dot x.

13:47.440 --> 13:49.000
Move input dot x.

13:49.600 --> 13:50.860
Okay, this is good.

13:50.860 --> 13:54.100
Now we just need to assign this value when we press in the buttons.

13:54.100 --> 13:56.020
That will be super easy to do.

13:56.020 --> 13:58.450
Let's go back to UN enable.

14:00.770 --> 14:01.370
Where is it?

14:01.370 --> 14:02.900
On the table over here.

14:02.900 --> 14:04.460
And we're going to do this.

14:04.880 --> 14:10.370
Player input player movement performed plus equals.

14:11.390 --> 14:11.930
CT.

14:12.230 --> 14:16.820
And we're going to put lambda expression, which basically means like return me this.

14:16.820 --> 14:24.290
And when we return this we just need to type move input equals to context which is CT in our case read

14:24.290 --> 14:26.270
value of vector two.

14:27.630 --> 14:28.500
Just like that.

14:28.500 --> 14:30.720
And then when input is cancelled.

14:30.720 --> 14:40.140
So player input, player movement cancelled, we're going to do Ctrl x return move input equals to vector

14:40.140 --> 14:41.160
2.0.

14:41.900 --> 14:42.410
Just like that.

14:42.410 --> 14:46.700
So when we're not pressing any buttons, when input is cancelled, we're going to set it back to zero

14:46.700 --> 14:50.240
because otherwise character will keep moving even if you release the button.

14:50.510 --> 14:51.890
Now let's take these two.

14:51.920 --> 14:53.270
Let's copy them.

14:53.600 --> 14:54.980
Let's go back here.

14:55.870 --> 14:57.010
And paste them in.

14:57.010 --> 15:00.070
And instead of plus over here we're going to do minus.

15:00.370 --> 15:02.620
And now we'll have movement of the player.

15:02.620 --> 15:04.030
Let's save this.

15:04.360 --> 15:05.530
And.

15:07.420 --> 15:07.870
Yeah.

15:07.870 --> 15:08.770
This should be good.

15:08.800 --> 15:09.850
Let's go back.

15:11.950 --> 15:13.300
Let's go to play mode.

15:14.910 --> 15:18.660
And we can move with the new input system to the.

15:19.620 --> 15:21.030
So basically that's it.

15:21.030 --> 15:24.690
I'm sorry it took a bit longer than it could, but I didn't want to rush it.

15:24.690 --> 15:27.750
I wanted to explain everything slowly and easy.

15:27.750 --> 15:33.360
I hope I did achieve the goal and you understood how we could do different inputs.

15:33.360 --> 15:37.890
You basically just add in a value for vector two or button for action type.

15:37.890 --> 15:43.530
We also have this um pose input, but we're going to deal with it in the in other video.

15:43.530 --> 15:44.160
All right.

15:44.160 --> 15:45.810
Because now pose will not work.

15:45.810 --> 15:47.460
You cannot pause the game okay.

15:47.460 --> 15:48.450
Thank you so much.

15:48.450 --> 15:50.220
And I'll see you in the next video.
