Hello guys.
Welcome to the video and to a new section.
Again, congratulations.
We are very close to the finish of the game.
I mean finish of the development.
There are some things that can be done to improve the game.
Like in this section we're going to add gamepad support.
And in the next couple of sections we're going to add a bit of the enemies.
But overall game is ready.
And you don't really need to do that if you don't want to.
But I suggest to stick with it, and I suggest to learn how we're going to use new input system to add
a gamepad support.
Okay.
So, uh, regarding the new input system, it is a powerful tool.
It allows you to use different input devices or even two of them at the same time and easily switch
between them.
But it takes some time to set up.
And when you don't have any experience with a new input system, it may be super confusing.
That's why I did not use it in the beginning of this course, because mostly this course is oriented
on beginners, and I don't want to confuse anyone at the start.
Like, I know it's super frustrating and super complicated, but it's very powerful.
It's a good trade, honestly, if you just make yourself comfortable with the new input system, you
will have no bounds in the development anyway.
Let's begin by simple.
Let's begin by going to um Package manager.
And in unity registry we need to find new right.
Where is it input.
Yeah.
Input system.
Let's install it.
So you need to build.
Ask us to restart the unity so we can switch to a new input system.
And we're going to click yes.
It will take a second but it will be very fast.
All right.
We are here.
Very cool.
And now, now, now, now, now we've got it.
We just need to check if new input system is enabled.
We need to go to build profiles player settings, and then over here all the way below we have.
Where is it?
Active input handling.
So we can enable both.
I think it's fine because if we need to set up something with a gamepad and other controls, we can
use new input system.
But if we need to quickly check something, we can use all the input system.
So I'm going to keep it both.
It's fine.
Now let's go and create the asset for the input system.
We need to make a folder.
Let's name it as Input Manager.
Let's go inside.
Let's click right button.
Create input actions and I'm going to call it player input.
Now when you open this, it will open a window where you can have different control schemes, different
action maps and different actions.
Again, if you are new to this, it can be confusing, but please don't worry, we're going to do a
very simple setup and this simple setup will show you how you can manage basically any input you want
to have.
Let's create control scheme.
First we want to do add control scheme.
This will be named as keyboard.
We're going to add device keyboard.
Keyboard.
All right.
Let's, uh.
Sorry.
Why the name was changed again.
Let's do keyboard and we're going to save.
Then we're going to add one more add control scheme.
Game pod we're going to do.
Plus, uh, game pod.
Game pod.
Okay.
Name again?
Game pad.
Now let's save it.
And let's go back and choose all control schemes by default so we can see them here.
Now we need an action map and usually action map used for different type of action and inputs.
Good example is my top down shooter course.
We have a robot who can run around and shoot, and also a robot can get into a car and when he's in
the car, buttons doing different things.
So that's what you need action maps for.
But in this game we have only player controls, so we don't need much.
I'm just going to name it as player.
And then on the actions over here we need to take it and rename it.
To movement.
If you did not have anything here.
You can just click plus and name it as.
Movement.
All right.
Now this movement should be switched from button to value because we need a vector two value.
Right.
Horizontal and vertical input.
Control type going to be vector two.
And then let's go to bindings.
And over here we're going to add.
Wait a second.
I kind of forgot.
Let's delete this and let's add.
Yeah, we need to click plus over here and choose add up down left right composite.
Very nice.
So this one has vector two digital normalized.
And here we're going to have movements up down left and right.
For now let's do this only for the keyboard.
So we're going to go to path.
We're going to click listen.
Press w w then down his s.
Left.
Lesson A and right listen D if you want to have arrows as a controls on your keyboard, you need to
add arrows as well so you can add to the vector and then go here click path listen and press the arrow
key.
But I'm not going to have arrows I don't need them okay.
Now this is going to be a value vector.
And we also need another action for the jump.
So I'm going to add action here.
Going to be called jump.
And this one going to be action type button.
Then we need to go and click plus and add the binding.
And this one will be.
And let's do listen space key.
So this is the jump button.
Let me delete the extra.
And now we have jump.
And let's select these buttons over here and choose keyboard on a control scheme.
Like that.
All right, we need one more button that would open UI.
So I'm going to click New Action.
It's going to be pause and for.
This over here.
We're going to do lesson escape and didn't work.
Let's type escape, then escape on keyboard and keyboard control scheme.
Nice.
So that's all what we need for now.
Uh, let's.
Let's save this.
Let's do a save asset and do not click auto save.
Because if you do, every time you change something, it's going to compile everything.
It's going to take long.
Better change all together and then save as at once.
Now we're going to close this and we need to generate C sharp class.
So we can use it for the inputs so we can get access to things that are in here.
Oh I'm sorry the things that are in here.
And to do that we need to click generate C sharp class.
You can change the name if you want.
Let's type player input.
And we're going to click apply.
Now.
It will create the player input C sharp script that has all of the functionality that we made right
now in that UI.
Right.
You can see it's here written player ID movement value vector two.
Everything that we just made is here in a form of a script.
And don't get scared.
We don't need to deal with this.
We can close it easily.
And as of now we can just close everything but player.
Now on the player we have all the input system working.
Where is it?
Handle.
Handle input this one.
And we're not going to use it any longer.
So it is safe to comment out this method.
Let's comment the functionality inside of it, just in case you want to keep it.
Maybe you want to check something or bring it back, so I'm going to comment it out.
I will not delete it.
Then in the update over here, we're gonna comment out handleinput.
Nice.
Now to use new input, we need to make the input and we need to get access to it.
And then we need to use it.
This is how are we going to do that all the way above over here I'm going to do private player input.
Player input just like that.
So we created a variable of the player input that we just generated in unity.
Then.
Then we need to go to the awake.
And over here we're going to type player input equals to new player input.
So we create a new instance of a player input that we have.
And we're going to use that inside of this script.
So we don't need to assign anything and variables.
We don't need to do anything else.
Very nice.
And then we need an enable method to enable the inputs and assign them.
This is a common practice.
This is how it's usually done.
Let's do private void on disable.
So on enable we're going to enable actions on disable.
We're going to disable actions.
That's how it's supposed to be done.
All right.
So let's go to enable and do player input dot enable.
So we enable the actions.
So in the new input system you can make different actions just like we did in the player input manager.
If you remember just now we made movement.
We made jump and we made pose actions.
And you can check and subscribe to different phases of those actions.
It can be started, performed or canceled for the next line of code.
For the jump we're going to use performed because we want to check when action was completed and then
when action was completed, we're going to make character jump.
So how it's all working together, let's just type first player input.
So this is the input.
Then we need to get access to the action map which is player.
Then we need to get access to the action itself which is jump.
Then we need to see what happened to the action if it was performed or canceled.
And we need performed.
So when action is performed and then we're going to type plus equals CT.
And CT is a basically short for context context.
Then we're going to do lambda expression and type here jump button.
And I know we're going to use the new input system.
That's why I made jump button method over here.
That has all of the jump functionality.
Before we go any further let's try to understand CT over there.
So CT stands for context.
And basically it is a callback from the input action.
This one contains information about the input event, such as the values of the input or the time when
it occurred.
In this case, exactly, we do not use context.
We're going to use it later on for the movement, but I just prefer to type it because later on when
we have entire code, it all looks the same and pretty.
And besides, maybe you need the context from it later on, so I just prefer to type it this way.
So basically that's what it is.
Context contains information about the input such as the value or time.
And also it has information about what we used to do the input like button or joystick or anything else.
Now let's take a look at this line again and let's try to understand what it does all together, how
it works.
Basically, we subscribe this method to the event of button being pressed, and every time button pressed
this method will be called as well.
That's the basic of it.
And over here on disable we want to do player input input disable and we want to unsubscribe from the
event.
So we're going to do player input player jump performed minus equals CTCs jump button.
Okay, now let's save this.
Let's go back to unity and see if it works.
We're going to close it.
We're going to go to one of the scenes.
Let's do level one.
Let's go to play mode.
And the old input system will not work because we commented the functionality in the update.
Right, I cannot walk, but if I jump.
I'm going to jump.
Yeah.
Jump with a new input system.
Now let's go back to the script and do the movement functionality.
So in the player input.
Sorry.
Give me a second.
I'm going to explain this.
Let's open this and let's attach it over here so we can always open it again.
All right.
So on the player input over here we have movement and it has 2D vector value which is basically x value
and y value that we need for movement.
And it's going to be very similar to what we had with the old input system.
We only need to replace this with one vector that would be easier for us.
So instead of having float, let's make private vector two.
Move input just like that.
And maybe it is a good idea to take it away from here and.
Place it closer to the player input like that.
So it's like all inputs are together.
Now.
Let's go.
And fixed a bunch of errors that we have.
Uh, one of them is here because we're trying to use y input.
We need to do move input dot y.
Then we're going to have to fix X input over here.
We're going to do move input dot x.
And in the handle flip as well.
Move input dot x.
Okay, this is good.
Now we just need to assign this value when we press in the buttons.
That will be super easy to do.
Let's go back to UN enable.
Where is it?
On the table over here.
And we're going to do this.
Player input player movement performed plus equals.
CT.
And we're going to put lambda expression, which basically means like return me this.
And when we return this we just need to type move input equals to context which is CT in our case read
value of vector two.
Just like that.
And then when input is cancelled.
So player input, player movement cancelled, we're going to do Ctrl x return move input equals to vector
2.0.
Just like that.
So when we're not pressing any buttons, when input is cancelled, we're going to set it back to zero
because otherwise character will keep moving even if you release the button.
Now let's take these two.
Let's copy them.
Let's go back here.
And paste them in.
And instead of plus over here we're going to do minus.
And now we'll have movement of the player.
Let's save this.
And.
Yeah.
This should be good.
Let's go back.
Let's go to play mode.
And we can move with the new input system to the.
So basically that's it.
I'm sorry it took a bit longer than it could, but I didn't want to rush it.
I wanted to explain everything slowly and easy.
I hope I did achieve the goal and you understood how we could do different inputs.
You basically just add in a value for vector two or button for action type.
We also have this um pose input, but we're going to deal with it in the in other video.
All right.
Because now pose will not work.
You cannot pause the game okay.
Thank you so much.
And I'll see you in the next video.