Skip to content

Screen Gamepad

Updated

Phones and tablets have no keyboard and no sticks. Screen Gamepad (comp-screen-gamepad) draws a gamepad over the 3D view: a left stick in the lower left with the D-pad buttons 1 to 4 above it, and a right stick in the lower right with the A, B, X and Y buttons above it. It behaves exactly like a real gamepad, so a Character Controller walks and jumps from it and the camera controllers look around with it, with no code. Its buttons also send events your scripts can use for actions of their own.

You should know:

Add Screen Gamepad to a camera entity: the camera the player looks through. It shows over that camera’s view only, so in a page that embeds the player in a smaller area, the pad stays inside the view.

Screen Gamepad changes no component and no prop. It feeds the shared gamepad as a second pad, so everything that reads a gamepad reacts exactly as it does with a real one:

Control Feeds Who reacts
Left stick The gamepad’s left stick A Character Controller walks, and runs when the stick is pushed 90% or more; Fly flies; Orbit pans its point.
Right stick The gamepad’s right stick First Person, Third Person, Orbit and Fly look around.
A Gamepad button A A Character Controller jumps.
B, X, Y Gamepad buttons B, X and Y Nothing built in: listen for its events.
1, 2, 3, 4 The D-pad: up, right, down, left Nothing built in: listen for its events.

A real gamepad works alongside it: each stick reads whichever is pushed further, and a button is down on either.

Its touches stay on its own sticks and buttons: they never turn the camera, select or grab anything. A finger on the rest of the view still looks around through the camera’s controller.

Prop Type Default Description
display string 'auto' When it shows: 'auto' on touch screens only; 'always' with a mouse too, which is handy for testing on a desktop.
buttons string array ['leftStick', 'rightStick'] The controls it shows, by name: leftStick, rightStick, a, b, x, y, 1, 2, 3, 4. When a stick is hidden, its buttons drop into its corner.
padding number or list 28 The gap to the view’s edges in pixels, like a UI padding: one value for every side, [vertical, horizontal], or three or four values clockwise from the top.
color string '#3b82f6' The accent colour: the sticks’ glowing rims, the direction being pushed, and the buttons’ rings. Any CSS colour.
opacity number 1 How opaque the whole pad is, 0 to 1.
scale number 1 How big the pad is, 0 to 1 of its full size. It also shrinks on small screens by itself.
fade string 'fade-in-out' How it appears and disappears, over a quarter of a second: 'fade-in', 'fade-out', 'fade-in-out' or 'none'.

The pad is drawn for a phone held sideways. In smaller views it shrinks with the view, and when a view is too narrow for both sides (a phone held upright), it shrinks further until they fit without overlapping.

  • In play, while its camera is the one rendering. With several cameras, only the rendering camera’s pad shows.
  • On a touch screen, unless display is 'always'.
  • Not during a scene switch: it hides as the switch starts and the next scene’s pad shows once that scene is in.
  • Never in VR, and never in the editor.

With a Signal Emitter on the camera entity, Screen Gamepad sends:

eventType EventType constant data When
ScreenGamepad::press EventType.ScreenGamepad.Press { button } An on-screen button went down: 'a', 'b', 'x', 'y', '1', '2', '3' or '4'.
ScreenGamepad::release EventType.ScreenGamepad.Release { button } It went up again.

The sticks send no events: read their effect through what they drive, such as the Character Controller’s movement.

1. Show only the controls this scene needs

Section titled “1. Show only the controls this scene needs”
PadSetup.jx
#pragma lifecycle(startup)
function startup() {
const cam = CameraDirector.main;
const pad = cam && cam.getComponent(ScreenGamepadComponent);
if (!pad) return;
// A walking scene: move, look and jump, plus one action button.
pad.setProps({ buttons: ['leftStick', 'rightStick', 'a', 'x'], color: '#22c55e', opacity: 0.85 });
// ?pad=always shows it on a desktop too, for testing.
if (WebBrowser.queryParams().pad === 'always') pad.setProps({ display: 'always' });
log(Device.isTouch() ? 'Touch controls ready' : 'Touch controls hidden on this device');
}
  • CameraDirector.main is the main camera entity, where the Screen Gamepad lives; see Cameras.
  • setProps changes which controls appear and their look. A prop change redraws the pad at once.
  • A query parameter switches display to 'always', so you can test the pad with a mouse; see Device and haptics.
PadMenu.jx
#pragma lifecycle(startup)
const OPTIONS = ['Resume', 'Settings', 'Quit'];
let index = 0;
function startup() {
const label = findEntity('Menu Label').getComponent(UILabelComponent);
const show = () => label.setProps({ text: `> ${OPTIONS[index]}` });
show();
on('pad', (event) => {
if (event.eventType !== EventType.ScreenGamepad.Press) return;
const button = event.data.button;
if (button === '1') index = (index + OPTIONS.length - 1) % OPTIONS.length; // up
if (button === '3') index = (index + 1) % OPTIONS.length; // down
if (button === 'a') emit('menu/choose', { option: OPTIONS[index] });
show();
});
}
  • Buttons '1' and '3' are D-pad up and down. The modulo keeps index within the list, wrapping around.
  • A chooses the highlighted option.
  • Because A also makes a Character Controller jump, disable the player’s movement while the menu is open.
PadActions.jx
#pragma lifecycle(startup)
// The player camera has a Screen Gamepad with buttons a, b, x and y shown,
// and a Signal Emitter with the signal 'pad'.
const ACTIONS = {
b: 'player/crouch',
x: 'door/open',
y: 'ui/map',
};
function startup() {
on('pad', (event) => {
if (event.eventType !== EventType.ScreenGamepad.Press) return;
const action = ACTIONS[event.data.button];
if (action) emit(action);
});
}
  • A already makes the Character Controller jump, so the table only maps the buttons with no built-in meaning.
  • event.data.button is the button’s name, used to look up the signal to emit.
  • Other scripts listen to door/open, ui/map and so on, the same way they would for a keyboard or a UI button.
PadDash.jx
#pragma lifecycle(startup, update)
let holding = false;
let held = 0;
function startup() {
// The player camera has a Screen Gamepad and a Signal Emitter on 'pad'.
on('pad', (event) => {
if (event.data.button !== 'b') return;
if (event.eventType === EventType.ScreenGamepad.Press) {
holding = true;
held = 0;
}
if (event.eventType === EventType.ScreenGamepad.Release && holding) {
holding = false;
emit('player/dash', { power: Math.min(1, held / 1.5) });
}
});
}
function update(dt) {
if (holding) held += dt;
}
  • press and release arrive as separate events, so the script remembers in holding that B is down.
  • update counts how long it is held; the release turns that into a dash power from 0 to 1.
  • The same idea works for any “hold to charge” action on a touch screen.
  • Adding it to the wrong entity. Screen Gamepad shows over its own camera’s view, only while that camera renders. Put it on the camera the player looks through.
  • Testing on a desktop. With display: 'auto' it shows only on touch screens. Use 'always' to try it with a mouse.
  • Waiting for stick events. Sticks send none. They feed the gamepad, which the Character Controller and camera controllers read.
  • Comparing buttons to numbers. D-pad buttons are the strings '1' to '4'.
  • Expecting it in VR. It never shows in VR; XR controllers take over there.