GDSense how-to guide

CharacterBody2D Player Movement in Godot

Build and verify a small platform controller with input actions, gravity, jumping, and collision checks.

Published Updated

All documentation guides

The short answer

Use CharacterBody2D for a scripted player, set velocity in _physics_process(), and call move_and_slide() once per physics tick. Velocity is measured in pixels per second; do not multiply the velocity passed to move_and_slide by delta. Use delta when integrating acceleration such as gravity.

Build the scene and input actions

Create a CharacterBody2D named Player with a CollisionShape2D child and a visible Sprite2D or Polygon2D. Assign a shape that fits the character. Add a StaticBody2D floor with a CollisionShape2D and ensure the player collision mask includes the floor layer.

In Project Settings → Input Map, create move_left, move_right, and jump. Bind keyboard or controller inputs to each. Attach the script below to Player. Set this scene as the main scene or run it directly.

Godot 4.7 CharacterBody2D reference

Move and jump during physics ticks

This example uses a fixed downward gravity for a simple platformer. The floor direction defaults upward. is_on_floor() reflects the previous move_and_slide result, so jumping is allowed only after floor contact has been established.

res://scripts/player.gd
extends CharacterBody2D

@export var speed: float = 220.0
@export var gravity: float = 980.0
@export var jump_speed: float = 420.0

func _physics_process(delta: float) -> void:
    if not is_on_floor():
        velocity.y += gravity * delta
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = -jump_speed
    velocity.x = Input.get_axis("move_left", "move_right") * speed
    move_and_slide()

Diagnose common movement problems

If input does nothing, confirm action spelling and whether the game window has focus. If the player falls through the floor, inspect both collision shapes and the player mask against the floor layer. Enable Debug → Visible Collision Shapes to see the runtime shapes.

If movement is very slow or depends on physics frequency, check that velocity was not multiplied by delta before move_and_slide(). If jumping never works, inspect floor contact and up_direction; a wall contact is not a floor.

Godot 4.7 debugging and visible collision shapes

Verify the controller before extending it

Test idle, left/right travel, jumping, landing, and contact with a wall. Hold jump to confirm it does not repeatedly trigger on landing. Try a ledge and verify that a midair jump is refused. Choose explicit rules before adding double jumps, coyote time, acceleration, or slopes.

When asking GDSense for help, attach the player script, describe the floor scene and collision layers, and include @errors or @log only when relevant. Request one behavioral change at a time and repeat these checks after each edit.

Try this workflow inside Godot

Use GDSense to ask questions, attach the context you choose, and review proposed changes without leaving the editor.