Godot 4 (GDScript)
player_coyote_jump.gdextends CharacterBody2D
@export var jump_velocity: float = -420.0
@export var gravity: float = 1280.0
@export var coyote_time: float = 0.11 # 110ms post-ledge grace
@export var jump_buffer_time: float = 0.12 # 120ms pre-landing queue
var _coyote_timer: float = 0.0
var _jump_buffer_timer: float = 0.0
func _physics_process(delta: float) -> void:
if is_on_floor():
_coyote_timer = coyote_time
else:
_coyote_timer = maxf(0.0, _coyote_timer - delta)
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump"):
_jump_buffer_timer = jump_buffer_time
else:
_jump_buffer_timer = maxf(0.0, _jump_buffer_timer - delta)
# Fire jump when BOTH a valid floor/coyote grace and buffered press overlap
if _jump_buffer_timer > 0.0 and _coyote_timer > 0.0:
velocity.y = jump_velocity
_coyote_timer = 0.0
_jump_buffer_timer = 0.0
# Variable jump height cut when player releases early
if Input.is_action_just_released("jump") and velocity.y < 0.0:
velocity.y *= 0.45
move_and_slide()