Player Movement in Godot: GDScript Guide
In this article
- Why GDScript for Movement?
- The Core Pattern: CharacterBody2D + Input Map
- Platformer Movement: Gravity and Jumping
- Handling Delta and Frame-Rate Independence
- 3D Movement: CharacterBody3D Basics
- Common Mistakes and How to Fix Them
- Choosing Between move_and_slide and move_and_collide
- Frequently Asked Questions
Player movement in Godot is implemented with GDScript by reading input actions and applying them to a node's position or velocity each frame. The standard approach uses the _physics_process(delta) callback with Input.get_vector() and a CharacterBody2D node, giving smooth, physics-based motion. This guide covers the core patterns, common pitfalls, and how to adapt them for top-down, platformer, and 3D games.
Why GDScript for Movement?
GDScript is Godot's built-in language, designed specifically for the engine. It compiles to a fast internal bytecode, integrates directly with the scene tree and signals, and requires no external build tools. For movement, its tight coupling to _physics_process and node properties makes it the most straightforward choice. While C# and C++ are also supported, GDScript is the default and the best documented for beginners and rapid prototyping.
The Core Pattern: CharacterBody2D + Input Map
The recommended setup for 2D movement uses a CharacterBody2D node with a CollisionShape2D child. The script reads input from the Input Map (configured in Project Settings > Input Map) and applies it in _physics_process. Here is a minimal top-down movement script:
extends CharacterBody2D
@export var speed: float = 300.0
func _physics_process(delta):
var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
velocity = direction * speed
move_and_slide()This works because Input.get_vector() returns a normalized vector (length 1) when any key is pressed, and a zero vector when none are. Multiplying by speed gives the velocity, and move_and_slide() applies it while resolving collisions with the environment. The delta parameter is not needed here because move_and_slide() internally handles frame-rate independence.
Platformer Movement: Gravity and Jumping
For a platformer, you add gravity and a jump impulse. The same CharacterBody2D works, but you modify the velocity manually:
extends CharacterBody2D
@export var speed: float = 200.0
@export var jump_velocity: float = -400.0
var gravity: float = 980.0
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = jump_velocity
var direction = Input.get_axis("ui_left", "ui_right")
velocity.x = direction * speed
move_and_slide()Note the order: gravity is applied first, then jump, then horizontal movement. is_on_floor() is only valid after a move_and_slide() call, so the jump check uses the previous frame's floor state, which is standard practice.
Handling Delta and Frame-Rate Independence
All physics-related movement should happen in _physics_process, which is called at a fixed rate (default 60 Hz). Using delta (the time since the last frame) is essential for any manual velocity changes like gravity. Without it, movement would be faster on high-refresh-rate monitors. For simple velocity assignment, multiplication by delta is not needed because move_and_slide() integrates internally, but for acceleration, friction, or gravity, you must multiply by delta.
3D Movement: CharacterBody3D Basics
3D movement follows the same logic but uses CharacterBody3D. The input vector is mapped to the X and Z axes, and you typically add camera-relative rotation. A minimal first-person controller:
extends CharacterBody3D
@export var speed: float = 5.0
var gravity: float = 9.8
func _physics_process(delta):
if not is_on_floor():
velocity.y -= gravity * delta
var input = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input.x, 0, input.y)).normalized()
velocity.x = direction.x * speed
velocity.z = direction.z * speed
move_and_slide()This assumes the camera is a child of the player, so the basis already reflects the player's rotation. For a camera that rotates independently, you would use the camera's global basis instead.
Common Mistakes and How to Fix Them
- Movement is diagonal and faster: Always normalize the input vector.
Input.get_vector()does this automatically, but if you build a vector manually withInput.get_action_strength(), you must callnormalized(). - Player jitters or slides after stopping: This is usually due to not setting velocity to zero when there is no input. The examples above handle this because
directionbecomes zero, so velocity becomes zero. - Jump doesn't work sometimes:
is_on_floor()may be false if the floor is not aPhysicsBodyor if the collision layer is wrong. Check the collision mask on the player and the floor's layer. - Movement is frame-rate dependent: If you apply acceleration or friction, you must multiply by
delta. A common pattern is:velocity = velocity.move_toward(target_velocity, acceleration * delta). - Using
_processinstead of_physics_process: This can cause jitter because rendering frames vary. Always use_physics_processfor anything that affects the physics body.
Choosing Between move_and_slide and move_and_collide
move_and_slide() is the higher-level function that automatically slides along walls and updates the is_on_floor(), is_on_wall(), and is_on_ceiling() flags. It is ideal for most character controllers because it handles collision response for you. move_and_collide() returns a KinematicCollision2D object and stops at the first collision, giving you full control over the response. Use move_and_collide() for projectiles or when you need custom collision logic, and move_and_slide() for characters.
Frequently Asked Questions
How do I make the player face the movement direction?
For 2D, you can set rotation based on the velocity: if velocity.length() > 0: rotation = velocity.angle(). For 3D, use look_at() or lerp the rotation to the target direction. For top-down games, you might want to keep the sprite facing up and flip it horizontally instead.
Why is my player stuck in the floor?
This usually happens when the collision shape is too large or the spawn position overlaps a solid object. Check the collision layer and mask, and ensure the player's CollisionShape2D is not bigger than the visual sprite. Also verify that the floor's collision layer is included in the player's collision mask.
Can I use RigidBody2D for player movement?
Yes, but it requires applying forces or impulses rather than setting velocity directly. RigidBody2D is better for physics-driven objects like crates, but for a player character, CharacterBody2D gives more predictable control and is the recommended choice in the official documentation.
How do I add acceleration and friction?
Use velocity = velocity.move_toward(target_velocity, acceleration * delta) for acceleration, and apply friction when there is no input: velocity.x = move_toward(velocity.x, 0, friction * delta). This creates a smoother feel than instant speed changes.
What is the difference between Input.get_vector and Input.get_axis?
Input.get_axis(negative, positive) returns a single float between -1 and 1 for one axis (e.g., left/right). Input.get_vector(negative_x, positive_x, negative_y, positive_y) returns a 2D vector for both axes and normalizes it, preventing faster diagonal movement. Use get_vector for 2D movement and get_axis for single-axis controls like a platformer.
How do I make the player move relative to the camera in 3D?
Get the camera's global basis and use its forward and right vectors. For example: var forward = camera.global_transform.basis.z and var right = camera.global_transform.basis.x, then combine them with the input vector. This is common in third-person and first-person controllers.