Hello guys.
Welcome to the video.
And finally we have to set up movement of the player.
I'm very happy about it.
Let's do it right now.
It's going to be not that difficult because actually we did something similar in the first section of
this course.
Let's go to player now.
And here we need to save input that we have from a keyboard and then apply it to the movement together
with the move speed.
You can edit input before.
If you remember we typed input dot get axis horizontal.
But now I want to store this input in a variable so I can use it across different places in the script.
Let's make uh, let's make public float first because I want to see the while I'm testing it.
And then we're going to make it private.
So it's going to be private float X input.
And then in the update.
We can do x input equals to.
Input.
Get access.
Horizontal.
Now let's save this.
Go back to unity.
We have x input which is zero, and if we press x input like add or arrow keys, we can see that value
is changing from 0 to 1 or to minus one.
And we can see it slowly grows or slowly decreases to the needed value.
So it's like does not become 1 or -1 immediately.
This will uh, result a movement where player is speeding up slowly, which I don't really want to have.
I want to have quick, snappy, responsive movement.
And because of that we're going to use get axis row over here.
We're going to type row.
And now if you save this and try this again you will see that x input is changing to the -1 or 1 immediately.
Without that gradual change you can see one minus one.
This is very good for us because I want to have snappy, responsive controls on this platformer.
Now let's go back to player.
And use this input to move the character.
I'll just have to make it private because I don't need to see it anymore.
We know how it works.
And in the update we're going to use RB dot velocity equals to new vector two x input.
And for the y we're going to use rb velocity dot y itself.
Let's just try it first.
And then I'm going to explain what we did here.
Let's go to play mode.
And I can move to the left or to the right very slowly, but I'm moving, which is very cool.
Now what actually just happened?
Um, RB is a rigid body component, which we discussed before.
So by getting a component over here, we basically say that this RB is this rigid body over here.
And then when we type in RB in the update, we basically say, hey you rigid body, listen to me.
And then we set dot velocity and we control velocity of that rigid body.
Now why do we set it to vector two.
That is a very simple question with a very simple answer.
Everything in 2D world works with a vector two.
I actually have a good video that explains how vector two is working.
So maybe I will attach link to this lecture and you can just check it.
It's a really nice one I think.
But shortly I'll explain that you can see only two vectors y and x.
And the rule is simple.
You want to move, object to the right.
Change his position on the X, make it bigger.
It will move to the right.
Make it lower.
It will move to the left.
Because this is how vector two is working, right?
We have x y and it goes that side.
So 123 minus one minus two minus three.
Same for the Y123 minus one minus two minus three.
You want character.
Move to the right.
Change his velocity or position and make it x something like a positive value.
You want to move it to the left.
Make it minus x, something like minus four, minus three and so on.
And velocity of the rigid body always wants to know in which direction it's supposed to move.
And by saying that we change only x in the vector two, it knows that we applying.
Vector one over here.
And that's why it moves to the right or minus one over here.
And that's why it moves to the left.
Regarding the Y over here, even though we don't want to change velocity on the Y, we only want to
have horizontal movement.
We cannot type zero here because if you type zero here, it means you're trying to set velocity of the
y to the zero, which we don't really need.
We only want to change one velocity on the x.
Because of that, we type in our velocity dot y because in this way it will take an x input as the x
velocity.
But y velocity will be unchanged because it will take its own velocity.
You can say we python just hey, take the same velocity, don't change anything for the y, but for
the x input use this value over here.
Simple as that.
Yeah.
Well, that's actually was faster than I expected.
Now we want to control speed of the character.
And because of that, we can just take this x input and multiply it by move speed.
We have over there.
Let's do multiply move speed.
Now we can go back to unity and try this out.
Let's just go to play mode.
And now, if you'll try to move, nothing will happen because your move speed is zero and we are multiplying
x input by zero, which results a zero.
Let's go back and change move speed to something like four and try to move now.
And he moves faster.
He's responsible.
He's moving which I really, really like.
Very cool.
Yay!
We can make him faster.
Maybe eight, and this will work as well.
Very nice.
Now there are other things we need to change for the movement, but I think this will be enough for
this video.
I know it was very short and we didn't do much.
Maybe let's do a bit of a clean up and then we're going to jump to the next one.
We're going to set up visuals for the movement.
We can make a method to make our script cleaner from the start.
Okay, so let's make a method.
And this is how you make a method.
You just go outside of a curly brackets of another method.
But be sure you are within curly brackets of the class.
And then you type here private void.
Let's give it a name.
For example handle movement.
And press shift enter and it will open curly brackets for you.
Now we can take this line of code.
Just cut it out and paste it in here.
And this method will now be responsible for the movement of the player.
This will execute this line of code.
But as you can see this method is not highlighted.
So it is not working now.
It will do nothing.
Actually we can try that.
We can go back to player.
We can go back to play mode.
Let's change the mode speed to four and in the play mode.
You'll see he is not moving and we have more speed for that is happening because this function is not
called anywhere.
And to call this function, uh, all the time, because we want to apply movement all the time, we
need to type it in the update like that.
Handle movement.
Now it will work.
Let's go and try again.
And today we can move.
Super cool right now.
Let's go back and see if we can do anything else.
We actually can do something else.
We could make another method for the input to make it even cleaner, but as of now it's not that important.
So let's just leave it as it is.
We're going to take care of it later on.
Let's save the file and let's go to the next video.