Line 1: This script is attached to a CharacterBody3D, which is a built-in node for 3D characters. It already has useful movement functions like move_and_slide.
Line 4: SPEED: how fast the player moves.
Line 5: JUMP_VELOCITY: how forcefully the player jumps upward.
Line 8: This function runs every frame during physics updates.
delta = the time since the last frame, so movement looks smooth even if frame rates change.
Line 10: If the player is not standing on the floor, apply gravity.
velocity is the player’s movement speed in 3D.
get_gravity() gives the world’s gravity force.
* delta makes gravity frame-rate independent.
So: the player falls if not on the ground.
Line 14: If the jump key (ui_accept, usually Space) is pressed and the player is on the ground, set the vertical velocity (y) to the jump speed.
This makes the character go up.
Line 19: Get input from arrow keys (or WASD if mapped).
Returns a 2D vector (x, y):
Left/Right → x
Forward/Back → y
Line 20: Converts that 2D input into a 3D direction.
Vector3(input_dir.x, 0, input_dir.y) turns the 2D input into 3D (ignores Y since we don’t move up/down).
transform.basis rotates it to match the player’s facing direction.
.normalized() makes sure it’s length = 1 (so diagonal movement isn’t faster).
Line 21: If the player is pressing a movement key:
Set velocity on x and z based on the direction * speed.
This moves the character forward/backward/sideways.
Line 24: If the player is not moving:
Gradually reduce velocity toward 0 using move_toward.
This makes the character slow down smoothly instead of stopping instantly.
Line 28: Moves the character using the current velocity.
Handles sliding along walls/floors automatically.
Updates position based on physics.