XRShared Touch & Hover

This module of the XRShared addon handles touch interactions with the fingers, and touch and hover at a distance with the controller beams. For the interactions with Unity UI canvases, see "UI" in Interactions.

Fusion Touch and Hover
Fusion Touch and Hover

Touch and hover are detected on the local user hardware rig: their events are only triggered on the touching user's client, and are not synchronized. To share their result with the other users, the callbacks have to change a networked state (see Sharing a button status).

Former Touch Hover addon

The former standalone Touch Hover addon is now part of XRShared. Its UI components (TouchableCanvas, UITouchButton, TouchableSlider) have been removed: hands and controller beams now interact with UI canvases through the XSCInputModule (see "UI" in Interactions). TouchableTMPInputField is now part of the Virtual Keyboard addon.

Touch

Toucher

A Toucher detects the touchable objects (components implementing ITouchable) in contact with it, through Unity trigger events, and calls their OnToucherContactStart, OnToucherStay and OnToucherContactEnd methods. By default, the touchable component can be on a parent of the touched collider.

The Toucher prefab contains the toucher, a trigger sphere collider and a kinematic rigidbody. In the default rigs, it is placed on the index tip of the tracked hands (LeftHandTracking and RightHandTracking prefabs) and of the hands displayed with controllers (LeftControllerSimulatedHand and RightControllerSimulatedHand prefabs).

A toucher should be placed on the hardware rig: the remote users' rigs then trigger no collider events. If it is placed on a network rig instead, onlyTriggerCallbacksWhenStateAuthority ensures that only the rig of the local user touches objects.

Touchable

Touchable is a basic touchable object:

  • onTouchStart and onTouchEnd events are triggered when a toucher starts and stops touching it,
  • anti-bounce: a new touch is ignored during timeBetweenTouchTrigger (0.3 seconds by default) after the end of the previous one,
  • feedback: when touched, it plays a sound (audioType) and a haptic feedback on the controller or hand of the toucher (toucherHapticAmplitude, toucherHapticDuration).

The feedback is played by an IFeedbackHandler found in the parents of the object: XRShared's BasicFeedback, or the Feedback component of the Feedback addon, which supports a centralized audio source and sound list.

To test an object in the Editor, the "SimulateTouchStart", "SimulateTouchEnd" and "SimulateInstantTouch" context menus of the component trigger a touch without a toucher.

TouchableButton

TouchableButton is a button, with its type in buttonType:

  • PressButton: on while touched,
  • ToggleButton: each touch switches its status,
  • RadioButton: touching it switches it on, and switches off the other buttons of its group (radioGroupButtons). If no group is set, the group is made of the sibling radio buttons, under the same parent. isRadioGroupDefaultButton switches a button on at start.

To avoid pressing two neighbor buttons with one finger, a button cannot be touched while a sibling button is touched, or shortly after (doNotallowTouchIfSiblingTouched, doNotallowTouchIfSiblingWasRecentlyTouched). By default, the siblings are the TouchableButton objects under the same parent (automaticallyDetectSiblings).

Events:

  • onButtonTouchStart and onButtonTouchEnd, when a toucher starts and stops touching the button,
  • onStatusChanged (with the new status), onOnStatus and onOffStatus, when the button status changes.

The button can also change its look with its status:

  • touchMaterial is applied to its MeshRenderer while the button is on,
  • primaryIcon and secondaryIcon: for a toggle or radio button, the primary icon is displayed while the button is on, and the secondary one while it is off. For a press button, the secondary icon replaces the primary one while the button is pressed.

The Touchable Button prefab (Runtime/Interactions/Touch/Prefabs) provides a ready to use button.

TouchableButton also implements IRegisterableTouchable: other components can register an ITouchableListener to receive its contact events, like the Watch of the Watch Menu addon.

Sharing a button status

As the touch events are local, a button shared by all users needs a networked state. For instance:

  • in the onStatusChanged callback, the touching user stores the new status in a networked property, after taking the state authority of its NetworkObject if needed,
  • when this networked property changes, every user calls ChangeButtonStatus on the button, so that its status, material and icons follow the shared state. For a toggle or radio button, ChangeButtonStatus only triggers the status events if the status actually changes, so applying the received state does not loop.

Custom touchable objects

Any component implementing ITouchable can be touched. Note that the toucher parameter of its methods is null for touches that do not come from a Toucher, like a beam touch or a touch simulated in the Editor. The visionOS Helpers addon SpatialTouchable is an example.

Touching setup for other rigs

For a hardware rig not built from the XRShared prefabs, a TouchingSetup component on the rig adds the touchers at runtime: for each hand or controller rig part, it adds a Toucher on the index tip when it finds one (the hand index tip follower, or an interaction tip like the IndexTipMarker), or else on the rig part, with a kinematic rigidbody. When no usable collider exists, it creates a default trigger collider, and logs a warning suggesting to create one in the scene with the desired position. See "Interaction setups for other rigs" in Rig Setup.

Beam touch and hover

The controller beams (RayBeamer, see Locomotion) can also touch and hover objects at a distance. BeamToucher and BeamHoverer listen to the events of the RayBeamer found in their children: they are placed on the controller, above the beamer. The default HardwareRig does not include them.

BeamToucher

When the beam hits an object with an ITouchable in its parents and the trigger is pressed (useAction), BeamToucher simulates an instant touch on it: a contact start, then a contact end, with a haptic feedback on the controller. With touchMode, the touch can happen when the trigger is pressed (default) or released.

BeamHoverer and BeamHoverable

BeamHoverer warns the IBeamHoverListener found on the collider hit by the beam (or its children) when the beam starts and stops hovering it, and when the beam is released while hovering it.

BeamHoverable is the default hover listener:

  • while hovered, it activates the onHoverVisualFeedback object and applies onHoverableMaterial to its renderer,
  • when the hover starts, it plays an audio and haptic feedback (audioFeedbackType), with an IFeedbackHandler found in its parents,
  • onBeamHoverStart, onBeamHoverEnd and onBeamRelease events. onBeamRelease is triggered when the user releases the beam while it hovers the object: it can be used to select an object by pointing at it.

To receive the hover events in another component, implement IBeamHoverListener.

Demo

The "BeamerTouch" sample (Samples/Interactions_Beamer/Scenes/Beamer.unity) of the XRShared addon adds a BeamToucher and a BeamHoverer on the controllers of its hardware rig and desktop rig. Its touchable cubes (a press button and a toggle button) and its hoverable cube can be used with the fingers and the beams.

Download

Touch & Hover is part of the XRShared addon, included in the XR addons project: see the Download page.

Changelog

Touch & Hover is now part of the XRShared addon: see the XRShared changelog.

Former standalone addon versions:

  • Version 2.1.2: Small fix in Touchable to manage object deletion
  • Version 2.1.1: Remove dependency in Demo prefab
  • Version 2.1.0: Move toucher to XRShared addon
  • Version 2.0.0: First release

Last updated on

Back to top