Selection and grabbing
Updated
Three components make an entity respond to the player’s pointer without any code. Selectable lets the player click or touch it to select it. Grabbable lets them pick it up and carry it. Pivotable puts a gizmo of arrows, rings and spheres on it, so they can move, turn and scale it precisely. Each one works the same with a mouse, a finger, an XR controller’s ray or a tracked hand, and each tells your scripts what happened through events.
A Selectable that reports clicks and hovers, and a Grabbable you can drag with a mouse, a touch screen or an XR controller. A script listens to both on the event bus.
Hover and click the round shape on the left, then drag the box on the right. Drag to orbit the camera, scroll to zoom.
Before you start
Section titled “Before you start”You should know:
- How events reach a script through a Signal Emitter and
on(signal, callback); see Events and signals. - How to change a component’s props from a script with
setProps; see Components. - For grabbed objects that fall and bounce, what a rigid body is; see Physics.
| Component | Type | What the player does |
|---|---|---|
| Selectable | comp- |
Clicks or touches the object to select it. One object is selected at a time. |
| Grabbable | comp- |
Picks the object up and carries it. |
| Pivotable | comp- |
Moves, turns or scales the object with a gizmo. |
To drag an object within limits (a door, a drawer, a knob), use a Handle instead.
All three run in play only. In the editor, the editor’s own selection and transform gizmo take over, and these components listen to nothing. They are event-driven: an idle one does no work per frame.
Pointers: desktop, mobile and XR
Section titled “Pointers: desktop, mobile and XR”A pointer is whatever the player points with. Interaction components tell two kinds apart:
| Kind | pointerType values |
Where |
|---|---|---|
| cursor | screen-mouse, screen-touch, screen-pen, ray |
A mouse, a finger or a pen on the screen; an XR controller’s ray. |
| hand | grab, touch |
An XR hand close to the object: a pinch or grab (grab), or a fingertip poke (touch). |
Selectable and Grabbable have a cursor prop and a hand prop. Set one to false and that kind of pointer passes through the object as if the component were not there. Pivotable takes every pointer.
A press on any mesh under the entity, including a child entity’s mesh, counts as a press on the entity. When a Grabbable child sits inside a Grabbable group, the nearest Grabbable takes the press, so the child is grabbed on its own.
Shared pointer events
Section titled “Shared pointer events”Selectable, Grabbable, Pivotable and Handle all send two pointer events, for pointers they accept:
eventType |
data |
When |
|---|---|---|
<Source>::hover |
{ pointer |
A pointer came over the entity or any of its children. |
<Source>::leave |
{ pointer |
That pointer left it. |
<Source> is the component that sends it: Selectable, Grabbable, Pivotable or Handle. In a script, compare with EventType.Selectable.Hover, EventType.Grabbable.Leave and so on. Like every component event, they reach scripts only through a Signal Emitter on the entity.
Selectable
Section titled “Selectable”Selectable tracks whether its entity is selected.
- A click or touch on the entity selects it.
- Selection is single across the scene: selecting one entity deselects every other Selectable.
- A click on empty space clears the selection.
- A script can select it too, with
setSelected(true)or by setting theselectedprop. - A Pivotable with
activation: 'selection'shows its gizmo only while the entity is selected.
| Prop | Type | Default | Description |
|---|---|---|---|
selected |
boolean | false |
Selected state applied when the entity becomes active. Changing it at runtime selects or deselects. |
cursor |
boolean | true |
Selectable with a screen pointer or an XR controller ray. |
hand |
boolean | true |
Selectable with an XR hand, by a grab or a touch. |
Events
Section titled “Events”eventType |
EventType constant |
data |
When |
|---|---|---|---|
Selectable::select |
Event |
{ pointer |
It was selected. pointerType is absent when a script selected it. |
Selectable::deselect |
Event |
{ pointer |
It was deselected. |
Selectable::change |
Event |
{ selected, pointer |
Right after select or deselect, with the new state. |
Selectable::hover, Selectable::leave |
Event, .Leave |
{ pointer |
See Shared pointer events. |
Scripting
Section titled “Scripting”| Member | Description |
|---|---|
selected |
true while the entity is the current selection. |
set |
Select or deselect it from a script. Sends select or deselect, then change. |
Selectable |
Whether entity has a Selectable that is selected now. |
Grabbable
Section titled “Grabbable”Grabbable lets the player pick the entity up and carry it. How the entity follows depends on the pointer that grabbed it:
| Pointer | How the entity follows |
|---|---|
Screen pointer (screen-*) |
It is held in the camera’s frame: walking or turning carries it along, and moving the pointer drags it at the depth where it was grabbed. |
XR controller ray (ray) |
It is attached to the controller at the ray’s distance, following every move and turn. |
XR hand (grab, touch) |
It is attached to the hand, following every move and turn. |
While a grab is live:
- the grab is exclusive, and the pointer is captured, so a fast drag is not lost;
- the camera controller stands down, so the drag does not also turn the camera;
- a simulated rigid body on the entity is driven kinematically: it stops falling and pushes other bodies. It gets its own type back on release, so a dynamic crate falls when dropped.
A grab ends when the pointer is released or cancelled, and also when the component stops listening: the entity is deactivated, or a prop change turns its pointer off.
| Prop | Type | Default | Description |
|---|---|---|---|
cursor |
boolean | true |
Grabbable with a screen pointer or an XR controller ray. |
hand |
boolean | true |
Grabbable with an XR hand close to it. |
With both off, the entity is not hit by pointers at all.
Events
Section titled “Events”eventType |
EventType constant |
data |
When |
|---|---|---|---|
Grabbable::start |
Event |
{ pointer |
A grab began. |
Grabbable::end |
Event |
{ pointer |
The grab ended, however it ended. |
Grabbable::hover, Grabbable::leave |
Event, .Leave |
{ pointer |
See Shared pointer events. |
There is no event on every frame while the entity moves: read its transform if you need to follow it.
Scripting
Section titled “Scripting”| Member | Description |
|---|---|
isGrabbed |
true while the entity is held. |
Pivotable
Section titled “Pivotable”Pivotable puts an interactive gizmo on its entity: translate arrows, plane sliders, rotate rings, scale spheres and a free-move center. The player drags a part of the gizmo to move, turn or scale the entity.
Like Grabbable, a drag captures the pointer, makes the camera controller stand down and drives a simulated rigid body kinematically. An entity without a body is moved through its transform.
| Prop | Type | Default | Description |
|---|---|---|---|
translate |
boolean | true |
Show the translate arrows and plane sliders. |
rotate |
boolean | true |
Show the rotate rings. |
scale |
boolean | false |
Show the scale spheres. |
scaleAxes |
{ x?, y?, z? } of booleans |
all axes | A false axis hides that axis’s scale sphere and keeps that axis’s scale unchanged. Missing axes are allowed. |
space |
'local' or 'world' |
'world' |
Axis frame: local follows the entity’s orientation, world stays aligned with the world. |
size |
number | 1 |
Screen-size multiplier for the gizmo, 0.2 to 10. |
activation |
'selection' or 'always' |
'selection' |
selection shows the gizmo while the entity’s Selectable is selected; always keeps it visible. |
plane |
'XY', 'YZ' or 'XZ' |
none | Restrict the gizmo to one plane: its two arrows and scale spheres, its slider and the ring around its normal. Leave it out for full 3D. |
interactive |
boolean | true |
With false, the gizmo is a passive orientation indicator: only the axis arrows show, and nothing responds to the pointer. |
With activation: 'selection', add a Selectable to the same entity, or the gizmo never shows.
Events
Section titled “Events”eventType |
EventType constant |
data |
When |
|---|---|---|---|
Pivotable::start |
Event |
{ handle } |
A drag began. handle is arrow, slider, rotator, sphere or center. |
Pivotable::end |
Event |
{ handle } |
The drag ended. |
Pivotable::hover, Pivotable::leave |
Event, .Leave |
{ pointer |
See Shared pointer events. |
Scripting
Section titled “Scripting”| Member | Description |
|---|---|
isDragging |
true while a part of the gizmo is being dragged. |
Where they work
Section titled “Where they work”- Which entities. Grabbable, Selectable and Pivotable are offered on meshes, primitives, splats, text, media surfaces, UI panels, fire and groups; Selectable also on terrain and water. A grabbed group moves with everything under it.
- Object manipulation setting. When the project’s Allow Object Manipulation player setting is off, Grabbable, Pivotable and Handle move nothing, and a live drag is released. Selectable keeps working.
- Multiplayer. On a networked entity, selecting it, grabbing it or dragging its gizmo asks for its ownership, so what the player does reaches everyone. An entity locked to another player can still be selected, but not moved. See Multiplayer.
Examples
Section titled “Examples”1. A lamp switched by selection
Section titled “1. A lamp switched by selection”#pragma lifecycle(startup)
// The Lamp entity has a Selectable and a Signal Emitter with the signal 'lamp'.// Its child Lamp Bulb holds the light.function startup() { const bulb = findEntity('Lamp Bulb'); const lamp = findEntity('Lamp').getComponent(SelectableComponent);
on('lamp', (event) => { if (event.eventType !== EventType.Selectable.Change) return; bulb.setEnabled(event.data.selected); log(event.data.pointerType ? `Switched with ${event.data.pointerType}` : 'Switched by a script'); });
lamp.setSelected(true); // start with the lamp on}A lamp's Selectable switches its bulb: one listener on Selectable::change enables the bulb, with its light, while the lamp is selected.
The lamp starts on. Click empty space to switch it off, and click the lamp to switch it on again. Drag to orbit the camera, scroll to zoom.
Selectable::changearrives after every select and deselect, withdata.selected, so one check covers both.setEnabledswitches the bulb entity, and the light it holds, on or off.pointerTypeis absent when a script selected the lamp, which is what happens on the last line:setSelected(true)sendsselectandchangelike a click would.- Because selection is single, clicking another Selectable turns the lamp off. To keep several things on independently, track your own state on
Selectable::selectinstead.
2. Highlight on hover
Section titled “2. Highlight on hover”#pragma lifecycle(startup)
// Every exhibit has a Selectable, an Outline (disabled in the editor),// and a Signal Emitter with the signal 'exhibits'.function startup() { on('exhibits', (event) => { const entity = findEntity(event.entityId); const outline = entity && entity.getComponent(OutlineComponent); if (!outline) return;
if (event.eventType === EventType.Selectable.Hover) outline.setEnabled(true); if (event.eventType === EventType.Selectable.Leave && !entity.getComponent(SelectableComponent).selected) { outline.setEnabled(false); } if (event.eventType === EventType.Selectable.Deselect) outline.setEnabled(false); });}Three exhibits share one signal: hovering one enables its Outline, leaving removes it unless the exhibit is selected, and deselecting clears it.
Hover over an exhibit to outline it; click one to keep it outlined, then click empty space to clear it. Drag to orbit the camera, scroll to zoom.
- All exhibits share one signal, so one listener serves them all.
event.entityIdis the exhibit that sent the event, andfindEntityturns it into the entity. - The Outline component draws a coloured edge around the entity while it is enabled; see Graphics.
- Hover turns the outline on. Leave turns it off, unless the exhibit is selected, so the selected exhibit stays highlighted. Deselect then removes it.
3. Crates that return when dropped off the edge
Section titled “3. Crates that return when dropped off the edge”#pragma lifecycle(startup, update)
const FLOOR_LIMIT = -5; // below this height, the crate has fallen offconst crates = [];
function startup() { for (const name of ['Crate A', 'Crate B', 'Crate C']) { const entity = findEntity(name); crates.push({ entity, grab: entity.getComponent(GrabbableComponent), home: entity.position.clone() }); }
on('crates', (event) => { if (event.eventType === EventType.Grabbable.Start) log(`Picked up with ${event.data.pointerType}`); if (event.eventType === EventType.Grabbable.End) emit('sfx/drop', { id: event.entityId }); });}
function update() { for (const crate of crates) { if (crate.grab.isGrabbed || crate.entity.position.y > FLOOR_LIMIT) continue; crate.entity.setPosition(crate.home.x, crate.home.y, crate.home.z); log(`${crate.entity.name} returned`); }}Grabbable crates with dynamic bodies on a small platform: one script logs each pickup and puts back any crate that falls off the edge.
Drag a crate off the platform and let it fall. Scroll to zoom.
startuprecords each crate’s starting position withposition.clone(), a copy that will not change as the crate moves.- While a crate is held, its dynamic body is driven kinematically; on release it falls again under gravity.
updatechecks each crate’s height.isGrabbedskips a crate the player is holding.setPositionmoves the crate and its rigid body. Writingpositiondirectly would move only what is drawn, and the body would pull it back.
4. Switch the gizmo with keys
Section titled “4. Switch the gizmo with keys”#pragma lifecycle(startup, update)
let pivot;
function startup() { pivot = findEntity('Chair').getComponent(PivotableComponent);}
function update() { if (pivot.isDragging) return; // never change modes mid-drag
const keys = Input.keyboard; if (keys.isKeyReleased(Key.T)) pivot.setProps({ translate: true, rotate: false, scale: false, plane: 'XZ' }); if (keys.isKeyReleased(Key.R)) pivot.setProps({ translate: false, rotate: true, scale: false, plane: 'XZ' }); if (keys.isKeyReleased(Key.S)) pivot.setProps({ translate: false, rotate: false, scale: true, scaleAxes: { y: false } }); if (keys.isKeyReleased(Key.L)) { pivot.setProps({ space: pivot.props.space === 'local' ? 'world' : 'local' }); }}Click the chair to show its Pivotable gizmo and drag the arrows or rings to move it; T, R and S switch between moving on the floor, turning and scaling, and L between local and world axes.
Click the chair, drag its gizmo, then press T, R, S or L to change what the gizmo does. The arrow keys and WASD do not move the camera here. Drag the background to orbit, scroll to zoom.
setPropschanges several props at once. T keeps only the arrows and slider on the floor plane, R keeps only the ring that turns the chair around its vertical axis, S keeps only the scale spheres, with height scaling turned off.pivot.props.spacereads the current prop value, so L toggles between local and world axes.isDraggingstops a key press from changing the gizmo while the player is using it.
Snippets
Section titled “Snippets”1. Count what is selected across groups
Section titled “1. Count what is selected across groups”#pragma lifecycle(startup)
const PARTS = ['Engine', 'Wheel', 'Seat', 'Handlebar'];
function startup() { const parts = PARTS.map((name) => findEntity(name));
// Each part has a Selectable and a Signal Emitter with the signal 'parts'. on('parts', (event) => { if (event.eventType !== EventType.Selectable.Select) return; const chosen = parts.find((part) => SelectableComponent.isSelected(part)); emit('ui/inspect', { name: chosen ? chosen.name : null }); });
on('ui/clear', () => { for (const part of parts) part.getComponent(SelectableComponent).setSelected(false); });}SelectableComponent.isSelected(entity)is a static helper: it checks any entity, without fetching its component first.- On every select, the script finds which part is selected and tells an inspector panel through
emit. - The
ui/clearsignal deselects every part from code, for a “Clear” button.
React to every click
Section titled “React to every click”Selectable::select is sent when the entity becomes selected. A click on an entity that is already selected
changes nothing, so it sends no event. That suits a selection, where the object stays highlighted until something else
is picked. For a button-like object that should react to every click, release the selection as soon as you handle
it. The next click then selects it again and sends another select:
#pragma lifecycle(startup)
let clicks = 0;
function startup() { // The entity's Signal Emitter puts its Selectable events on 'counter/clicked'. on('counter/clicked', (event) => { if (event.eventType !== EventType.Selectable.Select) return; // Release the selection, so the next click selects it again. findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); clicks += 1; log('Clicked', clicks, 'times'); });}setSelected(false) also sends Selectable::deselect and Selectable::change. The check on Select ignores them.
Common mistakes
Section titled “Common mistakes”- Expecting a second click to fire again. A click on an already selected entity sends nothing. Release the
selection with
setSelected(false)to react to every click (see React to every click). - No Signal Emitter. Without one, the component’s events go nowhere and your listener never runs. Add a Signal Emitter with a non-empty
signalto the entity. - Expecting several selections. Selectable keeps one selection across the whole scene. For multi-select, keep your own list on
Selectable::select. - A Pivotable that never appears. With
activation: 'selection', the entity also needs a Selectable. Useactivation: 'always'for a gizmo that is always shown. - Testing in the editor. These components run in play only. Enter play to try them.
- Moving a grabbed body yourself. While
isGrabbedis true, the Grabbable drives the body. Wait forGrabbable::endbefore moving it from a script. - Writing
positionon a physics body. UsesetPosition, so the rigid body moves too.