Guides

VRTK_ObjectAutoGrab

Overview

Attempt to automatically grab a specified Interactable Object.

Required Components:

  • VRTK_InteractTouch - The touch component to determine when a valid touch has taken place to denote a use interaction can occur. This must be applied on the same GameObject as this script if one is not provided via the Interact Touch parameter.
  • VRTK_InteractGrab - The grab component to determine when a valid grab has taken place. This must be applied on the same GameObject as this script if one is not provided via the Interact Grab parameter.

Script Usage:

  • Place the VRTK_ObjectAutoGrab script on either:
    • The GameObject that contains the Interact Touch and Interact Grab scripts.
    • Any other scene GameObject and provide a valid VRTK_InteractTouch component to the Interact Touch parameter and a valid VRTK_InteractGrab component to the Interact Grab parameter of this script.
  • Assign the Interactable Object to auto grab to the Object To Grab parameter on this script.
  • If this Interactable Object is a prefab then the Object Is Prefab parameter on this script must be checked.

Inspector Parameters

  • Object To Grab: The Interactable Object that will be grabbed by the Interact Grab.
  • Object Is Prefab: If the Object To Grab is a prefab then this needs to be checked, if the Object To Grab already exists in the scene then this needs to be unchecked.
  • Clone Grabbed Object: If this is checked then the Object To Grab will be cloned into a new Interactable Object and grabbed by the Interact Grab leaving the existing Interactable Object in the scene. This is required if the same Interactable Object is to be grabbed to multiple instances of Interact Grab. It is also required to clone a grabbed Interactable Object if it is a prefab as it needs to exist within the scene to be grabbed.
  • Always Clone On Enable: If Clone Grabbed Object is checked and this is checked, then whenever this script is disabled and re-enabled, it will always create a new clone of the Interactable Object to grab. If this is unchecked then the original cloned Interactable Object will attempt to be grabbed again. If the original cloned object no longer exists then a new clone will be created.
  • Attempt Secondary Grab: If this is checked then the Object To Grab will attempt to be secondary grabbed as well as primary grabbed.
  • 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.
  • Interact Grab: The Interact Grab to listen for grab actions 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.
  • Secondary Interact Touch: The secondary controller Interact Touch to listen for touches on. If this field is left blank then it will be looked up on the opposite controller script alias at runtime.
  • Secondary Interact Grab: The secondary controller Interact Grab to listen for grab actions on. If this field is left blank then it will be looked up on the opposite controller script alias at runtime.

Class Events

  • ObjectAutoGrabCompleted - Emitted when the object auto grab has completed successfully.

Unity Events

Adding the VRTK_ObjectAutoGrab_UnityEvents component to VRTK_ObjectAutoGrab 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

ClearPreviousClone/0

public virtual void ClearPreviousClone()

  • Parameters
    • none
  • Returns
    • none

The ClearPreviousClone method resets the previous cloned Interactable Object to null to ensure when the script is re-enabled that a new cloned Interactable Object is created, rather than the original clone being grabbed again.

Example

VRTK/Examples/026_Controller_ForceHoldObject shows how to automatically grab a sword to each controller and also prevents the swords from being dropped so they are permanently attached to the user's controllers.