[MUSIC]
In this lecture, we're gonna make our player move up, down, left, right.
We'll be introducing the concept of Time.deltaTime, so
that we can make our game frame rate independent.
Okay, let's make our player ship move.
First thing, I'm going to do is click on my player and add a player script.
So I clicked on my hierarchy on the player, game object.
And I will now in the [INAUDIBLE] click on add component.
We will call this player, I've already got it typed in there.
And then click on new script, click Create and add.
That will give us a new script.
Over in my project folder, I will.
Do I have a script done?
I don't have a script folder yet.
Okay, right click Create Folder.
We'll call this script, there were go.
Drag the player script into the scripts folder.
Double click on that and open up player.
And that should open up Visual Studio for us, if I can click on it correctly.
Sometimes unity takes a little bit of time to think.
Need that half a second where its like hang on Rick.
Just slow down Rick, calm down, hold your horses, I'm doing some stuff.
Give it a minute, and then I need to click on it again.
Here we go,okay.
Now, I'm in player.cs, excellante.
Now, we're going to do something to make the player move.
And movement needs to be happening on Update.
Cuz we want it to be listening our input every frame.
Every single frame we want to have the ability to say, move up and left and
right and down.
So I will start by creating a method, Move ();.
That doesn't exist yet, so we're getting the red squiggly line.
Let's give ourselves a little space down below, and this will be a private void,
so there's no return type.
Call it Move.
Whoops, auto-complete thought I was trying to do something else.
Move, and then open and close our squiggly braces.
Our curly braces is probably the better term.
Now what do we need to do in here?
Well first of all, we want to create
this concept that there will be a new position that we're moving to.
So I'm gonna write in here var new nexXpos,
short for position.
We can write the whole thing,
position but pos is one of those things that we know it means position.
And instead of next I'm saying new.
There we go, lots of typos there, sorry, new x pos.
And you might say, well why is this a var?
I'll get to that in a moment.
Okay, equals, transform,
dot position .x so we're doing this on the x axis at the moment.
Just the x axis and we're saying where do we want to be on the x axis?
And we're saying it is where we currently are plus something.
And that plus something is going to be some sort of change so
we'll call this deltaX.
Delta referring to change or difference, or
distance between where we currently are and where we want to be, and semicolon.
Okay, so there's our first line.
It doesn't know what deltaX is, so we need to create that as well.
I'm gonna type in var again, again, I'll get back to var in just a moment.
Have a little chat about that.
deltaX, so the change equals, this is gonna be cool here.
We're gonna use Input, .GetAxis, and
we haven't used GetAxis before, and then we wanna know which particular axis.
So waht is the axis, and where do we get it from, and why are we using it?
Let's jump back over into Unity.
If you go up to Edit, Project Settings, and find Inpu,
you'll see we have the InputManager.
It might be closed up for you, you need to open up the Chevron thingy.
And you can see a number of different elements, a number of different aspects,
axes in our InputManager.
We have the Horizontal and we have the Vertical.
So in the past, in one of our previous sections,
in here we would have said Input.GetKeyDown, for example.
That's another, GetKeyDown,
it's another method that we can use within the Input class.
And GetKeyDown, we would say specifically what particular keys.
So it might space or we might say, a, or we might say, b.
I'm gonna undo that back to get axes.
Instead of doing that, we can just go and say let's go and use the horizontal axes.
The horizontal axes relates to the x coordinates.
And a negative is going to be negative x, and a positive is going to be positive x.
So basically, that's gonna be left and right,
because we know negative x heads to the left and positive x heads to the right.
And the negative button in this instance is left on the keyboard and
the positive is right.
Also there's an alternative negative button which is AND.
So if you wanna use WASD to control your game, or the cursor arrows,
the up, down, left, right, you can do both of those within here.
A couple of other settings,
we're not gonna play around with too much at the moment, gravity,
the dead zone, sensitivity, etc.
Sometimes a little bit more relevant for keyboard.
The cool thing is if you scroll down a little bit,
you'll see down at the bottom there's another horizontal.
Which is set up for the joystick but what I reallly wanted to show you, is we can
just used one aspect that we print our code, which we're gonna call horiztonal.
And then system knows that all of these different things can be
referring to horiztonal.
And if you want to change it,
then you can change it here in your in your inspector nice and easily.
Rather than having to go into your code and find your particular spot, where
we're referring to the left key or the right key or whichever key you're using.
So I'm gonna copy horizontal, back over into my player script.
in here, where we say input.getaccess open parenthesis and do our quotation.
And then paste in horizontal, or you can type it but
pasting it means we get it We know we get it correct for sure, and then semi colon.
So what we've created here is a var delta x, and
we have a squiggly line here at the moment.
I'm not happy, I'll get to that in a second.
Input dot GetAxis("Horizontal"), and the reason we got a red,
squiggly line, it's not happy that we're using it before we've declared it.
So we're gonna put this line,
I just wanted to do it this way around from a learning point of view.
So you could see this one first, that one second, but I will now put this up above.
So we're declaring what deltaX refers to, and
then we're using that deltaX in our newXPos.
So in other words, where we want our ship to be.
So I mentioned the var within our local variable here, we sometimes use var.
Or we could type in float, that would be just fine.
Because this is of type float, if I undo it.
If you mouse over delta x,
it shows you that it's a local variable with type float.
So in instances, in situations where Unity knows, that our Visual Studio knows that
this particular variable, because what follows it must make it a float.
Then it's okay for you to say var and it knows it's a float,
it converts it automatically.
So to move, you might know this already.
If you know this, pause the video and jump in and give this a try.
Otherwise, we're going to type in transform.position,
you guessed it, equals.
Well we need to use our new keyword to create a Vector2.
We're working in Vector2 in this game.
You could type Vector3, and then it would want to know X, Y and Z.
But we're not working with Z in this section of the course.
So we're just, Using Vector2.
And then what'sthat going to be?
Well, it's looking for an x and a y.
So float x, float y.
What's our float x?
Float x is going to be the new x position.
So this new x position.
And the y for now we're going to say is
transform.position.y; to round it off.
So we're saying that we want to get the change, this is the change
based upon what the player is gonna be doing that we're calling each frame.
And then that will allow us to say, what's the new position that we want the ship to
go to, which is the current position plus the change.
And then we need to say, well your position, your actual position,
this frame should be this new Vector2.
Which is going to be the x, which is our new position and then the y,
transform.position.y, is just saying stay where you are right now on the y.
Okay, now let's save that, come back over into Unity, click on Play.
Not on Pause, Rick, click on Play.
I'm having a bit of trouble with this today, aren't I?
Click on Play, okay, and then left and right, wow, fantastic.
No, up and down, just left and right.
This is going very fast, so what do we need to do next?
Well, next, what we need to do is something that,
I'm going to introduce a new concept for you here.
And I've got some documentation to show you.
It's going to be time dot delta time.
So very quickly I've also given you a link to input,
if you want to know more about input and all the different properties and
all the different methods that the input class has.
Also into the Input Manager you can read more about that, if you'd like.
We're looking at Time.deltaTime, and I've got a couple of slides on this.
But the summary of why use Time.deltaTime is to make our game
frame-rate independent.
That means that no matter how fast or
slow your computer runs, you will get the same experience.
So let's have a look at some slides on that.
So on Update, which we know is each frame, so every frame of our game.
We move the whatever, in this case our player ship, 1 unit to the left.
That's what we're saying in this particular example.
We're saying, On Update, move 1 unit to the left.
And on a slow computer, for example, this is just a guess.
We might be getting ten frames per second.
So the computer is chugging away saying [NOISE].
Each second it manages to calculate through ten frames worth of computer.
On a fast computer, it can calculate through a lot more frames.
It can render more frames.
Or it can process what we're trying to get it to do.
So, it might be getting 100 frames per second.
The problem varies if we saying on Update move 1 unit, then on the Slow Computer,
that's 1 unit x 10 Frames per second, the Distance per second will be 10 units.
And on the Fast Computer, it's gonna be 1 unit per frame to the left,
x 100 frames per second, which is 100 frames.
So you get a very different experinece on a slow computer than on a fast computer.
Now, what do we do about this?
Well, when we look at the duration of the frames on a slow computer,
if we're getting 10 frames per second, then each frame is 0.1s.
10 frames per second, that means each individual frame is .1 of a second.
And our example with the Fast Computer, if we're getting 100 frames per second,
then each frame is taking .01 second to run.
So every point every .01 of a second there's another frame, and
another frame, and another frame.
So the distance per second, if we were to multiply the 1 unit per frame
times by the 10 frames per second that our computer is capabale of running,
times by the duration of each frame, which is 0.1, we get a value of 1.
And that's exactly the same if you use the same calculation one frame to the left
per, so one unit per frame times by 100 frames per second,
times by 0.01 that it takes to run each frame, then we get a value of 1.
So you can see formula, gives us the same result on both computers.
So then we can use Time.deltaTime because using
Time.deltaTime Unity can tell us how long each frame took to execute.
We don't need to sit there with a stopwatch,
cuz we don't know how to do that.
It'd be impossible to measure the frames.
Unity knows exactly how long the last frame took to execute it.
Knows that it's keeping track of that so we can use that.
And so when we multiply something by Time.deltaTime it makes our game frame
rate independent.
So if I scroll back up here ,where the duration of the frame is
what we're getting by using time.delta time.
Time.delta time means how long did it take for that frame to execute?
Okay, and
so the end result is the game's behaving the same on fast and slow computers.
So back over into our game, let's go back to play.cs.
And to make this super clear, I'm just going to print, so debug.log,
Not quotation marks, thats if were doing a string, deltaX.
So we will just print this variable and see what it prints out for
us when we're moving.
So back over into Unity, you don't need to do this, this is just for
illustration purposes.
Play the game and then as I move to the left you can see the value.
I'm going to go off the screen.
If I hold down to the right, it goes all the way up to one.
Hold down to the left it goes all the way to minus one.
And there's a ramp up in between.
Cuz that's what happens, so when we jump back into our code here.
We're saying that as we're pushing this Horizontal, this value here,
this deltaX, we're getting the axis, it's going from minus 1 up to 1.
So we're gonna times this, this is where we want to be framerate independent.
We'll times this by Time.deltaTime.
Okay, that is making this framerate independent.
So I'm just going to get rid of this debug.log, don't need that anymore.
Save up, back over into Unity.
Now it's going to give us a very interesting result.
Not exactly what we want for our final game, but it's a good result.
So we'll move our ship to the left, [SOUND] very slow.
And to the right, very slow as well.
But this would be exactly the same on every computer.
This would be the exact same slowness of speed, which is great, that's a big win.
And the last step we need to do in here is to get the actual speed that we want.
We need to be able to tune it, cuz that's way too slow.
So I'm gonna create myself a new variable.
That I will call what SerializeField first of all, before I call it anything.
It's going to be a float and I'll call this moveSpeed and
we will initialize that at 10.
And it's going to be a float so we will be a f on the end of 10.
And now down here now in our delta so what is the change we say times by
Time.deltaTime times my moveSpeed, cuz we're wanting to speed it up.
Save, jump back over into Unity,
you can see if I click on my Player after it thinks, here we go.
Our move speed here is ten.
If I click on play we'll see how good that feels at the moment.
Might have got it right the first go.
Left, right, okay that's pretty good.
We can tune it a bunch when we've got more stuff in our game.
But for now that's feeling In the right ballpark.
And we can very easily increase this to 20 or decrease it to 5,
if we wanna double or half the speed of the player.
Okay, so that's us moving left and right.
We're using Time.deltaTime.
We're getting the axis, which is Horizontal.
And I have a challenge for you.
The challenge is to create vertical movement.
So allow the player to move up and down as well as left and right.
Basically, by replicating what we did just now for the horizontal axis.
Do the same thing for the vertical axis.
Okay, so jump in and take on that challenge and
I'll see you back here in a moment.
Okay, so we're gonna keep this really simple.
I'm just going to highlight my first two lines, copy, and
then paste them underneath the previous two lines.
And go through and change the reference to from X to Y,
and from our axis of Horizontal to Vertical.
And then for our new X position is actually our new Y position.
And then transform.position.y and deltaY.
And then the last step here in our transform.position,
the new Vector2 is newXPos, newY Pos, okay.
So let's give it a nice and simple, just we've got the same thing for
horizontal that we're now doing for the vertical.
Back over into Unity, click in play.
If all's gone to plan we can fly up, down, left,
right Okay, up down, left, right, cool.
So we've got movement in our game, the player can move around.
I can go off the screen, so
that's something we'll need to resolve pretty soon.
But we do have movement.
And this is framerate independent, which is very, very excellent.
It also means you can plug in a joystick, and you
could do exactly the same thing, without having to go and write additional code.
It just knows immediately, that horizontal and vertical maps to your joysticks or
your joypad.
So great work, well done and I'll see you in the next video.