Skip to main content

Platformer

Platformer2DBehavior and Platformer3DBehavior are kinematic character controllers with walk, run, jump (buffer, coyote time, multi-jump, jump-cut), gravity, and slope/autostep settings. The WASM runtime owns the character controller; your game writes $platformer2D or $platformer3D input each frame and reads state through the handle.

Minimal example (2D)​

See website/snippets/behaviors/catalog/platformer.ts:

import { Platformer2DBehavior } from '@zylem/game-lib/behavior';

const platformer = player.use(Platformer2DBehavior, {
walkSpeed: 8,
runSpeed: 14,
jumpForce: 12,
maxJumps: 2,
});

player.onUpdate(({ inputs }) => {
player.$platformer2D.moveX = inputs.p1.axes.Horizontal.value;
player.$platformer2D.jump = inputs.p1.buttons.A.held > 0;
player.$platformer2D.run = inputs.p1.shoulders.LTrigger.held > 0;
});

Options (high level)​

OptionDefault (2D)Notes
walkSpeed / runSpeed8 / 14Horizontal speed on ground.
jumpForce, maxJumps12 / 1Multi-jump when maxJumps > 1.
gravity, coyoteTime, jumpBufferTime20 / 0.1s / 0.1sFeel tuning.
jumpCutMultiplier0.5Short-hop when releasing jump.
autostep, snapToGroundDistancesmall step / 0.2SNES-style step-ups and downhill glue.
useAttachedCollidertrueKCC shape from Rapier collider when true.

3D adds moveZ on $platformer3D and uses X/Z locomotion with the same jump feature set.

Handle​

  • getState() — FSM state (idle, walking, running, jumping, falling, landing).
  • isGrounded(), getJumpCount().

Pitfalls​

  • Vertical motion for 2D platformer uses behavior-owned verticalVelocity; do not fight it with manual Y transforms.
  • Attach a suitable capsule/box collider before spawn; optional shape override requires useAttachedCollider: false.
  • For jump-only entities without full platformer locomotion, consider Jumper instead.

API reference​