XRShared Physics Grabbing

Physics grabbing lets users grab objects that keep colliding while they are held. The default grabbing of the XRShared addon makes a held object kinematic and places it on the hand, so it goes through walls. Physics grabbing keeps it a dynamic rigidbody and moves it toward the hand at each physics step, so the physics engine can still stop it, push it back or turn it. A held object stops against a wall or against another user's sword, and the displayed hand stays on it. Keep the default grabbing for objects that do not need to collide while held.

Physics grabbing is a module of XRShared (Runtime/Interactions/PhysicsGrabbing/), and requires Fusion 2.1 or newer, with forecast physics.

Overview

What users see and feel

  • The object follows the hand from the point where it was grabbed (the grab point), and collides with everything on the way.
  • When something blocks it, the object turns around the hand, as it would in a real hand.
  • The displayed hand stays on the object. If a wall stops the object, the hand stops with it, on every user's screen, while the real hand goes on.
  • While the object presses on something, the controller vibrates, more as the real hand moves away. The blocked hand and the vibration feel like a resistance (pseudo-haptic feedback). Tracked hands get no vibration.
  • A translucent ghost of the object shows the holder where the real hand holds a blocked object.
  • An object is held by one hand at a time, and another user can take it from you. Passing it to your own other hand does not work yet: the object drops when the first hand opens.
  • On release, the object keeps its velocity, so users can throw it, and an object released from a still hand stays still (see Release and inertia).

Choosing a follow: force or velocity

A follow moves the held object toward the hand at each physics step. The choice is between a physical object and a controlled one:

  • The force follow (ForcePhysicsGrabbable) pulls the object with springs of limited strength, so an obstacle can push it back, heavier objects feel heavier, and the grip can fail. Use it by default for physical objects: swords, heavy objects, fights between users.
  • The velocity follow (VelocityPhysicsGrabbable) sets the object's velocities toward the hand, so it follows the hand the same way whatever its mass, and the hand always wins. Use it for tools such as pens, for gameplay where the hand must always win, and for objects whose own mass distribution should shape their hits (a hammer).

Give objects that fight each other the same follow, as a velocity-held object always wins against a force-held one. Compare the follows in the DemoForceGrabbing (force) and DemoPhysicsGrabbing (velocity) scenes, in Samples/PhysicsGrabbing/Scenes/.

Setup

To make an object physics grabbable, put these components on its root GameObject:

  1. A NetworkObject with Allow State Authority Override checked, and Destroy When State Authority Leaves and Is Master Client Object unchecked.
  2. A NetworkTransform (not the Physics addon's NetworkRigidbody3D) with forecast physics enabled. Physics Forecast must also be on in the NetworkProjectConfig (it is by default).
  3. A Rigidbody, not kinematic, with Interpolate and Continuous Speculative collision detection. Its colliders can be on children, and must not be triggers.
  4. The components of one follow: NetworkForcePhysicsGrabbable and ForcePhysicsGrabbable, or NetworkVelocityPhysicsGrabbable and VelocityPhysicsGrabbable (the network component adds its partner if it is missing). The object must have a single Grabbable (a second one is logged as an error).
  5. CollisionAuthorityTransfer, on this object and on the free objects it should push. It is optional, but recommended: it makes collisions with other networked objects feel natural (see Pushing free objects).
  6. Optionally HeldObjectGhost, with its ghostMaterial set (see The ghost).

The project's Physics settings must use the FixedUpdate simulation mode (Unity's default). Their Default Max Angular Speed does not limit the follow: while an object is held, its Rigidbody limit is raised if it is too low. It still caps the spin of released and thrown objects (7 rad/s in projects created before Unity 2019.1: raise it if throws spin too slowly). The rig needs grabbers on its hands and controllers, as for any grabbing (the default HardwareRig, or GrabbingSetup, see Rig Setup).

Rigidbody and colliders

