· 6 min read
Godot C# Platformer Jump: Coyote Time and Jump Buffering
How to make a Godot 4 platformer jump feel fair in C# with coyote time, a jump buffer and a variable jump height, in about 50 lines of code.
A platformer jump in Godot C# takes about ten lines to make work. It takes a few more to make it feel fair. The basic version only jumps when the character stands on the floor at the exact frame you press the button. Players do not press buttons at exact frames. They press a little late, after running off a ledge, or a little early, just before landing. The game ignores both presses, and the player feels cheated. Two small tricks fix this: coyote time and jump buffering. This post shows both in a Godot 4 CharacterBody2D written in C#.
This is the first mechanic for Debri, my hobby game in Godot. I introduced the project in Starting a Godot Game as a Mobile Developer. The plan there was clear: before anything else, the hero has to run and jump in a test room, and it has to feel right.
The naive jump and why it feels wrong
Here is the jump most tutorials start with:
if (Input.IsActionJustPressed("jump") && IsOnFloor())
{
velocity.Y = JumpVelocity;
}
It is correct, and it is strict. Two cases break it:
- Late press. The player runs off a ledge and presses jump one or two frames after the feet leave the ground.
IsOnFloor()is already false. No jump. The character falls. - Early press. The player is falling toward a platform and presses jump a moment before landing.
IsActionJustPressedis true only on that one frame, and the character is still in the air. When it lands a moment later, the press is gone. No jump.
In both cases the player did the right thing a fraction too early or too late. The game says no. From my side as an app developer, this is like a button that only reacts if you tap its exact center pixel. Technically it works. Nobody would call it good.
Coyote time: a short grace period after the ledge
Coyote time is named after the cartoon coyote who runs off a cliff and only falls when he looks down. The idea: after the character leaves the floor, it can still jump for a short time, as if it were still standing there.
The implementation is one timer. Every physics frame on the floor, reset it to the full grace time. Every frame in the air, count it down. Allow a jump while it is above zero.
if (IsOnFloor())
_coyoteTimer = CoyoteTime;
else
_coyoteTimer -= dt;
A tenth of a second is a common starting value. It is short enough that nobody notices the hero "jumping on air", and long enough to catch the late press.
Jump buffering: remember the press for a moment
Jump buffering solves the early press. When the player presses jump, the game remembers it for a short time. If the character lands while the memory is still fresh, it jumps right away.
It is the same pattern, a second timer:
if (Input.IsActionJustPressed("jump"))
_jumpBufferTimer = JumpBufferTime;
else
_jumpBufferTimer -= dt;
Now the jump condition is no longer "pressed now and on the floor". It is "pressed recently and on the floor recently". After a jump, clear both timers. Otherwise a single press can trigger two jumps.
The full movement script in C#
Here is the whole script for the test room. It adds one more piece: a variable jump height. If the player releases the button while still going up, the upward speed is cut. A tap gives a small hop, a long press gives a full jump.
using Godot;
public partial class HeroMovement : CharacterBody2D
{
[Export] public float RunSpeed = 180f;
[Export] public float JumpVelocity = -360f;
[Export] public float CoyoteTime = 0.1f;
[Export] public float JumpBufferTime = 0.12f;
[Export] public float JumpCutFactor = 0.5f;
private float _coyoteTimer;
private float _jumpBufferTimer;
public override void _PhysicsProcess(double delta)
{
var dt = (float)delta;
var velocity = Velocity;
// Gravity and coyote time
if (IsOnFloor())
{
_coyoteTimer = CoyoteTime;
}
else
{
_coyoteTimer -= dt;
velocity += GetGravity() * dt;
}
// Jump buffer
if (Input.IsActionJustPressed("jump"))
_jumpBufferTimer = JumpBufferTime;
else
_jumpBufferTimer -= dt;
// Jump: pressed recently AND on the floor recently
if (_jumpBufferTimer > 0f && _coyoteTimer > 0f)
{
velocity.Y = JumpVelocity;
_jumpBufferTimer = 0f;
_coyoteTimer = 0f;
}
// Variable jump height: release early, jump lower
if (Input.IsActionJustReleased("jump") && velocity.Y < 0f)
velocity.Y *= JumpCutFactor;
// Horizontal movement
var direction = Input.GetAxis("move_left", "move_right");
velocity.X = direction * RunSpeed;
Velocity = velocity;
MoveAndSlide();
}
}
A few notes for anyone coming from GDScript or from mobile code:
partialis required. Godot's C# source generators add code to the class, so a script class must bepartial.- Copy, change, assign.
Velocityis aVector2struct. You cannot setVelocity.Ydirectly. Copy it to a local, change the local, then assign it back beforeMoveAndSlide(). GetGravity()returns the gravity that applies to the body, including the project default and anyArea2Dthat overrides it. That will matter later for zones with different physics.- Input actions (
jump,move_left,move_right) are defined under Project Settings > Input Map, not hard-coded keys. A gamepad can be added later without touching the script. [Export]puts each value in the Inspector. Tuning becomes a matter of changing a number and pressing play, not editing code.
The CharacterBody2D reference in the Godot docs covers MoveAndSlide() and IsOnFloor() in detail.
Tuning: an AI agent can write it, but cannot feel it
I work on Debri with Claude through a Godot MCP server, so the agent can edit scenes and run the project itself. Writing this script is easy for it. Coyote time and jump buffering are well-known patterns, and the code above is small.
What the agent cannot do is hold a gamepad. In an app, a spec and a test can tell you a feature is done. In a platformer, "the jump works" and "the jump feels good" are two different checks, and only the second one matters to a player. So my split is simple: the agent writes the mechanics and exposes every number as an export. I play the test room and change the numbers.
The values in the script are starting points, not answers. They also have to serve the game, not a generic platformer. Debri's hero is a ship's doctor, not an acrobat, and the first design pillar is fear through vulnerability. So the target is a jump that is a bit heavy but always fair. The player should feel that the hero is not built for this planet, never that the controls lost a button press. Coyote time and the buffer are what make "heavy" and "fair" possible at the same time.
Takeaways
- A strict
IsOnFloor()jump is correct but feels unfair. Players press a few frames early or late. - Coyote time lets the character jump for a short moment after leaving a ledge. It is one timer.
- Jump buffering remembers an early press and jumps on landing. It is a second timer. Clear both after a jump.
- A variable jump height (cut the upward speed on release) gives the player control over small and big jumps.
- Export every tuning value. Let the AI agent write the code, and keep the feel for yourself.