Guides
Guides

Snap Drop Zone

Overview

Provides a predefined zone where a valid interactable object can be dropped and upon dropping it snaps to the set snap drop zone transform position, rotation and scale.

Prefab Usage:

  • Place the VRTK/Prefabs/SnapDropZone/SnapDropZone prefab into the scene hierarchy.
  • Provide the SnapDropZone with an optional Highlight Object Prefab to generate an object outline in the scene that determines the final position, rotation and scale of the snapped object.
  • If no VRTK_BaseHighlighter derivative is applied to the SnapDropZone then the default MaterialColorSwap Highlighter will be used.
  • The collision zone that activates the SnapDropZone is a Sphere Collider by default but can be amended or replaced on the SnapDropZone GameObject.
  • If the Use Joint Snap Type is selected then a custom Joint component is required to be added to the SnapDropZone Game Object and upon release the interactable object's rigidbody will be linked to this joint as the Connected Body.

Inspector Parameters

  • Highlight Object Prefab: A game object that is used to draw the highlighted destination for within the drop zone. This object will also be created in the Editor for easy placement.
  • Snap Type: The Snap Type to apply when a valid interactable object is dropped within the snap zone.
  • Snap Duration: The amount of time it takes for the object being snapped to move into the new snapped position, rotation and scale.
  • Apply Scaling On Snap: If this is checked then the scaled size of the snap drop zone will be applied to the object that is snapped to it.
  • Clone New On Unsnap: If this is checked then when the snapped object is unsnapped from the drop zone, a clone of the unsnapped object will be snapped back into the drop zone.
  • Highlight Color: The colour to use when showing the snap zone is active. This is used as the highlight colour when no object is hovering but Highlight Always Active is true.
  • Valid Highlight Color: The colour to use when showing the snap zone is active and a valid object is hovering. If this is Color.clear then the Highlight Color will be used.
  • Highlight Always Active: The highlight object will always be displayed when the snap drop zone is available even if a valid item isn't being hovered over.
  • Valid Object List Policy: A specified VRTK_PolicyList to use to determine which interactable objects will be snapped to the snap drop zone on release.
  • Display Drop Zone In Editor: If this is checked then the drop zone highlight section will be displayed in the scene editor window.
  • Default Snapped Interactable Object: The Interactable Object to snap into the dropzone when the drop zone is enabled. The Interactable Object must be valid in any given policy list to snap.

Class Variables

  • public enum SnapTypes - The types of snap on release available.
    • UseKinematic - Will set the interactable object rigidbody to isKinematic = true.
    • UseJoint - Will attach the interactable object's rigidbody to the provided joint as it's Connected Body.
    • UseParenting - Will set the SnapDropZone as the interactable object's parent and set it's rigidbody to isKinematic = true.

Class Events

  • ObjectEnteredSnapDropZone - Emitted when a valid interactable object enters the snap drop zone trigger collider.
  • ObjectExitedSnapDropZone - Emitted when a valid interactable object exists the snap drop zone trigger collider.
  • ObjectSnappedToDropZone - Emitted when an interactable object is successfully snapped into a drop zone.
  • ObjectUnsnappedFromDropZone - Emitted when an interactable object is removed from a snapped drop zone.

Unity Events

Adding the VRTK_SnapDropZone_UnityEvents component to VRTK_SnapDropZone 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.

Event Payload

  • GameObject snappedObject - The interactable object that is dealing with the snap drop zone.

Class Methods

InitaliseHighlightObject/1

public virtual void InitaliseHighlightObject(bool removeOldObject = false)

  • Parameters
    • bool removeOldObject - If this is set to true then it attempts to delete the old highlight object if it exists. Defaults to false
  • Returns
    • none

The InitaliseHighlightObject method sets up the highlight object based on the given Highlight Object Prefab.

ForceSnap/1

public virtual void ForceSnap(GameObject objectToSnap)

  • Parameters
    • GameObject objectToSnap - The GameObject to attempt to snap.
  • Returns
    • none

the ForceSnap method attempts to automatically attach a valid GameObject to the snap drop zone.

ForceUnsnap/0

public virtual void ForceUnsnap()

  • Parameters
    • none
  • Returns
    • none

The ForceUnsnap method attempts to automatically remove the current snapped game object from the snap drop zone.

ValidSnappableObjectIsHovering/0

public virtual bool ValidSnappableObjectIsHovering()

  • Parameters
    • none
  • Returns
    • bool - Returns true if a valid object is currently in the snap drop zone area.

The ValidSnappableObjectIsHovering method determines if any valid objects are currently hovering in the snap drop zone area.

IsObjectHovering/1

public virtual bool IsObjectHovering(GameObject checkObject)

  • Parameters
    • GameObject checkObject - The GameObject to check to see if it's hovering in the snap drop zone area.
  • Returns
    • bool - Returns true if the given GameObject is hovering (but not snapped) in the snap drop zone area.

The IsObjectHovering method determines if the given GameObject is currently howvering (but not snapped) in the snap drop zone area.

IsInteractableObjectHovering/1

public virtual bool IsInteractableObjectHovering(VRTK_InteractableObject checkObject)

  • Parameters
    • VRTK_InteractableObject checkObject - The Interactable Object script to check to see if it's hovering in the snap drop zone area.
  • Returns
    • bool - Returns true if the given Interactable Object script is hovering (but not snapped) in the snap drop zone area.

The IsInteractableObjectHovering method determines if the given Interactable Object script is currently howvering (but not snapped) in the snap drop zone area.

GetHoveringObjects/0

public virtual List<GameObject> GetHoveringObjects()

  • Parameters
    • none
  • Returns
    • List<GameObject> - The List of valid GameObjects that are hovering (but not snapped) in the snap drop zone area.

The GetHoveringObjects method returns a List of valid GameObjects that are currently hovering (but not snapped) in the snap drop zone area.

GetHoveringInteractableObjects/0

public virtual List<VRTK_InteractableObject> GetHoveringInteractableObjects()

  • Parameters
    • none
  • Returns
    • List<VRTK_InteractableObject> - The List of valid Interactable Object scripts that are hovering (but not snapped) in the snap drop zone area.

The GetHoveringInteractableObjects method returns a List of valid Interactable Object scripts that are currently hovering (but not snapped) in the snap drop zone area.

GetCurrentSnappedObject/0

public virtual GameObject GetCurrentSnappedObject()

  • Parameters
    • none
  • Returns
    • GameObject - The GameObject that is currently snapped in the snap drop zone area.

The GetCurrentSnappedObejct method returns the GameObject that is currently snapped in the snap drop zone area.

GetCurrentSnappedInteractableObject/0

public virtual VRTK_InteractableObject GetCurrentSnappedInteractableObject()

  • Parameters
    • none
  • Returns
    • VRTK_InteractableObject - The Interactable Object script that is currently snapped in the snap drop zone area.

The GetCurrentSnappedInteractableObject method returns the Interactable Object script that is currently snapped in the snap drop zone area.

Clone/1

public virtual GameObject Clone(Vector3 position)

  • Parameters
    • Vector3 position - Position of the cloned GameObject
  • Returns
    • GameObject - The GameObject of the clone

The Clone method returns the GameObject of the cloned snap drop zone

Clone/0

public virtual GameObject Clone()

  • Parameters
    • none
  • Returns
    • GameObject - The GameObject of the clone

The Clone method returns the GameObject of the cloned snap drop zone

Example

VRTK/Examples/041_Controller_ObjectSnappingToDropZones uses the VRTK_SnapDropZone prefab to set up pre-determined snap zones for a range of objects and demonstrates how only objects of certain types can be snapped into certain areas.