Skip to main content

CharacterBody2D

Inherits: PhysicsBody2D < CollisionObject2D < Node2D < CanvasItem < Node < Object

Description

A 2D physics body specialized for characters moved by script. They are not affected by physics but affect other physics bodies in their path.

Properties

velocity
Vector2
default:"Vector2(0, 0)"
Current velocity vector in pixels per second, used and modified during calls to move_and_slide.
motion_mode
MotionMode
default:"MOTION_MODE_GROUNDED"
Sets the motion mode which defines the behavior of move_and_slide.
up_direction
Vector2
default:"Vector2(0, -1)"
Vector pointing upwards, used to determine what is a wall and what is a floor.
floor_max_angle
float
default:"0.785398"
Maximum angle (in radians) where a slope is still considered a floor (45 degrees by default).
floor_snap_length
float
default:"1.0"
Sets a snapping distance. When set to a value different from 0.0, the body is kept attached to slopes.

Methods

move_and_slide

bool move_and_slide()
Moves the body based on velocity. Returns true if the body collided.

is_on_floor

bool is_on_floor()
Returns true if the body collided with the floor on the last call of move_and_slide.

is_on_wall

bool is_on_wall()
Returns true if the body collided with a wall on the last call of move_and_slide.

is_on_ceiling

bool is_on_ceiling()
Returns true if the body collided with the ceiling on the last call of move_and_slide.

get_floor_normal

Vector2 get_floor_normal()
Returns the collision normal of the floor at the last collision point.

Example Usage

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

func _physics_process(delta):
    # Add gravity
    if not is_on_floor():
        velocity.y += ProjectSettings.get_setting("physics/2d/default_gravity") * delta
    
    # Handle jump
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY
    
    # Get input direction
    var direction = Input.get_axis("ui_left", "ui_right")
    if direction:
        velocity.x = direction * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
    
    move_and_slide()

Build docs developers (and LLMs) love