Hello, guys.
Welcome back.
And in this video, we're going to move our player for the first time.
I'm going to talk a bit about coding.
How are we going to use code in how to make script?
And all of that is very easy and common practice.
I'm sure you will understand it.
And if not in this video, then in the next one just be sure to follow me and repeat my actions.
Because when you do it, your brain absorbs the information.
And he trying to understand that he remembers the code, he remember the movement of the fingers and
so on.
Doing that stuff is essential for learning, right?
So there are two ways to add script to the player.
First is just add component new script.
You give it a name and I'll name minus player to that.
It is done well, let it assemble.
Or you could make it an asset folder at first place, so I'll remove it and delete this one.
Once again, red button, create sharp layer.
And then just drag it here and we can open it.
And this is Visual Studio for now.
As I said, just repeat after me and I'll explain something by bit so you can understand it, because
if I'll let you know all info at once, you'll just feel like, Wow, that's too much for me.
Yeah, what are we going to do here is go above the start function and type public float, move speed.
All right.
So what we did here, we created a variable with a type float and we gave it a name move.
Speed and public means it is accessible from any part of the program, from any part of the script,
from any part of the unit.
We can find it from anywhere and use it.
Major difference for now.
That is, we can see it in the inspector.
All right, So now, look, if I save the script and go back to Unity, I can see my move.
Speed over here and I can change it.
And you might notice I have decimals over there.
So it is 7.6 and 3.7 like that.
Now I want to go back and create another one public float jump force just like that.
Now we can see jump force as well and we can change it as well.
But what if we change this float to int?
Let's do it INT and save it.
And immediately you can see we lost the number and now if we change it, it goes as a whole number.
There is no decimals.
That is the difference between float and integer and they use it for different purposes.
Like move speed can be 4.5.
Right?
But if you have, let's say, amount of coins collected, you cannot have 4.5 coins should be either
four or five, something like that.
Now let's go and type here float.
We have these two variables.
We're going to use them.
And I want to make another variable here.
It's going to be public as well.
Let's make it public.
Read it by the two.
And now we create an a variable of type rigid body.
You can think of them as a jars that store, different information, different type of variables, store
different information.
This one stores info about rigid body.
Now let's give it a name.
Let's put a label on it.
RB Now, if we go back to Unity, we can see it's appeared here.
RB And it says none.
So it is empty and we need to assign it, same as we did just now with the numbers.
So to assign it for now, I'll take my rigid body from the player and just drag it like that.
And this move speed.
I'm going to set to one and jump first to one for the time being.
Now we have the player, we have access to Region body, as you understand now, are being the code.
It's the name of the variable we created.
And these are be the very same.
This are B and these are B is assigned to this rigid body that we have on the player that we control.
And now we can control this rigid body to move the player.
And let's do that.
Just repeat after me in the update.
We type.
RB That velocity equals two new vector two and vector two.
You know, what is x and Y, right?
Pretty simple.
Now we want to move object on the x, We want to make player run to one of the sides.
If you put positive value here, going to run to the right.
If you put negative value, you're going to run to the left.
So let's try that.
Let's just type here.
Speed, move, speed.
Sorry, move speed.
Right.
And for the Y and actually all the info you can see here what it needs vector to construct a new vector
with a given x y component.
Right.
For the Y, we're going to type rb dot velocity, dot y y is that we don't change y of the rigid body,
but if we put zero, it means we set value to zero.
And we don't need to do that.
We need to keep it on velocity on the Y, So we just type RB velocity dot y.
All right, enough talking.
Let's go back to the unity and try this.
I'm going to place my player somewhere at the beginning like that.
I'm going to take my platform.
I'm making bigger like that.
And just make sure you have over here selected 16 to 9 aspect instead of three aspect.
Right.
Cool.
Now hit play and watch this.
He moves slowly, but he does.
If I increase the speed, you're going to move faster.
And if I make it negative.
What's he doing?
He's moving back.
Because we change in his velocity and at the same time we change his position.
You can see in four about velocity of the rigid body over here.
Infer velocity.
You can see it's getting changed.
Oh, here, right now.
You've seen he falls, he's rotating on the Z.
So I'm offering you a small challenge.
In this video, you need to do it right now and just make him not fall on the side like he did.
Make him stand straight, even if he's moving past the video.
Do that.
Come back if you did or if you didn't, it's fine.
And I'll help you to fix that one, too.
So we go to constraints of the rigid body and click Freeze rotation Z.
Now he won't fall and we can increase speed by a bit like three and see how it works.
Doing.
He's running Yeah basically and let's runner is done.
He's running endlessly.
Cool.
Let's go to the next video.