Welcome back, my fellow game developers.
In this video, we are actually going to put the inputs we created to
use and we're going to see how important variables are.
So now when we run our gain,
you'll notice that we don't just stand there.
You don't have anything in the console.
When we press the WASD key,
we can see that our player moves left,
right, left, up, down.
And not only that, if I increase the movement speed,
there we go, We are much faster.
So not only do we move the player with the keys,
we also can control how fast he moved.
So well, that said,
let's not waste any more time and let's get started.
Okay, so now that we have the inputs ready,
we see how they work.
It's time to actually start moving the player.
As we promised.
Let's open up the script right here.
And let's go ahead and remove these debug dot log.
These were just here too.
Make us believe that this is actually working.
Okay, so the first thing that we need to do is we
need to access the transformed position of the player.
So we're going to go ahead and click Transform.
And you can see right here how helpful visual Studio is already.
It's telling us what the transform is.
So the transform that will write right now is the transport attached to this game object.
Which game object?
It's the game object that has the player controller script on it.
And if we look back in here,
we click on the layer.
Anytime now we click on the player.
We can see that the player controller is attached to the player and
the transform which is on the gameObject don't player as this one.
So we're accessing this specific transform.
So back in here,
we'll write transform again,
hit Tab to complete the word dot position.
You can see that can access the position, the scale,
the rotation, everything that is available on the transform.
And we write position.
Now, this is going to be equal to a new vector three.
And it's going to be,
let's say for now just 0.1 f.
And we can also write dot 1 f. This means 0,
and we'll keep the z axis on the same place. We'll save that.
Now, you might be thinking why Vector3
didn't we just say that we don't want to modify this?
Well, if we hover over position,
you can see that the world space position of the transform as a vector three,
that can also go through the documentations and see that it
is actually the transformed position of any object,
regardless whether we are using 3D,
2D 1D, That's impossible,
but you get the picture vector three position.
We always access the vector C position,
but we keep it as 0 f.
Now the scope is not complete,
but I'm going to use it just so we learn as we make mistakes,
I'm going to save this.
I'm going to go back in here.
And let's just get some real values in here so we know,
so minus five and let's say this is minus1 will save that.
We're going to run our game and let's look. There we go.
So 0.10.1.
Now, what seems to be the problem?
Aren't we calling it every single frame?
Well, the problem here is that we are simply assigning this value right here,
the new vector, 30.10.10 f to the position.
Every single frame we are not increasing the transform position.
So how are we going to start increasing?
Let me just stop the playwright here.
We're going to write that we take the transform dot position of the game object
and then we add to it the 0.10.1 vector 3, every single frame.
So we're going to save that.
And let's go back and look at our game,
how it looks right now.
We run the game and there you go. Very goals.
The player has just left the building,
as you can see, as you can see right now what happens in here.
We take the transform.py position.
We assign in the already available transform,
but position plus the vector 3,
and we do that every single frame.
So let's say we start off at 0, 0.
We take 000 right here,
we add to this vector,
the next frame we have 0.21.
We take it, we add to it, it becomes 0.2.
We take it, we add to it becomes 0.3.
So every single frame we increase it.
Now, as you can see, there is redundant code in here.
Redundant means we are using it multiple times.
To avoid this, we can simply remove this right
here and instead add a plus behind the equals.
And this is the exact same syntax as before.
So if we write this again,
let me just duplicate this.
I'll simply add two slashes.
This becomes a comment.
And as we've said,
a common does not execute the code,
and we'll simply remove this.
So as a small reminder of what this plus equals means.
So we'll save that.
We'll go back into our game and let's just make this a bit smaller,
so we'll add another 0 and here,
save that back in our game.
We run the code, the game.
And there we go,
we can see him slowly floating away and again, great.
So now we have our player moving around.
But we don't want these values to be just arbitrary.
We don't want to have 0.1 or 0.01.
We actually want the input that we've gotten here to move the player.
So this will be your challenge.
Your challenge is to move the player position with the input.
So you're going to access the position of the player's transform.
You're going to make the position a new vector 3.
And you're going to use the movement input.txt and dot
y as the x and y of this new vector
3 will also keep the z axis at 0 F. And some just a bit of extra help.
I recommend that you go through the Unity documentation and see the example.
They were so nice at Unity company somewhere
to give us an example of exactly what we are looking for.
So pause the video right now and go do the challenge.
Okay, welcome back.
So if we scroll down right here,
you can see that we get the horizontal input,
we get the vertical input,
and then we put all of them inside of the transformed position.
This is the exact example that we need and our gain.
So if we go back in here, instead of 0.01,
I'm going to write the movement input dot x. I'm going to write the movement input dot y.
And I'm going to save all of that.
Now back in our game,
we run the game and now our player doesn't move.
If we want to move him to the right,
we had the Dickey he moves to the right,
the left, and he disappeared.
So as you can see, our player is extremely, extremely fast.
Why is that? Well, it's because updates being called every single frame.
So if we try to play again,
let me show you what I mean.
If we look up here on the stats,
we can see how many frames per second we have.
So you know what frames per second is?
If you've ever played a game and you can see right now
it's almost a thousand frames per second.
We are calling this a thousand times, a 1000 times.
We are adding one,
so we're adding a 1000 every time or every second if you'd like.
So how are we going to control this?
Well, there is a very simple way,
and that is by multiplying this by Time.deltaTime.
And what happens right now,
Time.deltaTime, if we hover over it,
it's the interval and seconds from the last frame to the current one.
So what this does is it takes the time between each frame,
we multiply it by this one and now will become frame rate and dependent.
Let me show you what that means.
This means that when we run the game,
the time it takes from the first frame to the second one is added into the movement.
And now when we hit the D key, there we go.
We are much slower.
And if we had the W, we move up, SVMs down, a,
we move left, move right,
and so on and so forth.
How cool is that?
What we have in other plural,
who we are extremely low.
This is no way of playing a game,
especially when we have scary enemies following us.
So this is why we had this movement speed variable all the time.
This will allow us to control our speed.
So now we will also multiply this by the movement speed.
We will save that.
So back in our gain,
we can see that the movement speed is 20.
Let's set it to five.
Save that, run the game.
And now we can move up,
down, left, right, however we want.
How cool is that we finally have a character,
a player that is moving in a world that we've created by using simple code.
Let's make this 50.
Let's look at it. Well, there we go.
We can even control how fast it goes.
I mean, seriously, how excited are you?
Maybe it's just me.
Let's keep it at five. I think five.
No fibers actually slow. Let's make then.
Yeah, there we go.
So 10 looks really, really nice.
And with that done,
I hope you enjoy it.
Before I introduce the next video,
you'll notice that we can actually still walk through
walls and walk away, which is not good.
That's why in the next video,
we will be applying physics to our player and making
him interact with various objects in our world.
With that said, four,
I go make sure to commit your changes.
I'm not going to do with every single video because
it's something that you have to become automatic.
And every time you do something important in your game,
make sure to stage all work,
commit all your work,
and I'll see you in the next video.