Appearance
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.
nullwhile its position isn't tracked, on a gamepad, and whereenvironment.features.has('accessoryTracking')is false.
buttons
- Type: Object<string, any>
- Current button state by name; a button the controller lacks is
undefined. Gamepads:buttonAbuttonBbuttonXbuttonY,leftShoulderrightShoulder,leftTriggerrightTrigger(valueis analog),leftThumbstickButtonrightThumbstickButton,buttonMenubuttonOptions. Spatial controllers:trigger,gripButton,thumbstickButton,buttonA(lower face button of that hand),buttonB(upper face button),buttonMenu. Styluses:tip(pressed against a surface;valueis the pressure),primaryButton(the barrel button nearest the tip),secondaryButton(the one above it;valueis how hard it is pressed). Entries update in place every frame.
grip read-only
- Type: Pose | null
- Where the hand holds a spatial controller.
nullon 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 fromconnecton, unless accessory tracking isn't allowed.nullfor a gamepad, for a stylus nobody holds, and wherever the device doesn't say. Treatnullas 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
falseonce 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:
leftThumbstickrightThumbstickdirectionPadon a gamepad,thumbstickon a spatial controller. Entries update in place every frame.
Methods
off()
javascript
off(id: string): voidRemove a subscription made with on.
Parameters:
id(string) - Id returned fromon.
Returns: void
on()
javascript
on(type: string, options?: Object | function, listener?: function): string | anySubscribe 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 }, wherephaseis"began"(pressed),"ended"(released) or"changed"(value or touch changed while the press state stayed), andaimandgripare the controller's poses in the frame of the change, asPoseornull;"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>