Skip to content

GameController ​

A connected game controller, handed to scene.gameController.on('connect', ...) and listed in scene.gameController.controllers.

Properties ​

aim read-only ​

  • Type: Pose | null
  • The pointing pose: a stylus tip, a spatial controller's aim. null while its position isn't tracked, on a gamepad, and where environment.features.has('accessoryTracking') is false.

buttons ​

  • Type: Object<string, any>
  • Current button state by name; a button the controller lacks is undefined. Gamepads: buttonA buttonB buttonX buttonY, leftShoulder rightShoulder, leftTrigger rightTrigger (value is analog), leftThumbstickButton rightThumbstickButton, buttonMenu buttonOptions. Spatial controllers: trigger, gripButton, thumbstickButton, buttonA (lower face button of that hand), buttonB (upper face button), buttonMenu. Styluses: tip (pressed against a surface; value is the pressure), primaryButton (the barrel button nearest the tip), secondaryButton (the one above it; value is how hard it is pressed). Entries update in place every frame.

grip read-only ​

  • Type: Pose | null
  • Where the hand holds a spatial controller. null on every stylus and gamepad, and while untracked.

hand read-only ​

  • Type: string | null
  • "left" or "right": the side a spatial controller is built for, or the hand holding a stylus right now. A spatial controller is listed and announced only once its side is known, so it's set from connect on, unless accessory tracking isn't allowed. null for a gamepad, for a stylus nobody holds, and wherever the device doesn't say. Treat null as unknown, never as "no hand".

id read-only ​

  • Type: string
  • Unique for this connection; a controller that reconnects gets a new id. Use it to tell controllers apart, never to recognise a device.

isConnected ​

  • Type: boolean
  • false once the controller disconnected; its buttons then read released and its sticks centred.

kind ​

  • Type: string
  • "gamepad" (two hands), "spatialController" (one hand, tracked) or "stylus" (tracked, held in either hand).

name ​

  • Type: string
  • The controller's name, for display and logging. Don't branch on it.

sticks ​

  • Type: Object<string, any>
  • Current stick state by name, each axis -1 to 1, up is positive: leftThumbstick rightThumbstick directionPad on a gamepad, thumbstick on a spatial controller. Entries update in place every frame.

Methods ​

off() ​

javascript
off(id: string): void

Remove a subscription made with on.

Parameters:

  • id (string) - Id returned from on.

Returns: void

on() ​

javascript
on(type: string, options?: Object | function, listener?: function): string | any

Subscribe to this controller's input. Subscriptions end when the scene changes, as scene.on ones do; subscribe again in the new scene.

Example:

javascript
controller.on('button', { name: 'buttonA', phase: 'began' }, function () {
    jump();
});

Parameters:

  • type (string) - "button" → { controller, name, phase, pressed, value, touched, aim, grip }, where phase is "began" (pressed), "ended" (released) or "changed" (value or touch changed while the press state stayed), and aim and grip are the controller's poses in the frame of the change, as Pose or null; "stick" → { controller, name, x, y }.
  • options (Object | function) (optional) - Optional filter: name (one button or stick), and for "button" phase.
  • listener (function) (optional) - Called at most once per element per frame, with the latest state. A press and release within one frame is not seen, and neither is the press of a button already held when the controller connected: it reports "ended" on release.

Returns: string | any

pulse() ​

javascript
pulse(intensity: number, seconds: number): Promise<boolean>

Play a haptic pulse on this controller.

Example:

javascript
await controller.pulse(0.6, 0.15);

Parameters:

  • intensity (number) - Strength, 0 to 1.
  • seconds (number) - Duration in seconds.

Returns: Promise<boolean>