Give the courier controlled movement
Use input actions, a speed in pixels per second, and CharacterBody2D movement without a diagonal speed boost.
Godot 4.7.2 stable · standard edition · GDScript · 20 minutes estimated practiceContinue your working project from the previous lesson. Each download is the completed version of this lesson, for recovery and comparison. Start lesson one with checkpoint 01.
Continue from the room
Use your lesson-one working folder, or extract checkpoint 01 into a fresh working folder. Checkpoint 02 is the completed comparison for this lesson. Stop the running game before editing.
The courier already has a collision shape and floating motion mode. In this lesson it moves through an open room; the wall boundaries arrive in lesson three.
Replace the movement function
Open player.gd. Leave extends CharacterBody2D, const SPEED := 180.0 and _draw() in place. Replace the whole _physics_process function with the code below. Keep the indentation shown.
Input.get_vector combines the built-in directional actions and limits the vector’s length, so pressing two directions does not give a faster diagonal. velocity is in pixels per second. move_and_slide uses the physics step internally, so this velocity assignment does not multiply by delta.
- Save player.gd and press F5. Move with the arrow keys.
- Compare moving right with moving right and down together. Release all keys and check that movement stops.
- If the courier leaves the visible room in this checkpoint, stop and run again. The next lesson gives the room physical boundaries.
func _physics_process(_delta: float) -> void:
velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") * SPEED
move_and_slide()Make a measured speed experiment
With the game stopped, change SPEED from 180.0 to 120.0. Run it and compare the time needed to cross a similar distance. Restore 180.0 before moving to the next lesson.
The useful question is whether a particular speed makes the route comfortable. Record what you observed instead of assuming a larger number improves the game.
Check your work.
- Each arrow key moves in the expected direction.
- Diagonal movement feels comparable to straight movement.
- Releasing all direction keys stops the courier.
- Restoring SPEED to 180.0 restores the original setting.
If something went wrong.
- Replace the existing _physics_process function instead of adding a second function with the same name.
- Use all four action names exactly: ui_left, ui_right, ui_up and ui_down.
- Multiplying velocity by delta here makes movement much slower because move_and_slide already uses the physics step.
Ask about one small step.
I am using Godot 4.7.2 and the One Small Delivery movement checkpoint. My CharacterBody2D uses Input.get_vector and move_and_slide. Help me understand the relation between direction, velocity, speed and the physics step. Work from my current player.gd, suggest one small experiment, and give a check for straight, diagonal and released-key behavior.