Snap Drop Zone
Overview
This sets up a predefined zone where an existing interactable object can be dropped and upon dropping it snaps to the set snap drop zone transform position, rotation and scale.
The position, rotation and scale of the SnapDropZone
Game Object will be used to determine the final position of the dropped interactable object if it is dropped within the drop zone collider volume.
The provided Highlight Object Prefab is used to create the highlighting object (also within the Editor for easy placement) and by default the standard Material Color Swap highlighter is used.
An alternative highlighter can also be added to the SnapDropZone
Game Object and this new highlighter component will be used to show the interactable object position on release.
The prefab is a pre-built game object that contains a default trigger collider (Sphere Collider) and a kinematic rigidbody (to ensure collisions occur).
If an alternative collider is required, then the default Sphere Collider can be removed and another collider added.
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.
- 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 Object: The game object to snap into the dropzone when the drop zone is enabled. The game 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 toisKinematic = true
.UseJoint
- Will attach the interactable object's rigidbody to the provided joint as it'sConnected Body
.UseParenting
- Will set the SnapDropZone as the interactable object's parent and set it's rigidbody toisKinematic = 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 tofalse
- 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 game object 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.
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.
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.
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.
Updated less than a minute ago