Guides
These docs are for v3.2.0. Click to read the latest docs for v3.3.0.

VRTK_InteractGrab

Overview

The Interact Grab script is attached to a Controller object and requires the VRTK_ControllerEvents script to be attached as it uses this for listening to the controller button events for grabbing and releasing interactable game objects.

It listens for the AliasGrabOn and AliasGrabOff events to determine when an object should be grabbed and should be released.

The Controller object also requires the VRTK_InteractTouch script to be attached to it as this is used to determine when an interactable object is being touched. Only valid touched objects can be grabbed.

An object can be grabbed if the Controller touches a game object which contains the VRTK_InteractableObject script and has the flag isGrabbable set to true.

If a valid interactable object is grabbable then pressing the set Grab button on the Controller (default is Grip) will grab and snap the object to the controller and will not release it until the Grab button is released.

When the Controller Grab button is released, if the interactable game object is grabbable then it will be propelled in the direction and at the velocity the controller was at, which can simulate object throwing.

The interactable objects require a collider to activate the trigger and a rigidbody to pick them up and move them around the game world.

Inspector Parameters

  • Grab Button: The button used to grab/release a touched object.
  • Grab Precognition: An amount of time between when the grab button is pressed to when the controller is touching something to grab it. For example, if an object is falling at a fast rate, then it is very hard to press the grab button in time to catch the object due to human reaction times. A higher number here will mean the grab button can be pressed before the controller touches the object and when the collision takes place, if the grab button is still being held down then the grab action will be successful.
  • Throw Multiplier: An amount to multiply the velocity of any objects being thrown. This can be useful when scaling up the play area to simulate being able to throw items further.
  • Create Rigid Body When Not Touching: If this is checked and the controller is not touching an Interactable Object when the grab button is pressed then a rigid body is added to the controller to allow the controller to push other rigid body objects around.
  • Controller Attach Point: The rigidbody point on the controller model to snap the grabbed object to. If blank it will be set to the SDK default.
  • Controller Events: The controller to listen for the events on. If the script is being applied onto a controller then this parameter can be left blank as it will be auto populated by the controller the script is on at runtime.
  • Interact Touch: The Interact Touch to listen for touches on. If the script is being applied onto a controller then this parameter can be left blank as it will be auto populated by the controller the script is on at runtime.

Class Events

  • GrabButtonPressed - Emitted when the grab button is pressed.
  • GrabButtonReleased - Emitted when the grab button is released.
  • ControllerStartGrabInteractableObject - Emitted when a grab of a valid object is started.
  • ControllerGrabInteractableObject - Emitted when a valid object is grabbed.
  • ControllerStartUngrabInteractableObject - Emitted when a ungrab of a valid object is started.
  • ControllerUngrabInteractableObject - Emitted when a valid object is released from being grabbed.

Unity Events

Adding the VRTK_InteractGrab_UnityEvents component to VRTK_InteractGrab object allows access to UnityEvents that will react identically to the Class Events.

  • All C# delegate events are mapped to a Unity Event with the On prefix. e.g. MyEvent -> OnMyEvent.

Class Methods

IsGrabButtonPressed/0

public virtual bool IsGrabButtonPressed()

  • Parameters
  • none
  • Returns
  • bool - Returns true if the grab alias button is being held down.

The IsGrabButtonPressed method determines whether the current grab alias button is being pressed down.

ForceRelease/1

public virtual void ForceRelease(bool applyGrabbingObjectVelocity = false)

  • Parameters
  • bool applyGrabbingObjectVelocity - If this is true then upon releasing the object any velocity on the grabbing object will be applied to the object to essentiall throw it. Defaults to false.
  • Returns
  • none

The ForceRelease method will force the controller to stop grabbing the currently grabbed object.

AttemptGrab/0

public virtual void AttemptGrab()

  • Parameters
  • none
  • Returns
  • none

The AttemptGrab method will attempt to grab the currently touched object without needing to press the grab button on the controller.

GetGrabbedObject/0

public virtual GameObject GetGrabbedObject()

  • Parameters
  • none
  • Returns
  • GameObject - The game object of what is currently being grabbed by this controller.

The GetGrabbedObject method returns the current object being grabbed by the controller.

Example

VRTK/Examples/005_Controller/BasicObjectGrabbing demonstrates the grabbing of interactable objects that have the VRTK_InteractableObject script attached to them. The objects can be picked up and thrown around.

VRTK/Examples/013_Controller_UsingAndGrabbingMultipleObjects demonstrates that each controller can grab and use objects independently and objects can also be toggled to their use state simultaneously.

VRTK/Examples/014_Controller_SnappingObjectsOnGrab demonstrates the different mechanisms for snapping a grabbed object to the controller.