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.
Before you start
Section titled “Before you start”You should know:
- How the player’s character and camera controllers move and turn the view; see Character Controller and Camera controllers.
- How events reach a script through a Signal Emitter; see Events and signals.
Adding it
Section titled “Adding it”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.
What it does
Section titled “What it does”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 | ['left |
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.
When it shows
Section titled “When it shows”- In play, while its camera is the one rendering. With several cameras, only the rendering camera’s pad shows.
- On a touch screen, unless
displayis'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.
Events
Section titled “Events”With a Signal Emitter on the camera entity, Screen Gamepad sends:
eventType |
EventType constant |
data |
When |
|---|---|---|---|
Screen |
Event |
{ button } |
An on-screen button went down: 'a', 'b', 'x', 'y', '1', '2', '3' or '4'. |
Screen |
Event |
{ button } |
It went up again. |
The sticks send no events: read their effect through what they drive, such as the Character Controller’s movement.
Examples
Section titled “Examples”1. Show only the controls this scene needs
Section titled “1. Show only the controls this scene needs”#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');}A script reads the Screen Gamepad on the main camera and keeps only two sticks, A and X, recoloured green.
Watch the on-screen pad: the scene starts with only the sticks, A and X, in green. Drag to orbit the camera, scroll to zoom.
CameraDirector.mainis the main camera entity, where the Screen Gamepad lives; see Cameras.setPropschanges which controls appear and their look. A prop change redraws the pad at once.- A query parameter switches
displayto'always', so you can test the pad with a mouse; see Device and haptics.
2. Navigate a menu with the D-pad
Section titled “2. Navigate a menu with the D-pad”#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(); });}The camera's Screen Gamepad shows a D-pad and an A button; a script moves through a menu on presses of 1 and 3 and chooses with A.
Press the 1 and 3 buttons of the on-screen pad to move through the menu.
- Buttons
'1'and'3'are D-pad up and down. The modulo keepsindexwithin 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.
Snippets
Section titled “Snippets”1. Map buttons to actions
Section titled “1. Map buttons to actions”#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); });}Aalready makes the Character Controller jump, so the table only maps the buttons with no built-in meaning.event.data.buttonis the button’s name, used to look up the signal to emit.- Other scripts listen to
door/open,ui/mapand so on, the same way they would for a keyboard or a UI button.
2. Hold to sprint-charge, release to dash
Section titled “2. Hold to sprint-charge, release to dash”#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;}pressandreleasearrive as separate events, so the script remembers inholdingthat B is down.updatecounts how long it is held; the release turns that into a dash power from0to1.- The same idea works for any “hold to charge” action on a touch screen.
Common mistakes
Section titled “Common mistakes”- 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.