Skip to main content

First person

FirstPersonBehavior combines WASM first-person locomotion with presentation sync to a FirstPersonPerspective. Write $fps for move, look, and sprint; optional viewmodel config parents a weapon mesh to the camera. Jumping in FPS templates often adds Jumper3DBehavior via FirstPersonShooterCoordinator.

Minimal example​

website/snippets/behaviors/catalog/first-person.ts:

import { createCamera, Perspectives } from '@zylem/game-lib/core';
import { FirstPersonBehavior } from '@zylem/game-lib/behavior';

const camera = createCamera({ perspective: Perspectives.FirstPerson });
const fps = camera.getPerspective();

player.use(FirstPersonBehavior, {
perspective: fps,
walkSpeed: 8,
runSpeed: 16,
lookSensitivity: 2,
});

player.onUpdate(({ inputs }) => {
player.$fps.moveX = inputs.p1.axes.Horizontal.value;
player.$fps.moveZ = inputs.p1.axes.Vertical.value;
player.$fps.lookX = inputs.p1.axes.SecondaryHorizontal.value;
player.$fps.lookY = inputs.p1.axes.SecondaryVertical.value;
player.$fps.sprint = inputs.p1.shoulders.LTrigger.held > 0;
});

Options​

OptionRole
walkSpeed, runSpeedGround speed caps.
lookSensitivityScales look axes written to $fps.
eyeHeightEye offset above entity origin.
perspectiveFirstPersonPerspective from the active camera.
viewmodelEntity, offset, optional cameraObject for jitter-free weapons.

Handle​

  • getState(), getYaw(), getPitch(), attachViewmodel(...).

Pitfalls​

  • Pass skipDebugOrbit on the camera when debug orbit would fight mouse-look.
  • Simulation runs in WASM; FirstPersonPresentation only syncs camera/viewmodel from runtime poses.
  • Pair with a capsule collider and grounded stage geometry.

API reference​