Skip to content

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.

  • 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 compares event.eventType against the EventType constants.

  • 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.

  1. 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.
  2. Events reach scripts through a Signal Emitter. Each interaction component sends events such as Selectable::select or Grabbable::end. A Signal Emitter on the entity emits them on its signal; your on(signal, …) listener receives { entityId, eventType, data }.
  3. 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.

This walkthrough makes a lamp that switches on and off each time the player clicks or taps it.

  1. Add a Primitive Mesh with a sphere shape and name it Lamp. Give it a Standard Material.

  2. On the lamp, add a Selectable component. Leave its props at their defaults, so it can be clicked with any pointer.

  3. Still on the lamp, add a Signal Emitter and set its signal to lamp. This is the name your script listens to.

  4. 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');
    });
    }
  5. 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; the eventType check keeps only selects.
  • setSelected(false) releases the lamp straight away, so every click selects it again and sends a new select.
  • emissiveIntensity above 0 makes the material glow in the emissive colour.
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 GraphicsRaycaster, PhysicsRaycaster, XRRaycaster Picking and raycasts
To convert between screen, world and local points CoordinateSpace Coordinate space
To read keys, sticks or sensors yourself Input Input

The Snap Zone is the interaction entity type, with a page of its components, props, events and a first script:

EntityTypeWhat it is
Snap Zoneent-snap-zoneA box that snaps media into a neat grid.
GlobalWhat it is
SelectableComponentTracks 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.
GrabbableComponentMakes 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).
PivotableComponentAttaches 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.
HandleComponentMakes 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.
SnapZoneComponentA 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.
SnappableComponentLets 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.
GraphicsRaycasterA 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.
PhysicsRaycasterA 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.
XRRaycasterA 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:
CoordinateSpaceConversions between the screen, the world, and an object's own space.
EventTypeEvery event type a script can hear, by who sends it: compare them rather than typing the strings.
ComponentTypeWhat it does
Grabbablecomp-grabbableLets the player pick up and drag the entity with mouse, ray or hand.
Handlecomp-handleDrag the entity to move, rotate or scale it within limits.
Pivotablecomp-pivotableAdds an interactive move/rotate/scale gizmo to the entity.
Selectablecomp-selectableMakes the entity selectable with a click or hand touch.
Snap Zonecomp-snap-zoneA box that snaps media into a neat grid.
Snappablecomp-snappableLets a media entity snap into Snap Zones.
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.