Give your player a brain and drive it around the field with the arrow keys.
โฑ About 45โ60 minutes_ready (once) and _physics_process (every frame)CharacterBody3D. Rename it Player. You'll notice a yellow
warning triangle next to it โ that's normal, it just means "I need a body shape", which we
add next.
MeshInstance3D โ give it a
New BoxMesh. This is the bit you'll see on screen.
CollisionShape3D โ in
the Inspector set its Shape to New BoxShape3D. This invisible
box is how the player physically bumps into the world. The warning triangle disappears.
1 so it
starts above the floor.
extends CharacterBody3D # How fast the player moves var speed = 5.0 func _physics_process(delta): # Start with no movement var direction = Vector3.ZERO # Check which arrow keys are held down if Input.is_action_pressed("ui_up"): direction.z -= 1 if Input.is_action_pressed("ui_down"): direction.z += 1 if Input.is_action_pressed("ui_left"): direction.x -= 1 if Input.is_action_pressed("ui_right"): direction.x += 1 # Turn that direction into actual speed, then move velocity = direction * speed move_and_slide()
The arrow keys already work in Godot out of the box โ they're called
ui_up, ui_down, ui_left and ui_right.
extends CharacterBody3DYesterday's scripts extended Node. This one belongs to a CharacterBody3D, which
unlocks special powers like velocity and move_and_slide() that a
plain node doesn't have.
var speed = 5.0A var (variable) is a labelled box that stores a value. Here the box is
called speed and holds the number 5. Because it's a variable, you can change
movement speed for the whole game by editing this one number โ that's exactly why
we use variables instead of scattering the number 5 everywhere.
func _physics_process(delta):_ready (from Day 1) runs once at the start.
_physics_process is different โ Godot runs it about 60 times every
second, over and over. That constant repeating is what lets movement feel smooth and
live. The delta in the brackets is "how long since the last run" โ we'll use it
properly tomorrow.
var direction = Vector3.ZEROA Vector3 is just three numbers bundled together โ an X, a Y and a Z โ
perfect for describing a direction in 3D. Vector3.ZERO means (0, 0, 0): no
movement at all. We start from "not moving" each frame, then add to it.
if blocksif means "only do the next bit if something is true".
Input.is_action_pressed("ui_up") asks "is the up arrow held down right now?".
If yes, we nudge the direction's Z by -1 (forward). Hold two keys and you move diagonally,
because both ifs run. This is how nearly all keyboard control works.
-Z and "towards you" (back) is +Z. It feels
odd at first; you'll get used to it fast.
velocity = direction * speed then move_and_slide()Velocity means speed-with-a-direction. We take the direction (which way) and
multiply by speed (how fast) to get the final velocity. Then move_and_slide() is
the CharacterBody3D's built-in command that actually moves the player and slides
nicely along walls instead of getting stuck. You set the velocity; Godot does the moving.
var speed = 5.0 to 10.0, then to 2.0. Run each
time. Feel how one number changes the whole game's pace.
print(velocity) line right after move_and_slide(). Watch the
Output panel as you move โ you'll see the numbers change live. That's a window into your
game's brain (and a debugging superpower).
if Input.is_action_pressed("ui_select"): speed = 12.0
else speed = 5.0 at the top of the function. (The spacebar is
ui_select.) Now you walk normally and sprint on the spacebar.
# in front: # move_and_slide().
Run it. The keys do nothing! That proves move_and_slide() is the line that actually
performs the movement โ everything above it only decides the movement. Remove the
# to fix it.
"Red error mentioning 'velocity' or 'move_and_slide'." The first line must be
extends CharacterBody3D (not Node). Those powers only exist on a
CharacterBody3D.
"It won't run โ error about indentation." The lines inside the
if blocks need two Tabs (one for the function, one for the if). Match the
spacing in the example exactly.
"The box moves but falls through / floats oddly." That's fine for today โ we have no gravity yet. Tomorrow fixes it. Just make sure Player's Position Y is 1.
"Arrows don't do anything." Make sure the script is attached to the Player node (a little scroll icon shows next to it) and that you pressed Play, not just saved.
Tomorrow: gravity and jumping โ your player learns to fall and leap. ๐ฆ