In this video we'll add code to the ship
to control it with the keyboard.
That way we'll learn about Godot's input mapping features
and how to listen to input in our code.
Head to the project menu in the top left,
then Project Settings and to the Input Map tab.
This tab lists the key mappings in your Godot project.
It allows you to map keys on the keyboard
mouse clicks, mouse buttons
or buttons on a gamepad
to a name you can use conveniently.
Now you have a bunch of them that come precreated
in every Godot project for user interfaces.
We also created a couple more in this project for the practices
and for a couple of features we have in this course.
But we're going to be recreating a couple for practice in there.
I invite you to scroll to the point you see the move_left input
and on the right we're going to be clicking this dust bin
to remove the move left input.
So click the input next to that to move right, move up, down and boost
because we'll be remaking them.
I'll scroll back to the top
and typically the way you would create a new input mapping
is by going to this action bar and you start by typing in the name.
For example, to move left we'll use something like Move and Scroll Left.
You can press Enter to add the input action and it will be appended
at the bottom of the list.
Then, to the right you have a little "add event" button.
If you click it you get a drop down menu
where you can choose the kind of key or button
that you want to map to this action.
Typically you'll want to use something like
Physical Key or Key for the keyboard.
Joy Button and Access are for gamepads
and Mouse Button for the mouse or touchscreens.
The difference between Physical Key and Key
is that people have different keyboard layouts around the world.
For example, in North America they use the WASD keys to move a character.
In France, we might use ZQSD because the layout is different.
Physical Key is going to store the position of the key on the keyboard
rather than the letter.
and that's what we'll be using in here.
Click Physical Key and then you can type A, for example,
to make the character move left.
Now we're going to create three more inputs like these
for the move_right, up, and down actions.
In the action bar type "move_right".
We're going to create them all at once.
So move_up and move_down.
For move_right, you click the plus button next to it.
Physical Key, press the D key in my case
click OK, move up, same thing. Physical Key.
I'm going to press W, click OK to create the action and then Move Down.
I'm going to add Physical Key.
Press S to move down and OK,
and with that we have the input actions and we can start using them in our code.
Can click the Close button to get back to the script editor.
We will now add a couple of lines of code
to calculate the desired move direction of the player
based on the key presses.
I invite you to put your cursor at the end of the process function
and press Enter to insert a couple of lines
because we need to add code at the top of the function.
We'll start by defining a variable named "direction"
that will be our ship's move direction
and we will initialize it with a value of Vector2.ZERO.
Now, typically a direction vector is a vector with a maximum length of one.
This allows us to represent only the move direction of the player
and we can multiply that by a speed
to give our ship a velocity.
That is to say, a direction and a speed together.
Let's calculate the direction and I'll explain as we go.
So we're going to start with the horizontal direction.
On a new line we type, direction.x =
and we're going to type, Input.get_axis
and then this is a function that is going to give us a value
between minus one and one depending
on the two input actions that we give it as arguments.
We first have to write the input
that makes the value go towards minus one.
This is going to be move_left because minus one represents
the left horizontal direction in the engine
and then we add a comma followed by move_right.
Note how you have to put the input actions defined
in the project settings in quotes there.
This is going to give us minus one if we press the A key
and one if we press the D key.
Then we're going to do something similar for the y-axis.
direction.y = and you can copy the Input.get_axis call
from the line above and press Control V to paste it there.
We just have to change the input action.
So we're going to turn move_left into move_up
because minus one on the vertical axis represents moving up.
We're going to change the second argument to, move_down.
Before we move on, a word on this input thing here.
It's what we call a "global object".
It's something you can access in any script
and you can call functions on it,
functions that are useful to make your game.
You can use it to calculate the mouse position,
to know if a key was pressed.
We'll see that moving forward in the course.
Now we're going to change how we move the ship.
We want to calculate our velocity
by multiplying the direction vector, we just got, by some speed value.
At the top of our script,
we're going to define a new variable named "max speed".
This will be the ship's maximum movement speed
and let's set it to 600 pixels per second.
Then we can change the default value of the velocity
because now the velocity will depend on our keyboard input.
We'll set it to Vector2.ZERO.
That is to say, if we don't press anything
at the start of the game, the ship will not move.
Down before we change the position, we're going to calculate the velocity.
Like so: we're going to write velocity is equal to direction
multiplied by maximum speed.
This is a fairly typical way of coding movement in games.
You will see why at the end of the series
when we add smooth movement to the ship.
You can try your game by pressing F6 on your keyboard.
Now, if you press WASD, you will see your ship can move.
It moves horizontally, diagonally
and the thrusters update when it's not moving.
There's one thing we need to fix though.
If you try to move horizontally and then diagonally, you should see that
when you go diagonally, the ship moves instantly faster.
Why is that?
Well, this is due to our direction calculation right now.
When we only go horizontally or vertically,
we might get a value of one on that axis.
But when we go diagonally, we get an x direction, say, of one
and a y direction of one,
and our vector ends up being longer.
So it's increasing our velocity.
But we want the ship to move at the same speed
regardless of its move direction.
To do that, we have to add some condition, like so.
After calculating the direction, we're going to add new lines.
We're going to say, "if direction.length",
length is a function that tells you the length of the vector.
If it's greater than one, so if we're moving diagonally, basically,
we're going to change the direction value.
So we say direction is equal to direction.normalized.
This is another function of the vector 2 type
that's going to limit the length of the vector to one.
With these two lines, the ship will not move faster when going diagonally.
These two lines also work great with a gamepad,
if you are working with a joystick and using analog input.
There's one small little bug that we want to address,
that if you move to the left and stop,
the ship goes back to looking to the right.
One thing we can do is only change the rotation in our code
when the ship is actually moving or when there's an input direction.
So we're going to add a line before it.
We're going to say, "if direction.length is greater than zero"
or we can use the shorthand, "if direction"
the computer will understand that we want a vector
with a length greater than zero.
We can select the line below and press the tab key
to increase its indent level
to run it only if the direction vector
has a length greater than zero.
In that case, we rotate the ship.
You can now try the game and move
and when you stop moving,
the ship preserves its current loop direction.
With that, in the next video,
we'll add a boosting mechanic to the ship.
Which will be a great opportunity to look at signals,
another very important feature in Godot.
See you there.