Platformer Movement with Input
With a CharacterBody2D that falls and lands (previous lesson), a platformer controller needs three more things: horizontal movement from input, a jump, and the small "forgiveness" tricks that make jumping feel fair. This lesson writes all three and runs them against simulated input.
Actions, not keys
Godot code should ask about actions ("jump", "move_left"), not keys. The Input Map maps each action to any number of keys, gamepad buttons and axes, so one line of code supports keyboard, gamepad and rebinding (details in The Input Map & Input Events):
- Open Project > Project Settings > Input Map.
- Type
move_leftin the Add New Action field and press Add. Do the same formove_rightandjump. - Click the + next to each action and press the key (A / Left, D / Right, Space), then add a gamepad button or the left stick axis the same way.
The functions you use with actions:
| Call | True / returns |
|---|---|
Input.is_action_pressed("jump") |
while the action is held |
Input.is_action_just_pressed("jump") |
only on the frame the press happened |
Input.is_action_just_released("jump") |
only on the frame it was released |
Input.get_axis("move_left", "move_right") |
-1 … 1 (partial values from analog sticks) |
Input.get_vector("left", "right", "up", "down") |
a Vector2 with length ≤ 1 for top-down movement |
There is no keyboard in the headless test runner, so the examples press actions from code with Input.action_press() and Input.action_release(), following a small timeline. That's also a handy trick for automated tests and replays. A press made during one physics frame is reported as "just pressed" on the next physics frame, as a real key event between two frames would be.
Run and jump
move_toward(current, target, step) changes the speed by at most step, which gives acceleration and deceleration in one line. The jump sets velocity.y to a negative number (up), but only on the floor:
extends Node2D
# frames: 50
const SPEED := 150.0
const ACCEL := 900.0
const JUMP_VELOCITY := -300.0
const GRAVITY := 980.0
var player: CharacterBody2D
# A scripted "player": which actions are held on which physics frames.
var script_input := {
"move_right": [1, 30], # held from frame 1 to frame 30
"jump": [10, 12],
}
func make_box(size: Vector2) -> CollisionShape2D:
var shape := RectangleShape2D.new()
shape.size = size
var cs := CollisionShape2D.new()
cs.shape = shape
return cs
func _ready():
for action in ["move_left", "move_right", "jump"]:
InputMap.add_action(action)
var ground := StaticBody2D.new()
ground.add_child(make_box(Vector2(2000, 20)))
ground.position = Vector2(0, 18) # top at y = 8
add_child(ground)
player = CharacterBody2D.new()
player.add_child(make_box(Vector2(16, 16)))
player.position = Vector2(0, -0.08) # standing on the ground
add_child(player)
func feed_input(frame: int):
for action in script_input:
var span: Array = script_input[action]
if frame == span[0]:
Input.action_press(action)
elif frame == span[1] + 1:
Input.action_release(action)
func _physics_process(delta):
var frame := Engine.get_physics_frames()
feed_input(frame)
# --- the actual player controller ---
if not player.is_on_floor():
player.velocity.y += GRAVITY * delta
if Input.is_action_just_pressed("jump") and player.is_on_floor():
player.velocity.y = JUMP_VELOCITY
print("frame ", frame, ": jump!")
var dir := Input.get_axis("move_left", "move_right")
player.velocity.x = move_toward(player.velocity.x, dir * SPEED, ACCEL * delta)
player.move_and_slide()
# --- end of controller ---
if frame % 5 == 0:
print("frame ", frame, ": x = ", snappedf(player.position.x, 0.1), ", y = ", snappedf(player.position.y, 0.1),
", vx = ", snappedf(player.velocity.x, 0.1), ", floor = ", player.is_on_floor())frame 5: x = 3.8, y = -0.1, vx = 75.0, floor = true
frame 10: x = 13.8, y = -0.1, vx = 150.0, floor = true
frame 11: jump!
frame 15: x = 26.3, y = -22.4, vx = 150.0, floor = false
frame 20: x = 38.8, y = -37.8, vx = 150.0, floor = false
frame 25: x = 51.3, y = -46.5, vx = 150.0, floor = false
frame 30: x = 63.8, y = -48.4, vx = 150.0, floor = false
frame 35: x = 72.5, y = -43.4, vx = 75.0, floor = false
frame 40: x = 75.0, y = -31.7, vx = 0.0, floor = false
frame 45: x = 75.0, y = -13.1, vx = 0.0, floor = false
frame 50: x = 75.0, y = 0.0, vx = 0.0, floor = true
The body reaches full speed after 10 frames (150 / 900 = 1/6 s), jumps about 48 px high, and after move_right is released at frame 30 it slows to a stop in the air, because the same acceleration is used everywhere. Many games use a lower ACCEL in the air for more "weight", and a higher deceleration than acceleration so stopping feels snappy.
Tip: Tune jumps by height and time instead of raw numbers. For a jump of height h that peaks after t seconds,
gravity = 2h / t²andjump_velocity = -2h / t. The canvas-games platformer derives the same formulas.
Coyote time and jump buffering
Two tiny timers make a platformer feel fair. Players press jump a few frames after running off a ledge (they think they're still on it), or a few frames before landing. Without help, both presses are ignored:
- Coyote time: after leaving the floor, jumping is still allowed for a short time (about 0.1 s).
- Jump buffer: a jump press is remembered for a short time and used when the body lands.
Here the player runs off a ledge at frame 10 and presses jump four frames later:
extends Node2D
# frames: 40
const SPEED := 120.0
const JUMP_VELOCITY := -300.0
const GRAVITY := 980.0
const COYOTE_TIME := 0.1 # seconds you may still jump after leaving a ledge
const JUMP_BUFFER := 0.1 # seconds a jump press is remembered before landing
var player: CharacterBody2D
var coyote := 0.0
var buffer := 0.0
var was_on_floor := true
func make_box(size: Vector2) -> CollisionShape2D:
var shape := RectangleShape2D.new()
shape.size = size
var cs := CollisionShape2D.new()
cs.shape = shape
return cs
func _ready():
InputMap.add_action("jump")
var ledge := StaticBody2D.new()
ledge.add_child(make_box(Vector2(100, 20)))
ledge.position = Vector2(-40, 18) # top at y = 8, right edge at x = 10
add_child(ledge)
player = CharacterBody2D.new()
player.add_child(make_box(Vector2(16, 16)))
player.position = Vector2(0, -0.08)
add_child(player)
func _physics_process(delta):
var frame := Engine.get_physics_frames()
if frame == 13: Input.action_press("jump") # the player presses jump late...
if frame == 15: Input.action_release("jump")
if player.is_on_floor():
coyote = COYOTE_TIME
else:
coyote = maxf(coyote - delta, 0.0)
player.velocity.y += GRAVITY * delta
if Input.is_action_just_pressed("jump"):
buffer = JUMP_BUFFER
else:
buffer = maxf(buffer - delta, 0.0)
if buffer > 0.0 and coyote > 0.0:
player.velocity.y = JUMP_VELOCITY
print("frame ", frame, ": jump with ", snappedf(coyote, 0.001), " s of coyote time left")
buffer = 0.0
coyote = 0.0
player.velocity.x = SPEED
player.move_and_slide()
if was_on_floor and not player.is_on_floor():
print("frame ", frame, ": left the ledge at x = ", snappedf(player.position.x, 0.1))
was_on_floor = player.is_on_floor()
if frame % 10 == 0:
print("frame ", frame, ": y = ", snappedf(player.position.y, 0.1))frame 10: left the ledge at x = 20.0
frame 10: y = -0.1
frame 14: jump with 0.033 s of coyote time left
frame 20: y = -27.7
frame 30: y = -46.4
frame 40: y = -37.9
Change the press to frame 19 and run it in your own project: the coyote time has run out, the jump is ignored and the player falls. Both timers count down with delta, so they mean the same at any physics tick rate.
The controller in a real project
In your game the input comes from the Input Map and the script sits on the player itself, so it reads its own velocity and calls its own move_and_slide():
extends CharacterBody2D
@export var speed := 150.0
@export var accel := 900.0
@export var jump_velocity := -300.0
@export var coyote_time := 0.1
@export var jump_buffer := 0.1
var _coyote := 0.0
var _buffer := 0.0
func _physics_process(delta: float) -> void:
if is_on_floor():
_coyote = coyote_time
else:
_coyote = maxf(_coyote - delta, 0.0)
velocity += get_gravity() * delta
_buffer = jump_buffer if Input.is_action_just_pressed("jump") else maxf(_buffer - delta, 0.0)
if _buffer > 0.0 and _coyote > 0.0:
velocity.y = jump_velocity
_buffer = 0.0
_coyote = 0.0
# Variable jump height: releasing early cuts the rise short.
if Input.is_action_just_released("jump") and velocity.y < 0.0:
velocity.y *= 0.5
var dir := Input.get_axis("move_left", "move_right")
velocity.x = move_toward(velocity.x, dir * speed, accel * delta)
move_and_slide()@export makes the numbers editable in the Inspector while the game runs, which is how jump feel is really tuned (see @export & @onready).
Test yourself
Why should movement code call Input.get_axis("move_left", "move_right") instead of checking the A and D keys?
- It is faster
- Actions are mapped in the Input Map, so the same code works with keyboard, gamepad and rebinding
- Key checks don't work in Godot 4
get_axisalso applies gravity
What does coyote time do?
- Makes the player fall slower
- Lets the player still jump for a short time after walking off a ledge
- Remembers a jump press made just before landing
- Limits the jump height
velocity.x = move_toward(velocity.x, 150.0, 900.0 * delta) runs every physics frame at 60 FPS, starting from 0. How many frames until full speed?
- 1
- 6
- 10
- 150