Welcome back, my fellow game developers.
This is a very quick and short video.
This is just a small tweak that we want to make.
And the way our player moves around,
we don't have major cold,
we don't have major principles.
We do have a little lesson on mathematics,
which is acknowledged the most
exciting part in game development.
Yay, that's what you came here for math, right?
Anyways, let's not waste any more time.
And you can see that
the velocity when we move diagonal is the same
as when we are moving up or down or left and right.
Anyways, let's not waste
any more time and let's get started.
Oh, okay.
So let's see what's happening in here.
As I've told you,
whenever we are moving left and right,
we are moving at a certain velocity.
But when we are moving left or right and we're moving up,
we are moving at a much faster speed.
So if you're not convinced,
let's test this out.
Maybe I am lying.
I'm going to go down here.
I'm going to write debug dot log.
And in here I'm going to say walking
with a speed of and I'm
going to go ahead and add the layer rigid body velocity.
Let's save that and let's see what we have.
Now this might not work.
If I click on the Console,
you'll notice that we have 00.
So now if I move it,
you wouldn't see it stand.
When I move away,
it's minus 10 when I move up and to the right,
you can see that it's 10 by 10,
and that's the movement speed that we have right here.
But we don't want to see
how the vector of the velocity is.
We want to see the magnitude.
So I'm going to add something called the magnitude.
If you don't know what the magnitude is,
I'm going to explain it in just a bit.
So again, we run the game.
Now we have the magnitude 0.
If I walk to the right,
we have done to the left, we have ten.
If I move up, it's still 10.
You can see it increasing in here.
If I move down, it's still 10.
But now if I move to the right,
end up, we can see that it's 14.
So Michael, your instructor,
does not lie. Never.
You can see that we are moving
faster when we are moving diagonally,
up or down by pressing two keys.
Why is that happening?
Well, it comes back.
Simple mathematics.
You can see that we have
the velocity on the y and the velocity on the x.
That's what we were seeing before we added the magnitude,
which is 10 by 10.
So what happens here is we're adding both
of these vectors as we are moving diagonally.
And we have the final player velocity,
which is faster than those two.
Now, if you want,
you can keep this in
your game if it's part of your gameplay.
But usually we don't want the player to
move faster when he's moving in a diagonal.
This is not something logical.
So what we'll do is we'll normalize.
We do by normalizing is recreate this arc.
And this means this becomes a circle,
which means that the velocity on
the x plus the velocity on
the y is the same as
the velocity when we are moving in a diagonal.
So this is a normalized vector,
or when we say normalizing a vector.
So let's go ahead and do just that.
So back in our code,
let's go to Visual Studio.
And now let's see where do we
have our velocity being added?
It's right here.
So I'm going to add a,
another line or I'm sorry,
before we add this to the velocity,
I'm going to take
the movement input and I'm going to normalize it.
Save that.
So now when we go back into our game,
and by the way, the whole process here,
you know that this one is just taking a value.
Movement speed doesn't change.
So the only variable that's
changing is the movement input.
That's why I normalize the movement input.
Hence, the velocity stays the same.
So just if you were confused,
why we're changing the movement input, not the velocity.
So we go back in here,
we run our game.
And now you can see we walked to the right.
It Stan, to the left is still 10,
up, Stan down, it's 10.
When we move in a diagonal.
There we go.
You can see that it is still done.
How cool is that?
It's amazing.
On a final note, maybe you want to keep it.
Maybe you want to make the player
faster as you move diagonally.
That's not a problem.
You can do whatever you want.
This is your game,
add your own flavor to it.
But this is a battle game development habit
that you should have as
you progress through your journey.
Well, that said this was a quick and short video.
Make sure to commit your changes.
I forgot to commit them last time, but no worries.
I can keep this player controller as we've only
changed the debug dot log in here.
And the normalized, I can stage these,
commit these changes and then commit the other ones.
So, well that said,
I hope you enjoy it.
And I believe this is the end of
section 2 where we are going to be setting up our player.
In the next section,
we are actually going to start using
our gun to shooting by adding shooting mechanics.
So I'll see you then.