Handles
Updated
A Handle (comp-handle) makes its entity, with its children, something the player drags directly. There is no gizmo: the drag itself moves, turns or scales the entity, within the locks and limits you set for each axis. Use it for objects that move in a constrained way: a door on its hinge, a drawer on its rails, a knob, a slider, a lever, or a push button that does not move at all.
Before you start
Section titled “Before you start”You should know:
- Pointers, pointer types and the shared hover and leave events; see Selection and grabbing.
- How events reach a script through a Signal Emitter; see Events and signals.
- That an entity’s position and rotation are relative to its parent, and that rotations are in radians.
How a drag works
Section titled “How a drag works”- Pressing any of the entity’s meshes, or a child entity’s, starts a drag. On desktop and mobile a screen pointer drags it; in XR a controller ray, a hand ray, a pinch or a fingertip poke all work. The
cursorandhandprops say which pointers it takes. - One pointer carries it like a held object. Twisting an XR controller turns it too.
- The drag is filtered by the axis options: locked axes keep the value they had when the drag began, clamped axes stay within their range.
- With
multitouch, a second pointer joins the drag: the midpoint between the two moves it, the line between them turns it, and their distance scales it. - During a drag the pointer is captured and the camera controller stands down. A dynamic rigid body is led kinematically and gets its own type back on release. Anything else is moved through the entity’s transform.
Drag modes
Section titled “Drag modes”Besides moving the entity, translate can turn the pointer’s motion into something else:
'as-rotate'turns the pointer’s sweep around the entity’s origin into a rotation. To make a door, put the Handle on a group whose origin is at the hinge, with the door mesh as its child.'as-scale'scales the entity by the pointer’s distance from its origin.
Axis options
Section titled “Axis options”translate, rotate and scale each take one of these values:
| Value | Meaning |
|---|---|
true |
Every axis is free. |
false |
Every axis is locked. |
'x', 'y' or 'z' |
Only that axis moves. |
{ x?, y?, z? } |
Per axis: true free, false locked, or [min, max] clamped to that range. A missing axis is free. |
- Ranges are absolute values in the parent’s frame, not amounts of change:
z: [0, 0.4]means the entity’s localposition.zstays between0and0.4. - Ranges are in scene units for position, radians for rotation (a quarter turn is
Math.PI / 2, about1.5708), and factors for scale. - With
uniformScaleon, scale limits constrain the single shared factor; the tightest limit stops the resize.
| Prop | Type | Default | Description |
|---|---|---|---|
translate |
axes, 'as-rotate' or 'as-scale' |
true |
Position axes the drag moves, or a drag mode. |
rotate |
axes | true |
Rotation axes the drag turns, in radians. |
scale |
axes | false |
Scale axes the drag stretches, as factors. |
uniformScale |
boolean | true |
Scale all axes by one factor. |
multitouch |
boolean | true |
A second pointer joins the drag: two hands rotate and scale. |
cursor |
boolean | true |
Dragged by a screen pointer or an XR controller ray. |
hand |
boolean | true |
Dragged by an XR hand close to it. |
With both cursor and hand off, the entity is not hit by pointers at all.
Common setups
Section titled “Common setups”| Object | Where the Handle goes | translate |
rotate |
scale |
|---|---|---|---|---|
| Door opening a quarter turn | A group whose origin is the hinge | 'as-rotate' |
{ x: false, y: [- |
false |
| Drawer sliding 40 cm | The drawer | { x: false, y: false, z: [0. |
false |
false |
| Knob turning about Y | The knob | 'as-rotate' |
'y' |
false |
| Floor slider at a fixed height | The slider | { y: false } |
false |
false |
| Push button | A group, with the cap as a child | false |
false |
false |
Events
Section titled “Events”eventType |
EventType constant |
data |
When |
|---|---|---|---|
Handle::start |
Event |
{ pointer |
A drag began. |
Handle::end |
Event |
{ pointer |
The drag ended: released, cancelled, or the component stopped listening. |
Handle::hover |
Event |
{ pointer |
A pointer came over the entity. |
Handle::leave |
Event |
{ pointer |
It left. |
No event is sent on every frame during a drag. To follow the motion, read the entity’s transform in update while isDragging is true, or once on Handle::end.
Push buttons
Section titled “Push buttons”With every axis locked (translate, rotate and scale all false), a Handle never moves. It still sends Handle::start on a press, even one with no drag, and Handle::end on release, from any pointer: a click, a tap, an XR ray, a pinch or a fingertip’s poke. That makes it a push button that works everywhere, including a real “press with your finger” in XR.
Scripting
Section titled “Scripting”| Member | Description |
|---|---|
isDragging |
true while the Handle is being dragged. |
set |
Change locks and limits at runtime. A change ends any drag in progress first. |
Where it works
Section titled “Where it works”- Which entities. Handle is offered on meshes, primitives, groups, text, media surfaces and UI panels.
- Play only. In the editor, the editor’s transform gizmo takes over.
- Object manipulation setting. When the project’s Allow Object Manipulation player setting is off, a Handle does not move, and a live drag is cancelled.
- Multiplayer. Dragging the Handle of a networked entity asks for its ownership; see Multiplayer.
Examples
Section titled “Examples”1. Set up a door from a script
Section titled “1. Set up a door from a script”#pragma lifecycle(startup)
const OPEN = -Math.PI / 2; // a quarter turn
function startup() { const hinge = findEntity('Door Hinge'); hinge.getComponent(HandleComponent).setProps({ translate: 'as-rotate', rotate: { x: false, y: [OPEN, 0], z: false }, scale: false, });
on('door', (event) => { if (event.eventType !== EventType.Handle.End) return; const openness = hinge.rotation.y / OPEN; // 0 closed, 1 fully open log(`Door ${Math.round(openness * 100)}% open`); if (openness > 0.9) emit('door/opened'); });}A script configures a door hinge's Handle to turn the pointer's sweep into a clamped rotation, and reports how far the door is open when you let go.
Drag the door to swing it open, then let go. Scroll to zoom.
'as-rotate'turns the player’s sweep around the group’s origin into a rotation, so the door swings about its hinge.rotatelocks X and Z and clamps Y between a quarter turn (-π/2) and closed (0). These are absolute angles in the parent’s frame.- On
Handle::end, the script reads the hinge’srotation.yto see how far the door is open, and announces when it is nearly fully open.
2. A push button
Section titled “2. A push button”#pragma lifecycle(startup)
// The Button group has a Handle with translate, rotate and scale all false,// and a Signal Emitter with the signal 'button'. Its child Cap is the part that moves.function startup() { const cap = findEntity('Cap'); const rest = cap.position.y;
on('button', (event) => { if (event.eventType === EventType.Handle.Start) { Tween.to(cap.position, { y: rest - 0.01 }, { duration: 0.05 }); } if (event.eventType === EventType.Handle.End) { Tween.to(cap.position, { y: rest }, { duration: 0.15 }); emit('lamp/toggle'); } });}A Handle with every axis locked still reports press and release: the cap sinks with a tween, springs back, and the release toggles a lamp through an Enable On Signal.
Press and release the button to switch the lamp on and off. Drag elsewhere to orbit the camera, scroll to zoom.
- With every axis locked, the Handle never moves, but still reports press (
start) and release (end). - The cap sinks a centimetre on press and springs back on release, using
Tween.toto animate its position; see Animation. - The action fires on release, the way buttons usually behave, so a player can slide off to cancel.
3. A drawer that knows when it is open
Section titled “3. A drawer that knows when it is open”#pragma lifecycle(startup, update)
const CLOSED_Z = 0.03;const OPEN_Z = 0.43;let drawer;let handle;let wasOpen = false;
function startup() { // The Drawer has a Handle: translate { x: false, y: false, z: [0.03, 0.43] }, rotate false. drawer = findEntity('Drawer'); handle = drawer.getComponent(HandleComponent);}
function update() { if (!handle.isDragging) return; const amount = (drawer.position.z - CLOSED_Z) / (OPEN_Z - CLOSED_Z); // 0 to 1 const open = amount > 0.5; if (open !== wasOpen) { wasOpen = open; emit(open ? 'drawer/opened' : 'drawer/closed'); }}A Handle lets a drawer slide only along Z between two limits; while it is dragged, a script reads its position and signals when it passes halfway, which lights a lamp through an Enable On Signal.
Drag the drawer out past halfway to light the green lamp, then push it back in. Drag elsewhere to orbit the camera, scroll to zoom.
- The Handle lets the drawer slide only along local Z, between the two limits.
- Because no event arrives during the drag, the script reads
position.zinupdatewhileisDraggingis true. wasOpenremembers the last state, so each signal is sent once when the drawer crosses the halfway point.
4. A knob that sets a value
Section titled “4. A knob that sets a value”#pragma lifecycle(startup)
const MIN_ANGLE = -2.4; // radians, turned fully clockwise (seen from above)const MAX_ANGLE = 2.4; // turned fully counter-clockwise
function startup() { // The Knob has a Handle and a Signal Emitter with the signal 'knob'. const knob = findEntity('Knob'); knob.getComponent(HandleComponent).setProps({ translate: 'as-rotate', rotate: { x: false, y: [MIN_ANGLE, MAX_ANGLE], z: false }, });
on('knob', (event) => { if (event.eventType === EventType.Handle.Hover) Haptics.vibrate(10, { intensity: 0.2 }); if (event.eventType !== EventType.Handle.End) return;
// Turning clockwise lowers rotation.y: fully counter-clockwise is 0, fully clockwise is 1. const volume = (MAX_ANGLE - knob.rotation.y) / (MAX_ANGLE - MIN_ANGLE); emit('audio/volume', { value: Math.min(1, Math.max(0, volume)) }); });}A script sets a knob's Handle to turn only about Y, within about 275 degrees, and maps the angle to a volume from 0 to 1 when it is let go.
Drag across the knob to turn it, then let go: the volume is sent on audio/volume. Drag elsewhere to orbit the camera, scroll to zoom.
- The knob turns only about Y, within about 275 degrees, like a real volume knob.
- A tiny buzz on hover tells an XR player their hand is on the knob; see Device and haptics.
- On release, the angle is mapped to a volume between
0and1and sent on a signal for an audio script to apply.
5. Lock the door once a puzzle is solved
Section titled “5. Lock the door once a puzzle is solved”#pragma lifecycle(startup)
function startup() { const hinge = findEntity('Door Hinge'); const handle = hinge.getComponent(HandleComponent);
on('puzzle/solved', () => { // Snap the door shut, then lock every axis. hinge.setRotation(0, 0, 0); handle.setProps({ translate: false, rotate: false, scale: false }); log('The door is locked'); });
on('puzzle/reset', () => { handle.setProps({ translate: 'as-rotate', rotate: { x: false, y: [-Math.PI / 2, 0], z: false } }); });}When the puzzle is solved, a script snaps a door shut and locks every axis of its Handle; resetting the puzzle restores the Handle's props.
Drag the door to swing it, then press Solve puzzle: it snaps shut and no longer moves. Press Reset puzzle to unlock it. Scroll to zoom.
- A
setPropscall that locks every axis turns the door into an immovable object. If the player is dragging it, the drag ends first. setRotationputs the door back to closed through the entity’s transform setter.- Restoring the original props unlocks it again.
Common mistakes
Section titled “Common mistakes”- Using deltas as limits. Limits are absolute values in the parent’s frame. A drawer whose rest position is
z = 0.03needs[0.03, 0.43], not[0, 0.4]. - Degrees instead of radians. Rotation limits are in radians. Use
Math.PI / 2for 90°. - A door that spins around its middle.
'as-rotate'turns around the entity’s origin. Put the Handle on a group whose origin is on the hinge. - Waiting for a per-frame event. There is none. Read the transform in
updatewhileisDragging, or onHandle::end. - Expecting a Handle drag to snap into a Snap Zone. Snap zones react only to Grabbable drags.