Skip to content

Camera3DControls

A first-person camera controller built as a Behavior. It creates a PerspectiveCamera, attaches the audio listener, and handles WASD movement with mouse-look rotation.

Usage

ts
import { Actor, Camera3DControls } from "@jolly-pixel/engine";

const actor = new Actor(world, { name: "Camera" });
actor.addComponent(Camera3DControls, {
  speed: 15,
  rotationSpeed: 0.003,
  bindings: {
    forward: "KeyW",
    backward: "KeyS",
    left: "KeyA",
    right: "KeyD",
    up: "Space",
    down: "ShiftLeft",
    lookAround: "middle"
  }
});

Options

ts
interface Camera3DControlsOptions {
  speed?: number;
  rotationSpeed?: number;
  maxRollUp?: number;
  maxRollDown?: number;
  bindings?: {
    forward?: InputKeyboardAction;
    backward?: InputKeyboardAction;
    left?: InputKeyboardAction;
    right?: InputKeyboardAction;
    up?: InputKeyboardAction;
    down?: InputKeyboardAction;
    lookAround?: MouseEventButton;
  };
}
OptionDefaultDescription
speed20Movement speed
rotationSpeed0.004Mouse look sensitivity
maxRollUpπ / 2Maximum upward pitch (radians)
maxRollDown-π / 2Maximum downward pitch (radians)
bindingsWASD + Space/Shift + middle mouseKey and mouse bindings

Runtime properties

ts
interface Camera3DControls {
  // The underlying Three.js camera
  camera: THREE.PerspectiveCamera;

  // Change movement speed at runtime
  set speed(value: number);

  // Change rotation speed at runtime
  set rollSpeed(value: number);
}

See also