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

VRTK_InteractGrab

Overview

The Interact Grab script is attached to a Controller object within the [CameraRig] prefab and the Controller object 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

  • Controller Attach Point: The rigidbody point on the controller model to snap the grabbed object to (defaults to the tip).
  • Hide Controller On Grab: Hides the controller model when a valid grab occurs.
  • Hide Controller Delay: The amount of seconds to wait before hiding the controller on grab.
  • 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 [CameraRig] 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.

Class Events

  • ControllerGrabInteractableObject - Emitted when a valid object is grabbed.
  • 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.

  • OnControllerGrabInteractableObject - Emits the ControllerGrabInteractableObject class event.
  • OnControllerUngrabInteractableObject - Emits the ControllerUngrabInteractableObject class event.

Event Payload

  • uint controllerIndex - The index of the controller doing the interaction.
  • GameObject target - The GameObject of the interactable object that is being interacted with by the controller.

Class Methods

ForceRelease/0

public void ForceRelease()

  • Parameters
  • none
  • Returns
  • none

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

AttemptGrab/0

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