Our Script will control the player.
Our Script will control the player.
Click Player and create a new script.
Click Player and create a new script.
Save it as Player.gd
Save it as Player.gd
By default, Godot creates code to move a player
By default, Godot creates code to move a player
for scripts that inherit from CharacterBody2D.
for scripts that inherit from CharacterBody2D.
For this lesson, we are going to delete everything
For this lesson, we are going to delete everything
except line 1:
except line 1:
Inside the script, we will create a function called _physics_process.
Inside the script, we will create a function called _physics_process.
This function gets called at a consistent rate per second.
This function gets called at a consistent rate per second.
Now we don’t need to use the delta time
Now we don’t need to use the delta time
value for moving the player.
value for moving the player.
In the function, we will detect keyboard inputs
In the function, we will detect keyboard inputs
and modify our velocity based on that.
and modify our velocity based on that.
To do this, we will start by setting
To do this, we will start by setting
the velocity to zero on both axes.
the velocity to zero on both axes.
Use If statements to change velocity for each different key press
Use If statements to change velocity for each different key press
Call the move_and_slide() function for physics calculations behind the scenes.
Call the move_and_slide() function for physics calculations behind the scenes.
Player Speed
Player Speed
If you test now the player moves SO SLOWLY you would think the code is not working!
If you test now the player moves SO SLOWLY you would think the code is not working!
Multiply velocity by 300 before calling move_and_slide()
Multiply velocity by 300 before calling move_and_slide()
Test again.
Test again.
A better way to control speed is to use a variable.
A better way to control speed is to use a variable.
Create a speed variable at the top of the script.
Create a speed variable at the top of the script.
Call it moveSpeed.
Call it moveSpeed.
Then multiply velocity by moveSpeed.
Then multiply velocity by moveSpeed.