Player Script
Player Script
Add a Script to the Player Node.
Add a Script to the Player Node.
Call it Player
Call it Player
Remove all generated code except the first line.
Remove all generated code except the first line.
Input Map
Input Map
Click on:
Click on:
Project --> Project Settings --> Input Map
Project --> Project Settings --> Input Map
Name your action move_forward
Name your action move_forward
Click on Add
Click on Add
Now click on the + symbol
Now click on the + symbol
Godot is listening for input.
Godot is listening for input.
Press the key you want to use to move the player forward (W)
Press the key you want to use to move the player forward (W)
W on the keyboard is now the forward key.
W on the keyboard is now the forward key.
Follow the same process for backwards (S), left (A), right (D) and jump (SPACE).
Follow the same process for backwards (S), left (A), right (D) and jump (SPACE).
Variables
Variables
Set up variables for move_speed, jump_force and gravity
Set up variables for move_speed, jump_force and gravity
We also need to position the camera as the player moves.
We also need to position the camera as the player moves.
Add a variable to track the angle the player is facing.
Add a variable to track the angle the player is facing.
To rotate the Player Model we need an instance of it stored in a variable.
To rotate the Player Model we need an instance of it stored in a variable.
We use the @onready annotation so that the variable is loaded when the scene is ready.
We use the @onready annotation so that the variable is loaded when the scene is ready.
We get the node named Model and store it in our MeshInstance3D variable
We get the node named Model and store it in our MeshInstance3D variable
Which we have named "model"
Which we have named "model"
Creating the Player Movement
Creating the Player Movement
To move the player we will use the _physics_process method instead of _process
To move the player we will use the _physics_process method instead of _process
_physics_process runs at a constant rate every second
_physics_process runs at a constant rate every second
whereas _physics runs every frame.
whereas _physics runs every frame.
Use built-in method get_vector to find out which direction player is moving based on their key presses.
Use built-in method get_vector to find out which direction player is moving based on their key presses.
Now store the Player's x, y and z direction in a new Vector3 variable:
Now store the Player's x, y and z direction in a new Vector3 variable:
Note that get_vector returns a 2-dimensional vector (x and y only).
Note that get_vector returns a 2-dimensional vector (x and y only).
The player only needs to move on the x and z axes
The player only needs to move on the x and z axes
So we set the y value to 0 and use the y value from get_vector for z
So we set the y value to 0 and use the y value from get_vector for z
Now set the built-in velocity variable to the value of our dir vector.
Now set the built-in velocity variable to the value of our dir vector.
Call the move_and_slide method to run the players movement methods.
Call the move_and_slide method to run the players movement methods.
Test
Test
We can move around with our keys but:
We can move around with our keys but:
- We are not moving based on move_speed and we cannot change it's value.
- Gravity has no effect on us
- We cannot jump