Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 36 ยท Input and Physics

Phaser Platformer Movement: Running, Jumping, Coyote Time

Build responsive platformer movement in Phaser: gravity, running, onFloor jumps, variable jump height, coyote time, jump buffering, one-way platforms and animation states.

The platformer is the genre where physics, input and animation meet. The whole game depends on one question: does the character feel good to control? Players cannot say why, but they notice instantly when jumps feel heavy, floaty or unfair. This lesson builds platformer movement step by step, from the basics to the tricks professional games use.

The ingredients

A platformer needs only a handful of settings:

  • Gravity (a strong one: 800 to 1600 pixels per second squared),
  • Static platforms as ground,
  • A collider between the player and the platforms,
  • Horizontal velocity set from the keyboard,
  • A jump: a sudden upward velocity, allowed only when standing on something.
JavaScript
// config: physics: { default: 'arcade', arcade: { gravity: { y: 1000 } } }
this.physics.add.collider(this.player, this.platforms);

update() {
  const body = this.player.body;
  if (this.cursors.left.isDown)       this.player.setVelocityX(-200);
  else if (this.cursors.right.isDown) this.player.setVelocityX(200);
  else                                this.player.setVelocityX(0);

  if (this.cursors.up.isDown && body.onFloor()) this.player.setVelocityY(-520);
}

body.onFloor() is true when the body is blocked from below, which is exactly "standing on the ground". The initial jump speed together with gravity decides the jump height: the apex height is vยฒ / (2g). With v = 520 and g = 1000, the player rises 520 x 520 / 2000 = 135 pixels. Work backwards when designing a level: if you want a 4-tile jump at 32 pixels per tile, you need about 128 pixels plus a margin.

A complete first platformer

The player is a rounded box with a face that flips when walking left. Platforms are static. Coins are collected by overlap. The scene reuses everything from the last lessons.

Run, jump and collect

Here the horizontal movement uses acceleration and a dragX instead of directly setting the velocity. That gives a little slide at start and stop, which feels more natural than an instant switch. If you prefer a snappy, arcade feel, set the velocity directly as in the first snippet.

Making the jump feel good

A bare onFloor jump works, but three classic tweaks make it feel professional. They are used in nearly every successful platformer.

1. Variable jump height

Players expect that tapping jump gives a small hop and holding it gives a big jump. Implement it by cutting the upward speed when the key is released early:

JavaScript
if (Phaser.Input.Keyboard.JustUp(this.jumpKey) && body.velocity.y < 0) {
  this.player.setVelocityY(body.velocity.y * 0.4);   // short hop
}

2. Coyote time

When the player runs off a ledge, they usually press jump a few frames late. A strict onFloor check rejects those presses and the game feels unfair ("I pressed it!"). Coyote time (named after the cartoon coyote who hangs in the air after running off a cliff) allows a jump for about 100 ms after leaving the ground.

JavaScript
if (body.onFloor()) this.lastGrounded = time;
const canJump = time - this.lastGrounded < 100;

3. Jump buffering

The opposite problem: the player presses jump a few frames before landing. Remember that press for about 100 ms and perform the jump as soon as the player touches the ground.

JavaScript
if (Phaser.Input.Keyboard.JustDown(this.jumpKey)) this.lastJumpPressed = time;
const wantsJump = time - this.lastJumpPressed < 100;
if (wantsJump && canJump) { jump(); this.lastJumpPressed = -1000; this.lastGrounded = -1000; }

Resetting both timers after the jump prevents a double jump from one press. Together these three tricks make the player's intentions work even when their timing is imperfect, which is the secret of "tight" controls.

Coyote time, jump buffer and variable height

Run to the end of the left ledge and press jump a split second after you leave it: you still jump (coyote time). Press jump just before landing: the jump happens as you touch down (the buffer). Tap for a hop, hold for a high jump.

Animation states

A character looks alive when its animation matches the state: idle, run, jump, fall. In update pick the state from the body, and call play(key, true) so the animation continues instead of restarting (see Spritesheets and Frame Animation):

JavaScript
if (!body.onFloor())          this.player.play(body.velocity.y < 0 ? 'jump' : 'fall', true);
else if (body.velocity.x !== 0) this.player.play('run', true);
else                          this.player.play('idle', true);

Flip with setFlipX(velocity < 0); do not make a mirrored copy of the art.

More platformer features

  • Double jump: count jumps; reset the count when onFloor(); allow a second press in the air.
  • Wall jump: check body.blocked.left or right while in the air and push off the wall.
  • One-way platforms: for the platform set body.checkCollision.down = false, left and right to false (see Collisions).
  • Moving platforms: an immovable dynamic body with setVelocityX, reversing at ends; the player follows with body.touching.down checks or friction.
  • Camera follow: this.cameras.main.startFollow(this.player, true, 0.1, 0.1) for scrolling levels; Cameras covers it in the next chapter.
  • Hazards and death: an overlap with spikes calls a respawn; falling below the world triggers it, as in the demo.

Tuning checklist

Feel Adjust
Floaty Increase gravity; reduce jump speed accordingly
Heavy Lower gravity or add a quick "hang time" at the top
Slippery Increase dragX; stop faster than you start
Stiff Use acceleration instead of direct velocity
Frustrating edges Add coyote time and jump buffering
Hard to land Make the body slightly narrower than the art

Change one number at a time, test by playing, and keep your tuning values in one object at the top of the file so you can adjust them quickly.

Common mistakes

  • Jumping with isDown and onFloor but no buffer. Players feel the controls ignore them.
  • Setting velocity every frame in update for the Y axis. You overwrite gravity; only set Y for jumps.
  • A body larger than the art. The player seems to hit walls early; shrink it and use an offset.
  • Pressing jump in the air. Always check ground contact (or coyote time).
  • Using touching.down after setting velocity manually. Use blocked.down or onFloor().

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which check tells you that the player is standing on the ground?

    onFloor() is true when the body is blocked from below (by a collider or the world bottom).
  2. 2. What is coyote time?

    Allowing a jump for about 100 ms after leaving the ground forgives presses that are a few frames late.
  3. 3. What does jump buffering do?

    A press shortly before landing is remembered and executed on landing, so early presses are not lost.
  4. 4. How is a variable jump height implemented?

    Multiplying a rising velocity by a fraction when the key is released makes short taps produce short hops.
  5. 5. A jump with speed v and gravity g reaches a height of about:

    From physics: height = vยฒ / (2g). Use it to design jumps that fit your level tiles.

Summary

  • Platformer basics are gravity, static platforms, a collider, horizontal velocity or acceleration and a jump gated by onFloor().
  • Jump height is vยฒ / 2g; design levels from it and keep tuning numbers in one place.
  • Variable jump height, coyote time and jump buffering make controls feel fair and responsive.
  • Choose animations from the body state with play(key, true) and mirror with setFlipX.
  • Add double jumps, wall jumps, one-way and moving platforms as separate small features.

Next: Tilemap Basics