TExcelTAD
Home › Godot 4 with GDScript › CharacterBody3D & 3D Physics

CharacterBody3D & 3D Physics

CharacterBody3D is the 3D twin of CharacterBody2D: a kinematic body that you move yourself by setting its velocity property and calling move_and_slide(). It collides, slides along walls, walks up slopes up to floor_max_angle and reports is_on_floor(), is_on_wall() and is_on_ceiling(). Gravity, jumping and acceleration are your code, which is exactly what players want from a character: precise, predictable control.

The other 3D bodies:

Node Moved by Use for
StaticBody3D nothing (or code, rarely) floors, walls, level geometry
AnimatableBody3D code or an AnimationPlayer; pushes others moving platforms, doors, elevators
RigidBody3D the physics engine (forces, impulses, gravity) crates, debris, balls, ragdoll parts
CharacterBody3D your code through velocity + move_and_slide() players, NPCs
Area3D nothing; detects overlaps triggers, pickups, damage zones, water

Every body needs at least one CollisionShape3D child holding a shape resource (CapsuleShape3D, BoxShape3D, SphereShape3D, ...).

Physics engines: Godot Physics and Jolt

Godot 4 ships two 3D physics engines:

  • Godot Physics, the engine's own implementation;
  • Jolt Physics, the open-source engine used in Horizon Forbidden West. It was a separate GDExtension at first, became built in with Godot 4.4, and Godot 4.6 made it the default for new 3D projects. Projects created earlier keep the engine they had.

The choice lives in Project Settings > Physics > 3D > Physics Engine (Jolt Physics or GodotPhysics3D; DEFAULT means Godot Physics). Jolt is generally faster and more stable with many stacked bodies. Its documented differences are mostly in joints (several soft-limit properties are unsupported), collision margins and a few project settings, so check the "Using Jolt Physics" page in the docs if you rely on joints. Your CharacterBody3D code does not change. 2D physics always uses Godot's own 2D engine.

A body falling onto a floor (verified)

Physics runs headless, so this example builds a floor and a capsule in code and moves the capsule diagonally while gravity pulls it down. (The test project has no physics setting, so this run uses Godot Physics; with Jolt the landing frame can differ slightly.)

gdscript · try it yourself
extends Node3D

# frames: 40
const SPEED := 5.0
var body: CharacterBody3D
var gravity := 9.8

func _ready():
	var floor := StaticBody3D.new()
	var fs := CollisionShape3D.new()
	var box := BoxShape3D.new()
	box.size = Vector3(50, 1, 50)
	fs.shape = box
	floor.add_child(fs)
	floor.position = Vector3(0, -0.5, 0)
	add_child(floor)
	body = CharacterBody3D.new()
	var cs := CollisionShape3D.new()
	var cap := CapsuleShape3D.new()
	cap.radius = 0.4
	cap.height = 1.8
	cs.shape = cap
	body.add_child(cs)
	body.position = Vector3(0, 2, 0)
	add_child(body)

func _physics_process(delta):
	if not body.is_on_floor():
		body.velocity.y -= gravity * delta
	var dir := Vector3(1, 0, -1).normalized()
	body.velocity.x = dir.x * SPEED
	body.velocity.z = dir.z * SPEED
	body.move_and_slide()
	var f := Engine.get_physics_frames()
	if f % 8 == 0:
		print("frame ", f, " pos ", body.position.snappedf(0.01), " on_floor=", body.is_on_floor())
frame 8 pos (0.47, 1.9, -0.47) on_floor=false
frame 16 pos (0.94, 1.63, -0.94) on_floor=false
frame 24 pos (1.41, 1.18, -1.41) on_floor=false
frame 32 pos (1.89, 0.9, -1.89) on_floor=true
frame 40 pos (2.36, 0.9, -2.36) on_floor=true

The capsule is 1.8 m tall, so its centre rests at y = 0.9 once it lands, and the horizontal movement continues at 5 m/s × 1/60 s per frame. move_and_slide() already multiplies velocity by the physics delta; never multiply velocity by delta yourself before calling it.

A third-person controller

This is the controller most 3D Godot games start from. The scene tree:

text
Player (CharacterBody3D)            player.gd
├── CollisionShape3D                CapsuleShape3D, height 1.8
├── Model (Node3D)                  the imported character, faces +Z
└── CameraPivot (Node3D)            at head height, yaw + pitch
    └── SpringArm3D                 spring_length = 4, pulls the camera in front of walls
        └── Camera3D
gdscript
extends CharacterBody3D

@export var speed := 6.0
@export var jump_velocity := 4.5
@export var mouse_sensitivity := 0.003
@export var turn_speed := 12.0

@onready var pivot: Node3D = $CameraPivot
@onready var model: Node3D = $Model

var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready() -> void:
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		pivot.rotate_y(-event.relative.x * mouse_sensitivity)
		pivot.rotation.x = clampf(pivot.rotation.x - event.relative.y * mouse_sensitivity, -1.2, 0.5)
	elif event.is_action_pressed("ui_cancel"):
		Input.mouse_mode = Input.MOUSE_MODE_VISIBLE

func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity.y -= gravity * delta
	elif Input.is_action_just_pressed("jump"):
		velocity.y = jump_velocity

	# Input relative to the camera's yaw, not the world
	var input := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
	var dir := (pivot.global_basis * Vector3(input.x, 0, input.y))
	dir.y = 0
	dir = dir.normalized() * input.length()

	velocity.x = move_toward(velocity.x, dir.x * speed, speed * 10 * delta)
	velocity.z = move_toward(velocity.z, dir.z * speed, speed * 10 * delta)
	move_and_slide()

	# Turn the model (not the body) toward the movement direction
	if dir.length() > 0.1:
		var target_yaw := atan2(dir.x, dir.z)        # model faces +Z
		model.rotation.y = lerp_angle(model.rotation.y, target_yaw, 1.0 - exp(-turn_speed * delta))

Notes:

  • move_left, move_right, move_forward, move_back and jump are actions you add in Project Settings > Input Map (The Input Map & Input Events). Input.get_vector returns a vector already clamped to length 1 with a dead zone, so diagonals are not faster.
  • The camera pivot rotates, the body does not. Rotating the whole CharacterBody3D with the mouse also rotates its collision capsule, which is harmless for a capsule but wrong for a box.
  • Movement is in _physics_process, mouse look in _unhandled_input (so UI clicks are not stolen).
  • atan2(dir.x, dir.z) gives the yaw that turns +Z toward dir, because imported models face +Z (Vector3.MODEL_FRONT). For a model that faces −Z, use atan2(-dir.x, -dir.z).
  • lerp_angle takes the short way around the circle; 1.0 - exp(-k * delta) makes the smoothing independent of the frame rate.

Useful CharacterBody3D settings

Property Default What it does
motion_mode GROUNDED FLOATING for space ships and swimmers: no floor/wall distinction
up_direction Vector3.UP what "floor" means; change for wall-walking or planets
floor_max_angle 45° steeper surfaces count as walls
floor_snap_length 0.1 keeps the body glued to the floor when walking down slopes and steps
floor_stop_on_slope true no sliding down slopes when standing still
platform_on_leave add velocity what happens to a moving platform's velocity when you jump off

After move_and_slide(), get_slide_collision_count() and get_slide_collision(i) tell you what you hit (the collider, the normal, the position), for example to push a RigidBody3D crate with apply_central_impulse.

Note: Physics queries such as raycasts work the same way as in 2D: a RayCast3D node, or get_world_3d().direct_space_state.intersect_ray(PhysicsRayQueryParameters3D.create(from, to)). See Areas, Collision Layers & Raycasts.

Test yourself

How do you move a CharacterBody3D?

  1. Apply forces and let the engine integrate them
  2. Set its velocity property and call move_and_slide() in _physics_process
  3. Set position every frame and the body resolves collisions
  4. Call apply_impulse once

Which statement about Jolt Physics in Godot is accurate?

  1. It is only available as a paid plugin
  2. It has been built in since 4.4 and is the default 3D engine for projects created with 4.6 or later
  3. It replaced 2D physics
  4. It requires C#

In the controller, why is the input vector multiplied by pivot.global_basis?

  1. To scale it by the camera's zoom
  2. So 'forward' means the direction the camera faces, not world −Z
  3. Because Input.get_vector returns degrees
  4. To add gravity