Starting points for the Rigidbody and its colliders:

  • Continuous Speculative collision detection is the only mode that accounts for rotation (Unity's speculative CCD).
  • For objects pressed against other held objects (a sword, a bat), use colliders about 5 cm thick or more, as thinner ones can go through each other (at high speed, for instance).
  • Without gravity, set the linear and angular damping to about 1.5, so that released objects come to rest. With gravity, keep it much lower, or dropped objects fall slowly.
  • If you want physics objects to glide against each other, give them a physics material with a friction of 0.1 and the Average combine mode.

How it works

Why the object turns around the hand

The physics engine does not know about the hand. It treats a held object as a free object, and a free object that is hit turns around its center of mass. A long object held at one end would then swing its handle, and the displayed hand with it:

Plain Old Text

Center of mass (o) in the middle:

    [hand]=========o=========| obstacle
    the object turns around o: the handle, and the displayed hand, swing the other way

Center of mass at the grab point:

    [hand]o==================| obstacle
    the object turns around the hand: the hand stays, the object yields

So while the object is held, its center of mass is moved to the grab point (always with the force follow, by default with the velocity follow), and its inertia is made the same around every axis (sameInertiaOnAllAxesWhileGrabbed), so that it resists turning alike in every direction, as in a hand. Both are restored on release, without changing the throw.

The displayed hand and the vibration

While the object is held, every client draws the holder's hand at the pose that holds the object where the physics left it, not at its tracked pose (the physics grabbable is a position modifier of the hand, see Position modifiers). The follow itself reads the tracked pose, or the object would chase itself.

While the object touches something, the hand's NetworkRigPart vibrates the controller in proportion to the distance between the real hand and the displayed one. Its settings are on the network rig's NetworkRigPart: positionOffsetForMaxHapticFeedback (0.3 m) is the distance for the full amplitude (the force follow changes it, see its grip failure), positionOffsetHapticAmplitude sets the strength, and turning off addHapticFeedbackOnPositionModifications removes the vibration.

Networking: what runs where

  • The state authority (the user holding the object) moves the real object. The other clients run the same follow toward their view of the holder's hand, and the NetworkTransform corrects their copy toward the holder's state.
  • The follow runs on every client in Unity's FixedUpdate, where forecast physics simulates. Its inputs, the grabber and the grab offsets, are networked, and every client derives the center of mass and inertia changes from them.
  • Every client reads the settings at each physics step. Keep them the same on every client (prefab values are), and network the ones that change at runtime, as the Line Drawing addon's NetworkPhysicsDrawingSettings does for its force drawings.
  • The ghost, collision sounds and haptics are local. The force follow has more local state (see Force Grabbing).

To write your own follow, derive from NetworkRigidbodyGrabbable, the base class of both network components, and implement PhysicsFollow: its class header describes what to implement.

Pushing free objects

A free networked object follows its state authority's simulation, often another client's, so it moves by jerks for the user pushing it. CollisionAuthorityTransfer, on the held object and on the free objects it should push, makes the pusher's client take the state authority of the free objects its held object hits. One client then simulates both, and the push is smooth. A pushed object that is not grabbable needs setup steps 1 to 3 and CollisionAuthorityTransfer. Use this component rather than the older SharedPhysicsAuthorityTransfer, made for kinematic grabbing.

The ghost

HeldObjectGhost draws a translucent copy of the object at the pose the hand would give it, fading in as the gap grows. Only the holder sees it, in every camera of their client (a spectator camera, a video stream). The addon does not draw a ghost of the hand.

The ghost copies the object's meshes and lines, without texts or skinned meshes. Add GhostExcluded to leave other parts out, or implement IGhostVisual to draw your own visual. Its default thresholds suit the force follow, where it also turns red near the grip limits. The velocity follow lags behind a moving hand, so raise them there (Velocity Grabbing).

Settings

Release and inertia

These settings are on both ForcePhysicsGrabbable and VelocityPhysicsGrabbable. Opening the hand or letting go of the grip button moves the hand a little, which would make a long object swing away from a still hand: slow release velocities are scaled down, removed below the still speeds and kept above the throw speeds.

Field Default What it does
sameInertiaOnAllAxesWhileGrabbed on Makes the held object resist turning alike in every direction (why). Turn it off when the object's mass distribution matters
scaleSlowReleaseVelocities on Scales slow release velocities down. Turned off, every release keeps its velocity
stillReleaseSpeed, stillReleaseAngularSpeed 0.05 m/s, 45°/s Below these, the object is released still. Raise them if objects drift after a still release
throwReleaseSpeed, throwReleaseAngularSpeed 0.3 m/s, 180°/s Above these, the velocity is kept. Lower them if gentle tosses stop dead
logReleaseVelocities off Logs the speeds at each release, to tune the four fields above

Ghost (HeldObjectGhost)

Field Default What it does
ghostMaterial none, to set The ghost's material. Set it to the provided Runtime/Interactions/PhysicsGrabbing/Materials/HeldObjectGhost.mat (URP). Your own transparent material also works if it has a _BaseColor: its alpha is the ghost's alpha at full visibility
useDefaultVisuals on Draws the copies of the meshes and lines. Turn it off when your IGhostVisual components draw them
showDistance, fullDistance 0.02 m, 0.1 m Gap at which the ghost starts to appear, and is fully visible
showAngle, fullAngle 5°, 30° The same for the angle
warningColor, warningStart red, 0.5 Force follow only: the color blended in from warningStart (a fraction of the grip limits) up to the limit, and during a stagger and its recovery

Troubleshooting

Setup problems are logged at startup, at spawn, or at the first grab. Each follow's page has its own rows: force, velocity.

Symptom What to check
The object does not follow the hand Not spawned, kinematic Rigidbody, forecast physics disabled, or physics not simulated in FixedUpdate (each is logged)
Grabbing does nothing A second Grabbable left on the object (a plain one, or the other follow's), picked by the grabber (logged as an error)
A held object goes through a wall or another held object Discrete collision detection, no solid collider, or a layer that does not collide with itself (each logged at the first grab); or colliders thinner than about 5 cm (Rigidbody and colliders)
A held object does not push back another held object Both need physics grabbing: an object held with a plain Grabbable is kinematic
Pushing a free object is jerky for the user pushing it CollisionAuthorityTransfer missing on one of the two objects
My own collision effect plays while the object is shaken in the air Continuous Speculative reports contacts before the colliders touch. Ignore them with collision.IsTouching(maxSeparation) (CollisionExtensions)
A center of mass or inertia my script sets is lost after a grab The follows replace them while the object is held: set them outside a grab, or turn off sameInertiaOnAllAxesWhileGrabbed (and the velocity follow's centerOfMassAtGrabPoint)
A released object drifts forever, or falls too slowly Damping too low without gravity, or too high with gravity (Rigidbody and colliders)
An object released from a still hand starts turning, or a gentle toss stops dead Raise the still speeds, or lower the throw speeds (Release and inertia)
The ghost never shows No ghostMaterial, or HeldObjectGhost is not next to the network component (both logged)
The ghost is magenta, or does not fade nor turn red HeldObjectGhost.mat is a URP material: on another render pipeline, use a transparent material with a _BaseColor property
Over Quest Link, hands flicker between two poses in the Editor An Editor effect over Link: judge the feel on a device build

Last updated on

Back to top