# CharacterBody3D Slopes, Snap & Coyote Jump in Godot 4

> Crisp Godot 4 CharacterBody3D locomotion combining floor_snap_length, apex hang gravity, jump-cut, coyote time, and zero-arg move_and_slide.

- **Canonical page:** https://gamecrew.ai/techniques/godot/characterbody3d-platformer-controller
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Godot 4
- **Category:** physics
- **Tags:** `characterbody3d`, `platformer`, `physics`, `slopes`, `jump`, `coyote-time`
- **Verification:** Compile-verified pattern

## When to Use & Field Best Practices

Default gravity integration with `CharacterBody3D` feels floaty at jump peaks and brittle when running fast over down-ramps or hitting jump two frames before landing:

1. **Configure Slope & Snap Properties Once in `_ready()`**: Set `floor_max_angle = deg_to_rad(45.0)`, `floor_snap_length = 0.45`, and `floor_constant_speed = true`, then call parameterless `move_and_slide()`.
2. **Coyote Time + Jump Buffer + Apex Hang**: Retain jump eligibility for ~`0.11 s` after stepping off ledges, buffer early `jump` presses ~`0.12 s` before touchdown, and cut upward gravity slightly near vertical speed zero for readable mid-air control.
3. **Variable Jump-Cut**: If the player releases `jump` while still rising steeply, multiply upward `velocity.y` softly so short taps clear low gaps and held jumps reach high ledges cleanly.

## Mathematical & Algorithmic Core

- **Initial Impulse from Target Jump Height $H$**:
  $$v_{y,0} = \sqrt{2 g H}$$
- **Apex Gravity Scaling**: When $|v_y| < v_{\text{apex}}$, scale gravity by $0.58$ to extend hang time at jump apex without lengthening total fall duration.

## Engine Implementation

### Platformer `CharacterBody3D` Motor (`Assets/Game/Scripts/player/platformer_motor.gd`)

```gdscript
extends CharacterBody3D
## Responsive 3D platformer motor with slope snap, coyote time, jump buffer, and apex hang gravity.

const COYOTE_TIME := 0.11
const JUMP_BUFFER := 0.12
const APEX_SPEED := 2.2
const APEX_GRAV_MULT := 0.58

var view_yaw := 0.0
var _coyote_t := 0.0
var _jump_buf_t := 0.0
var _was_grounded := true
var _did_jump_cut := false


func _ready() -> void:
	floor_max_angle = deg_to_rad(45.0)
	floor_snap_length = 0.45
	floor_constant_speed = true
	floor_stop_on_slope = true


func _physics_process(delta: float) -> void:
	var gravity := Tuning.value("gravity", 24.0)
	var move_speed := Tuning.value("moveSpeed", 6.5)
	var accel := Tuning.value("acceleration", 40.0)
	var jump_height := Tuning.value("jumpHeight", 1.6)

	var move_in := GameInput.move()
	var dir := Vector3(move_in.x, 0.0, -move_in.y).rotated(Vector3.UP, view_yaw)
	var target_vel := dir * move_speed
	var flat := Vector3(velocity.x, 0.0, velocity.z).move_toward(target_vel, accel * delta)
	velocity.x = flat.x
	velocity.z = flat.z

	if dir.length_squared() > 0.01:
		rotation.y = lerp_angle(rotation.y, atan2(-dir.x, -dir.z), minf(1.0, 12.0 * delta))

	if is_on_floor():
		_coyote_t = COYOTE_TIME
		_did_jump_cut = false
		if velocity.y < 0.0:
			velocity.y = 0.0
	else:
		_coyote_t = maxf(0.0, _coyote_t - delta)
		var g_scale := APEX_GRAV_MULT if absf(velocity.y) < APEX_SPEED else 1.0
		velocity.y -= gravity * g_scale * delta

	if GameInput.pressed("jump"):
		_jump_buf_t = JUMP_BUFFER
	else:
		_jump_buf_t = maxf(0.0, _jump_buf_t - delta)

	if _jump_buf_t > 0.0 and _coyote_t > 0.0:
		velocity.y = sqrt(2.0 * gravity * jump_height)
		_jump_buf_t = 0.0
		_coyote_t = 0.0
		GameAudio.play("jump")
		Playtest.log_event("jump")

	if velocity.y > 1.5 and not GameInput.held("jump") and not _did_jump_cut:
		velocity.y *= 0.55
		_did_jump_cut = true

	move_and_slide()

	if is_on_floor() and not _was_grounded:
		GameAudio.play("land")
	_was_grounded = is_on_floor()
```

## WebGL & Physics Consistency

- **Avoid Godot 3 Signatures**: Always pass zero arguments to `move_and_slide()`; slope angle, up direction (`Vector3.UP`), and snap length live on properties of `CharacterBody3D`.
- **Keep Tuning Knobs Aligned with `level.json`**: Read `"gravity"`, `"moveSpeed"`, `"acceleration"`, and `"jumpHeight"` from `Tuning.value` so slider tweaks update jump height live.

## Verification Checklist

1. Run a play test jumping onto a raised platform both while moving uphill and tapping `jump` 3 frames before landing — confirm coyote and buffered jumps never drop.
2. Confirm `godot compile` and `godot lint` report 0 errors and 0 warnings.

## Official references

- Godot 4 CharacterBody3D Physics Notes
- Maddy Thorson (Celeste Movement & Coyote Time Feel)
