Interaction
Updated
Interaction is how players act on the objects of your world: clicking a button on a machine, picking up a box, opening a door, dropping a photo onto a wall. Interaction components (Selectable, Grabbable, Pivotable, Handle and Snap Zone) do this for you on desktop, mobile and XR alike: a Grabbable box can be dragged with a mouse, a finger or an XR hand without a single line of code. They tell your scripts what happened through events, and raycasts answer what is under the pointer or along any line. Most interactivity comes from these components, added to any entity; the Snap Zone is also an entity of its own, an interactive object that stands on its own in a scene.
To read devices directly (keys, sticks, sensors), see Input.
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.
Key concepts
Section titled “Key concepts”-
Pointer. Whatever the player points with. Interaction components tell two kinds apart: a cursor (a mouse, a finger or pen on the screen, or an XR controller’s ray) and a hand (an XR hand close to the object, grabbing or poking it).
-
Interaction component. A component that makes an entity respond to pointers by itself: Selectable (click to select), Grabbable (pick up and carry), Pivotable (move, turn and scale with a gizmo), Handle (drag within limits), Snap Zone (drop media into a grid). They run in play only.
-
Signal Emitter. Components never call your script directly. They hand their events to a Signal Emitter on the same entity, which puts them on the event bus under a signal name of your choice. A script listens with
on(signal, callback)and comparesevent.eventTypeagainst theEventTypeconstants. -
Raycast. An invisible line cast into the scene to ask “what is along here?”. Raycasts turn a screen point, a controller or any world line into the entity it hits, the exact point and the surface’s facing.
How it works
Section titled “How it works”- Interaction components handle pointers on their own. When the player clicks a Selectable or drags a Grabbable, the component does the work: it selects, moves the entity, captures the pointer so the drag is not lost, and makes the camera controller stand down so the drag does not also turn the view. A dynamic rigid body being dragged is driven kinematically, then gets its own type back on release.
- Events reach scripts through a Signal Emitter. Each interaction component sends events such as
Selectable::selectorGrabbable::end. A Signal Emitter on the entity emits them on its signal; youron(signal, …)listener receives{ entityId, eventType, data }. - Scripts react, or ask. A script reacts to those events, and casts rays when it needs to know what is under the pointer without a component.
A Snap Zone works together with components on other entities: each item it takes carries a Snappable, which marks it as an item a zone may take, and a Grabbable, which lets the player drag it. Matching groups on the zone and the Snappables decide which items go where. The zone sends SnapZone::snap, unsnap, change and resize through its Signal Emitter, and each item sends Snappable::snap and unsnap on its own entity.
Interaction components are play only. In the editor they listen to nothing, and the editor’s own selection and transform gizmo take over.
Your first interaction
Section titled “Your first interaction”This walkthrough makes a lamp that switches on and off each time the player clicks or taps it.
-
Add a Primitive Mesh with a sphere shape and name it
Lamp. Give it a Standard Material. -
On the lamp, add a Selectable component. Leave its props at their defaults, so it can be clicked with any pointer.
-
Still on the lamp, add a Signal Emitter and set its
signaltolamp. This is the name your script listens to. -
Add a Behavior entity anywhere in the scene with this script:
LampSwitch.jx #pragma lifecycle(startup)let lit = false;function startup() {const lamp = findEntity('Lamp');const material = lamp.getComponent(StandardMaterialComponent);// The lamp's Signal Emitter puts its Selectable events on 'lamp'.on('lamp', (event) => {if (event.eventType !== EventType.Selectable.Select) return;// Release the selection, so the next click selects it again.lamp.getComponent(SelectableComponent).setSelected(false);lit = !lit;material.setProps({ emissive: '#facc15', emissiveIntensity: lit ? 2 : 0 });log(lit ? 'Lamp on' : 'Lamp off');});} -
Enter play and click the lamp: it lights up, and the next click switches it off.
on('lamp', …)receives every event the lamp’s Signal Emitter sends; theeventTypecheck keeps only selects.setSelected(false)releases the lamp straight away, so every click selects it again and sends a newselect.emissiveIntensityabove 0 makes the material glow in theemissivecolour.
Choosing
Section titled “Choosing”| If you want… | Use… | Read |
|---|---|---|
| Click to select, pick up and carry, or move with a gizmo | Selectable, Grabbable, Pivotable | Selection and grabbing |
| A door, drawer, knob, lever or push button | Handle | Handles |
| Dragged images, videos, PDFs and other flat media to line up in a tidy grid on a wall or shelf | A Snap Zone, and a Snappable on each item | Snap zones |
| What is under the pointer, or along any line | Graphics, Physics, XRRaycaster |
Picking and raycasts |
| To convert between screen, world and local points | Coordinate |
Coordinate space |
| To read keys, sticks or sensors yourself | Input |
Input |
Entity types
Section titled “Entity types”The Snap Zone is the interaction entity type, with a page of its components, props, events and a first script:
| Entity | Type | What it is |
|---|---|---|
| Snap Zone | ent-snap-zone | A box that snaps media into a neat grid. |
In scripts
Section titled “In scripts”| Global | What it is |
|---|---|
SelectableComponent | Tracks an entity's runtime selected state, driven by a pointer click on the entity — by a cursor (screen pointer or XR ray) and/or an XR hand, as cursor and hand allow, like Grabbable. Selection is single across the scene: selecting one entity deselects every other SelectableComponent (coordinated over the event bus), and a click on empty space clears the selection. Other components read the state — PivotableComponent shows its gizmo only while selected. |
GrabbableComponent | Makes its entity draggable with a pointer. Follow strategy is picked from the grabbing pointer: cursor (screen-*, ray) — screen pointers hold the entity in the camera frame, the XR ray welds it to the controller at ray distance (6DoF); hand (grab, touch) welds it to the hand (6DoF). All XR grabs are 6DoF. Grabs are exclusive and the pointer is captured. While live the pointer is captured at InputLayer.Grab, the camera rig is suspended, and a sibling PhysicsRigidBodyComponent is driven kinematically. Play-mode only, event-driven (see InteractableComponent). |
PivotableComponent | Attaches an interactive pivot gizmo to the entity — a faithful, imperative port of drei's PivotControls (arrows, plane sliders, rotators, scaling spheres). Each handle produces a world-space delta matrix; the delta is composed onto the entity's captured world matrix (mW = mdW · mW0) and converted back to the parent's local frame, exactly as drei does, so drags feel identical. |
HandleComponent | Makes its entity (with its children) a handle: dragging it translates, rotates or scales it directly, with no gizmo, within per-axis locks and limits in its parent's frame. One pointer carries it like a held object; translate: 'as-rotate' sweeps it around its origin (a door on its hinge), as-scale stretches it from there; two pointers move, turn and scale it. The drag math is @pmndrs/handle's. |
SnapZoneComponent | A volumetric box that snaps planar media entities with a Snappable in one of its groups (any, when it lists none) onto an auto-sizing grid. |
SnappableComponent | Lets its planar media entity (image, KTX2, GIF, SVG, video, PDF, Lottie) snap into Snap Zones: a zone attracts, takes and keeps only Snappables, and of those only the ones sharing one of its groups, or any when it lists none. Dragging needs a Grabbable too. Disabled, it snaps nowhere. |
GraphicsRaycaster | A ray against what is drawn: meshes, lines, points and sprites, down to the triangle. A native THREE.Raycaster; see Raycaster for how to use it. Hidden objects and objects excluded by the current local view are never hit. |
PhysicsRaycaster | A ray against colliders, in the physics world: one query over a few simple shapes instead of every triangle, and exactly what the physics blocks. A native THREE.Raycaster too; see Raycaster for how to use it. |
XRRaycaster | A ray against the real world, asked of the device (a WebXR hit test): the floor, walls, tables and anything else it understands, in an immersive session. The device only casts from what it tracks, so the rays start there: |
CoordinateSpace | Conversions between the screen, the world, and an object's own space. |
EventType | Every event type a script can hear, by who sends it: compare them rather than typing the strings. |
Components
Section titled “Components”| Component | Type | What it does |
|---|---|---|
| Grabbable | comp-grabbable | Lets the player pick up and drag the entity with mouse, ray or hand. |
| Handle | comp-handle | Drag the entity to move, rotate or scale it within limits. |
| Pivotable | comp-pivotable | Adds an interactive move/rotate/scale gizmo to the entity. |
| Selectable | comp-selectable | Makes the entity selectable with a click or hand touch. |
| Snap Zone | comp-snap-zone | A box that snaps media into a neat grid. |
| Snappable | comp-snappable | Lets a media entity snap into Snap Zones. |
In this section
Section titled “In this section”| Topic | Description |
|---|---|
| Selection and grabbing | Selectable, Grabbable and Pivotable, pointer kinds and their shared hover events. |
| Handles | Drag an entity within per-axis locks and limits: doors, drawers, knobs and push buttons. |
| Snap zones | Boxes that pull dragged media into a tidy grid, and the Snappable items they take. |
| Picking and raycasts | Graphics, physics and real-world rays, and the hits they return. |
| Coordinate space | Convert points between the screen, the world and an object’s own space. |