Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 71.49 KB | None | 0 0
  1. // Interactable Object|Interactables|35010
  2. namespace VRTK
  3. {
  4.     using UnityEngine;
  5.     using System.Collections;
  6.     using System.Collections.Generic;
  7.     using GrabAttachMechanics;
  8.     using SecondaryControllerGrabActions;
  9.  
  10.     /// <summary>
  11.     /// Event Payload
  12.     /// </summary>
  13.     /// <param name="interactingObject">The GameObject that is initiating the interaction (e.g. a controller).</param>
  14.     public struct InteractableObjectEventArgs
  15.     {
  16.         public GameObject interactingObject;
  17.     }
  18.  
  19.     /// <summary>
  20.     /// Event Payload
  21.     /// </summary>
  22.     /// <param name="sender">this object</param>
  23.     /// <param name="e"><see cref="InteractableObjectEventArgs"/></param>
  24.     public delegate void InteractableObjectEventHandler(object sender, InteractableObjectEventArgs e);
  25.  
  26.     /// <summary>
  27.     /// Determines if the GameObject can be interacted with.
  28.     /// </summary>
  29.     /// <remarks>
  30.     /// **Required Components:**
  31.     ///  * `Collider` - A Unity Collider to determine when an interaction has occured. Can be a compound collider set in child GameObjects.
  32.     ///
  33.     /// **Optional Components:**
  34.     ///  * `Rigidbody` - A Unity Rigidbody to allow the GameObject to be affected by the Unity Physics System (not required for Climbable Grab Attach Types).
  35.     ///  * `VRTK_BaseGrabAttach` - A Grab Attach mechanic for determining how the Interactable Object is grabbed by the primary interacting object.
  36.     ///  * `VRTK_BaseGrabAction` - A Grab Action mechanic for determining how to manipulate the Interactable Object when grabbed by the secondary interacting object.
  37.     ///
  38.     /// **Script Usage:**
  39.     ///  * Place the `VRTK_InteractableObject` script onto the GameObject that is to be interactable.
  40.     ///  * Alternatively, select the GameObject and use the `Window -> VRTK -> Setup Interactable Object` panel to set up quickly.
  41.     ///  * The optional Highlighter used by the Interactable Object will be selected in the following order:
  42.     ///    * The provided Base Highlighter in the `Object Highlighter` parameter.
  43.     ///    * If the above is not provided, then the first active Base Highlighter found on the Interactable Object GameObject will be used.
  44.     ///    * If the above is not found, then a Material Color Swap Highlighter will be created on the Interactable Object GameObject at runtime.
  45.     ///
  46.     /// **Script Dependencies:**
  47.     ///  * Interactions
  48.     ///    * To near touch an Interactable Object the Interact NearTouch script is required on a controller Script Alias GameObject.
  49.     ///    * To touch an Interactable Object the Interact NearTouch script is required on a controller Script Alias GameObject.
  50.     ///    * To grab an Interactable Object the Interact Grab script is required on a controller Script Alias GameObject.
  51.     ///    * To use an Interactable Object the Interact Use script is required on a controller Script Alias GameObject.
  52.     ///  * Highlighting
  53.     ///    * To highlight an Interactable Object on a given interaction then a valid Interact Object Highlighter script must be associated with the Interactable Object.
  54.     ///  * Appearance
  55.     ///    * To affect the appearance of an Interactable Object then a valid Interact Object Appearance script must be associated with the Interactable Object.
  56.     /// </remarks>
  57.     /// <example>
  58.     /// `VRTK/Examples/005_Controller_BasicObjectGrabbing` uses the `VRTK_InteractTouch` and `VRTK_InteractGrab` scripts on the controllers to show how an interactable object can be grabbed and snapped to the controller and thrown around the game world.
  59.     ///
  60.     /// `VRTK/Examples/013_Controller_UsingAndGrabbingMultipleObjects` shows multiple objects that can be grabbed by holding the buttons or grabbed by toggling the button click and also has objects that can have their Using state toggled to show how multiple items can be turned on at the same time.
  61.     /// </example>
  62.     [AddComponentMenu("VRTK/Scripts/Interactions/Interactables/VRTK_InteractableObject")]
  63.     public class VRTK_InteractableObject : MonoBehaviour
  64.     {
  65.         /// <summary>
  66.         /// The interaction type.
  67.         /// </summary>
  68.         public enum InteractionType
  69.         {
  70.             /// <summary>
  71.             /// No interaction is affecting the object.
  72.             /// </summary>
  73.             None,
  74.             /// <summary>
  75.             /// The near touch interaction is affecting the object.
  76.             /// </summary>
  77.             NearTouch,
  78.             /// <summary>
  79.             /// The near untouch interaction stopped affecting the object
  80.             /// </summary>
  81.             NearUntouch,
  82.             /// <summary>
  83.             /// The touch interaction is affecting the object.
  84.             /// </summary>
  85.             Touch,
  86.             /// <summary>
  87.             /// The untouch interaction stopped affecting the object
  88.             /// </summary>
  89.             Untouch,
  90.             /// <summary>
  91.             /// The grab interaction is affecting the object.
  92.             /// </summary>
  93.             Grab,
  94.             /// <summary>
  95.             /// The ungrab interaction stopped affecting the object
  96.             /// </summary>
  97.             Ungrab,
  98.             /// <summary>
  99.             /// The use interaction is affecting the object.
  100.             /// </summary>
  101.             Use,
  102.             /// <summary>
  103.             /// The unuse interaction stopped affecting the object
  104.             /// </summary>
  105.             Unuse
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Allowed controller type.
  110.         /// </summary>
  111.         public enum AllowedController
  112.         {
  113.             /// <summary>
  114.             /// Both controllers are allowed to interact.
  115.             /// </summary>
  116.             Both,
  117.             /// <summary>
  118.             /// Only the left controller is allowed to interact.
  119.             /// </summary>
  120.             LeftOnly,
  121.             /// <summary>
  122.             /// Only the right controller is allowed to interact.
  123.             /// </summary>
  124.             RightOnly
  125.         }
  126.  
  127.         /// <summary>
  128.         /// The types of valid situations that the object can be released from grab.
  129.         /// </summary>
  130.         public enum ValidDropTypes
  131.         {
  132.             /// <summary>
  133.             /// The object cannot be dropped via the controller.
  134.             /// </summary>
  135.             NoDrop,
  136.             /// <summary>
  137.             /// The object can be dropped anywhere in the scene via the controller.
  138.             /// </summary>
  139.             DropAnywhere,
  140.             /// <summary>
  141.             /// The object can only be dropped when it is hovering over a valid snap drop zone.
  142.             /// </summary>
  143.             DropValidSnapDropZone
  144.         }
  145.  
  146.         [Header("General Settings")]
  147.  
  148.         [Tooltip("If this is checked then the Interactable Object component will be disabled when the Interactable Object is not being interacted with.")]
  149.         public bool disableWhenIdle = true;
  150.  
  151.         [Header("Near Touch Settings")]
  152.  
  153.         [Tooltip("Determines which controller can initiate a near touch action.")]
  154.         public AllowedController allowedNearTouchControllers = AllowedController.Both;
  155.  
  156.         [Header("Touch Settings")]
  157.  
  158.         [Tooltip("Determines which controller can initiate a touch action.")]
  159.         public AllowedController allowedTouchControllers = AllowedController.Both;
  160.         [Tooltip("An array of colliders on the GameObject to ignore when being touched.")]
  161.         public Collider[] ignoredColliders;
  162.  
  163.         [Header("Grab Settings")]
  164.  
  165.         [Tooltip("Determines if the Interactable Object can be grabbed.")]
  166.         public bool isGrabbable = false;
  167.         [Tooltip("If this is checked then the grab button on the controller needs to be continually held down to keep grabbing. If this is unchecked the grab button toggles the grab action with one button press to grab and another to release.")]
  168.         public bool holdButtonToGrab = true;
  169.         [Tooltip("If this is checked then the Interactable Object will stay grabbed to the controller when a teleport occurs. If it is unchecked then the Interactable Object will be released when a teleport occurs.")]
  170.         public bool stayGrabbedOnTeleport = true;
  171.         [Tooltip("Determines in what situation the Interactable Object can be dropped by the controller grab button.")]
  172.         public ValidDropTypes validDrop = ValidDropTypes.DropAnywhere;
  173.         [Tooltip("Setting to a button will ensure the override button is used to grab this specific Interactable Object. Setting to `Undefined` will mean the `Grab Button` on the Interact Grab script will grab the object.")]
  174.         public VRTK_ControllerEvents.ButtonAlias grabOverrideButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  175.         [Tooltip("Determines which controller can initiate a grab action.")]
  176.         public AllowedController allowedGrabControllers = AllowedController.Both;
  177.         [Tooltip("This determines how the grabbed Interactable Object will be attached to the controller when it is grabbed. If one isn't provided then the first Grab Attach script on the GameObject will be used, if one is not found and the object is grabbable then a Fixed Joint Grab Attach script will be created at runtime.")]
  178.         public VRTK_BaseGrabAttach grabAttachMechanicScript;
  179.         [Tooltip("The script to utilise when processing the secondary controller action on a secondary grab attempt. If one isn't provided then the first Secondary Controller Grab Action script on the GameObject will be used, if one is not found then no action will be taken on secondary grab.")]
  180.         public VRTK_BaseGrabAction secondaryGrabActionScript;
  181.  
  182.         [Header("Use Settings")]
  183.  
  184.         [Tooltip("Determines if the Interactable Object can be used.")]
  185.         public bool isUsable = false;
  186.         [Tooltip("If this is checked then the use button on the controller needs to be continually held down to keep using. If this is unchecked the the use button toggles the use action with one button press to start using and another to stop using.")]
  187.         public bool holdButtonToUse = true;
  188.         [Tooltip("If this is checked the Interactable Object can be used only if it is currently being grabbed.")]
  189.         public bool useOnlyIfGrabbed = false;
  190.         [Tooltip("If this is checked then when a Pointer collides with the Interactable Object it will activate it's use action. If the the `Hold Button To Use` parameter is unchecked then whilst the Pointer is collising with the Interactable Object it will run the `Using` method. If `Hold Button To Use` is unchecked then the `Using` method will be run when the Pointer is deactivated. The Pointer will not emit the `Destination Set` event if it is affecting an Interactable Object with this setting checked as this prevents unwanted teleporting from happening when using an Interactable Object with a pointer.")]
  191.         public bool pointerActivatesUseAction = false;
  192.         [Tooltip("Setting to a button will ensure the override button is used to use this specific Interactable Object. Setting to `Undefined` will mean the `Use Button` on the Interact Use script will use the object.")]
  193.         public VRTK_ControllerEvents.ButtonAlias useOverrideButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  194.         [Tooltip("Determines which controller can initiate a use action.")]
  195.         public AllowedController allowedUseControllers = AllowedController.Both;
  196.  
  197.         [Header("Obsolete Settings")]
  198.  
  199.         [System.Obsolete("`VRTK_InteractableObject.objectHighlighter` has been replaced with `VRTK_InteractObjectHighlighter.objectHighlighter`. This parameter will be removed in a future version of VRTK.")]
  200.         [ObsoleteInspector]
  201.         public Highlighters.VRTK_BaseHighlighter objectHighlighter;
  202.         [System.Obsolete("`VRTK_InteractableObject.touchHighlightColor` has been replaced with `VRTK_InteractObjectHighlighter.touchHighlight`. This parameter will be removed in a future version of VRTK.")]
  203.         [ObsoleteInspector]
  204.         public Color touchHighlightColor = Color.clear;
  205.  
  206.         protected Rigidbody interactableRigidbody;
  207.         protected HashSet<GameObject> currentIgnoredColliders = new HashSet<GameObject>();
  208.         protected HashSet<GameObject> hoveredSnapObjects = new HashSet<GameObject>();
  209.         protected HashSet<GameObject> nearTouchingObjects = new HashSet<GameObject>();
  210.         protected HashSet<GameObject> touchingObjects = new HashSet<GameObject>();
  211.         protected List<GameObject> grabbingObjects = new List<GameObject>();
  212.         protected VRTK_InteractUse usingObject = null;
  213.         protected Transform trackPoint;
  214.         protected bool customTrackPoint = false;
  215.         protected Transform primaryControllerAttachPoint;
  216.         protected Transform secondaryControllerAttachPoint;
  217.         protected Transform previousParent;
  218.         protected bool previousKinematicState;
  219.         protected bool previousIsGrabbable;
  220.         protected bool forcedDropped;
  221.         protected bool forceDisabled;
  222.         protected bool hoveredOverSnapDropZone = false;
  223.         protected bool snappedInSnapDropZone = false;
  224.         protected VRTK_SnapDropZone storedSnapDropZone;
  225.         protected Vector3 previousLocalScale = Vector3.zero;
  226.         protected bool startDisabled = false;
  227.  
  228.         /// <summary>
  229.         /// Emitted when the Interactable Object script is enabled;
  230.         /// </summary>
  231.         public event InteractableObjectEventHandler InteractableObjectEnabled;
  232.         /// <summary>
  233.         /// Emitted when the Interactable Object script is disabled;
  234.         /// </summary>
  235.         public event InteractableObjectEventHandler InteractableObjectDisabled;
  236.         /// <summary>
  237.         /// Emitted when another interacting object near touches the current Interactable Object.
  238.         /// </summary>
  239.         public event InteractableObjectEventHandler InteractableObjectNearTouched;
  240.         /// <summary>
  241.         /// Emitted when the other interacting object stops near touching the current Interactable Object.
  242.         /// </summary>
  243.         public event InteractableObjectEventHandler InteractableObjectNearUntouched;
  244.         /// <summary>
  245.         /// Emitted when another interacting object touches the current Interactable Object.
  246.         /// </summary>
  247.         public event InteractableObjectEventHandler InteractableObjectTouched;
  248.         /// <summary>
  249.         /// Emitted when the other interacting object stops touching the current Interactable Object.
  250.         /// </summary>
  251.         public event InteractableObjectEventHandler InteractableObjectUntouched;
  252.         /// <summary>
  253.         /// Emitted when another interacting object grabs the current Interactable Object.
  254.         /// </summary>
  255.         public event InteractableObjectEventHandler InteractableObjectGrabbed;
  256.         /// <summary>
  257.         /// Emitted when the other interacting object stops grabbing the current Interactable Object.
  258.         /// </summary>
  259.         public event InteractableObjectEventHandler InteractableObjectUngrabbed;
  260.         /// <summary>
  261.         /// Emitted when another interacting object uses the current Interactable Object.
  262.         /// </summary>
  263.         public event InteractableObjectEventHandler InteractableObjectUsed;
  264.         /// <summary>
  265.         /// Emitted when the other interacting object stops using the current Interactable Object.
  266.         /// </summary>
  267.         public event InteractableObjectEventHandler InteractableObjectUnused;
  268.         /// <summary>
  269.         /// Emitted when the Interactable Object enters a Snap Drop Zone.
  270.         /// </summary>
  271.         public event InteractableObjectEventHandler InteractableObjectEnteredSnapDropZone;
  272.         /// <summary>
  273.         /// Emitted when the Interactable Object exists a Snap Drop Zone.
  274.         /// </summary>
  275.         public event InteractableObjectEventHandler InteractableObjectExitedSnapDropZone;
  276.         /// <summary>
  277.         /// Emitted when the Interactable Object gets snapped to a Snap Drop Zone.
  278.         /// </summary>
  279.         public event InteractableObjectEventHandler InteractableObjectSnappedToDropZone;
  280.         /// <summary>
  281.         /// Emitted when the Interactable Object gets unsnapped from a Snap Drop Zone.
  282.         /// </summary>
  283.         public event InteractableObjectEventHandler InteractableObjectUnsnappedFromDropZone;
  284.  
  285.         /// <summary>
  286.         /// The current using state of the Interactable Object. `0` not being used, `1` being used.
  287.         /// </summary>
  288.         [HideInInspector]
  289.         public int usingState = 0;
  290.  
  291.         /// <summary>
  292.         /// isKinematic is a pass through to the `isKinematic` getter/setter on the Interactable Object's Rigidbody component.
  293.         /// </summary>
  294.         public bool isKinematic
  295.         {
  296.             get
  297.             {
  298.                 if (interactableRigidbody != null)
  299.                 {
  300.                     return interactableRigidbody.isKinematic;
  301.                 }
  302.                 return true;
  303.             }
  304.             set
  305.             {
  306.                 if (interactableRigidbody != null)
  307.                 {
  308.                     interactableRigidbody.isKinematic = value;
  309.                 }
  310.             }
  311.         }
  312.  
  313.         public virtual void OnInteractableObjectEnabled(InteractableObjectEventArgs e)
  314.         {
  315.             if (InteractableObjectEnabled != null)
  316.             {
  317.                 InteractableObjectEnabled(this, e);
  318.             }
  319.         }
  320.  
  321.         public virtual void OnInteractableObjectDisabled(InteractableObjectEventArgs e)
  322.         {
  323.             if (InteractableObjectDisabled != null)
  324.             {
  325.                 InteractableObjectDisabled(this, e);
  326.             }
  327.         }
  328.  
  329.         public virtual void OnInteractableObjectNearTouched(InteractableObjectEventArgs e)
  330.         {
  331.             if (InteractableObjectNearTouched != null)
  332.             {
  333.                 InteractableObjectNearTouched(this, e);
  334.             }
  335.         }
  336.  
  337.         public virtual void OnInteractableObjectNearUntouched(InteractableObjectEventArgs e)
  338.         {
  339.             if (InteractableObjectNearUntouched != null)
  340.             {
  341.                 InteractableObjectNearUntouched(this, e);
  342.             }
  343.         }
  344.  
  345.         public virtual void OnInteractableObjectTouched(InteractableObjectEventArgs e)
  346.         {
  347.             if (InteractableObjectTouched != null)
  348.             {
  349.                 InteractableObjectTouched(this, e);
  350.             }
  351.         }
  352.  
  353.         public virtual void OnInteractableObjectUntouched(InteractableObjectEventArgs e)
  354.         {
  355.             if (InteractableObjectUntouched != null)
  356.             {
  357.                 InteractableObjectUntouched(this, e);
  358.             }
  359.         }
  360.  
  361.         public virtual void OnInteractableObjectGrabbed(InteractableObjectEventArgs e)
  362.         {
  363.             if (InteractableObjectGrabbed != null)
  364.             {
  365.                 InteractableObjectGrabbed(this, e);
  366.             }
  367.         }
  368.  
  369.         public virtual void OnInteractableObjectUngrabbed(InteractableObjectEventArgs e)
  370.         {
  371.             if (InteractableObjectUngrabbed != null)
  372.             {
  373.                 InteractableObjectUngrabbed(this, e);
  374.             }
  375.         }
  376.  
  377.         public virtual void OnInteractableObjectUsed(InteractableObjectEventArgs e)
  378.         {
  379.             if (InteractableObjectUsed != null)
  380.             {
  381.                 InteractableObjectUsed(this, e);
  382.             }
  383.         }
  384.  
  385.         public virtual void OnInteractableObjectUnused(InteractableObjectEventArgs e)
  386.         {
  387.             if (InteractableObjectUnused != null)
  388.             {
  389.                 InteractableObjectUnused(this, e);
  390.             }
  391.         }
  392.  
  393.         public virtual void OnInteractableObjectEnteredSnapDropZone(InteractableObjectEventArgs e)
  394.         {
  395.             if (InteractableObjectEnteredSnapDropZone != null)
  396.             {
  397.                 InteractableObjectEnteredSnapDropZone(this, e);
  398.             }
  399.         }
  400.  
  401.         public virtual void OnInteractableObjectExitedSnapDropZone(InteractableObjectEventArgs e)
  402.         {
  403.             if (InteractableObjectExitedSnapDropZone != null)
  404.             {
  405.                 InteractableObjectExitedSnapDropZone(this, e);
  406.             }
  407.         }
  408.  
  409.         public virtual void OnInteractableObjectSnappedToDropZone(InteractableObjectEventArgs e)
  410.         {
  411.             if (InteractableObjectSnappedToDropZone != null)
  412.             {
  413.                 InteractableObjectSnappedToDropZone(this, e);
  414.             }
  415.         }
  416.  
  417.         public virtual void OnInteractableObjectUnsnappedFromDropZone(InteractableObjectEventArgs e)
  418.         {
  419.             if (InteractableObjectUnsnappedFromDropZone != null)
  420.             {
  421.                 InteractableObjectUnsnappedFromDropZone(this, e);
  422.             }
  423.         }
  424.  
  425.         public InteractableObjectEventArgs SetInteractableObjectEvent(GameObject interactingObject)
  426.         {
  427.             InteractableObjectEventArgs e;
  428.             e.interactingObject = interactingObject;
  429.             return e;
  430.         }
  431.  
  432.         /// <summary>
  433.         /// The IsNearTouched method is used to determine if the Interactable Object is currently being near touched.
  434.         /// </summary>
  435.         /// <returns>Returns `true` if the Interactable Object is currently being near touched.</returns>
  436.         public virtual bool IsNearTouched()
  437.         {
  438.             return (!IsTouched() && nearTouchingObjects.Count > 0);
  439.         }
  440.  
  441.         /// <summary>
  442.         /// The IsTouched method is used to determine if the Interactable Object is currently being touched.
  443.         /// </summary>
  444.         /// <returns>Returns `true` if the Interactable Object is currently being touched.</returns>
  445.         public virtual bool IsTouched()
  446.         {
  447.             return (touchingObjects.Count > 0);
  448.         }
  449.  
  450.         /// <summary>
  451.         /// The IsGrabbed method is used to determine if the Interactable Object is currently being grabbed.
  452.         /// </summary>
  453.         /// <param name="grabbedBy">An optional GameObject to check if the Interactable Object is grabbed by that specific GameObject. Defaults to `null`</param>
  454.         /// <returns>Returns `true` if the Interactable Object is currently being grabbed.</returns>
  455.         public virtual bool IsGrabbed(GameObject grabbedBy = null)
  456.         {
  457.             if (grabbingObjects.Count > 0 && grabbedBy != null)
  458.             {
  459.                 return (grabbingObjects.Contains(grabbedBy));
  460.             }
  461.             return (grabbingObjects.Count > 0);
  462.         }
  463.  
  464.         /// <summary>
  465.         /// The IsUsing method is used to determine if the Interactable Object is currently being used.
  466.         /// </summary>
  467.         /// <param name="usedBy">An optional GameObject to check if the Interactable Object is used by that specific GameObject. Defaults to `null`</param>
  468.         /// <returns>Returns `true` if the Interactable Object is currently being used.</returns>
  469.         public virtual bool IsUsing(GameObject usedBy = null)
  470.         {
  471.             if (usingObject != null && usedBy != null)
  472.             {
  473.                 return (usingObject.gameObject == usedBy);
  474.             }
  475.             return (usingObject != null);
  476.         }
  477.  
  478.         /// <summary>
  479.         /// The StartNearTouching method is called automatically when the Interactable Object is initially nearly touched.
  480.         /// </summary>
  481.         /// <param name="currentNearTouchingObject">The interacting object that is currently nearly touching this Interactable Object.</param>
  482.         public virtual void StartNearTouching(VRTK_InteractNearTouch currentNearTouchingObject = null)
  483.         {
  484.             GameObject currentNearTouchingGameObject = (currentNearTouchingObject != null ? currentNearTouchingObject.gameObject : null);
  485.             if (currentNearTouchingGameObject != null)
  486.             {
  487.                 if (nearTouchingObjects.Add(currentNearTouchingGameObject))
  488.                 {
  489.                     ToggleEnableState(true);
  490.                     OnInteractableObjectNearTouched(SetInteractableObjectEvent(currentNearTouchingGameObject));
  491.                 }
  492.             }
  493.         }
  494.  
  495.         /// <summary>
  496.         /// The StopNearTouching method is called automatically when the Interactable Object has stopped being nearly touched.
  497.         /// </summary>
  498.         /// <param name="previousNearTouchingObject">The interacting object that was previously nearly touching this Interactable Object.</param>
  499.         public virtual void StopNearTouching(VRTK_InteractNearTouch previousNearTouchingObject = null)
  500.         {
  501.             GameObject previousNearTouchingGameObject = (previousNearTouchingObject != null ? previousNearTouchingObject.gameObject : null);
  502.             if (previousNearTouchingGameObject != null && nearTouchingObjects.Remove(previousNearTouchingGameObject))
  503.             {
  504.                 OnInteractableObjectNearUntouched(SetInteractableObjectEvent(previousNearTouchingGameObject));
  505.             }
  506.         }
  507.  
  508.         /// <summary>
  509.         /// The StartTouching method is called automatically when the Interactable Object is touched initially.
  510.         /// </summary>
  511.         /// <param name="currentTouchingObject">The interacting object that is currently touching this Interactable Object.</param>
  512.         public virtual void StartTouching(VRTK_InteractTouch currentTouchingObject = null)
  513.         {
  514.             GameObject currentTouchingGameObject = (currentTouchingObject != null ? currentTouchingObject.gameObject : null);
  515.             if (currentTouchingGameObject != null)
  516.             {
  517.                 IgnoreColliders(currentTouchingGameObject);
  518.                 if (touchingObjects.Add(currentTouchingGameObject))
  519.                 {
  520.                     ToggleEnableState(true);
  521.                     OnInteractableObjectTouched(SetInteractableObjectEvent(currentTouchingGameObject));
  522.                 }
  523.             }
  524.         }
  525.  
  526.         /// <summary>
  527.         /// The StopTouching method is called automatically when the Interactable Object has stopped being touched.
  528.         /// </summary>
  529.         /// <param name="previousTouchingObject">The interacting object that was previously touching this Interactable Object.</param>
  530.         public virtual void StopTouching(VRTK_InteractTouch previousTouchingObject = null)
  531.         {
  532.             GameObject previousTouchingGameObject = (previousTouchingObject != null ? previousTouchingObject.gameObject : null);
  533.             if (previousTouchingGameObject != null && touchingObjects.Remove(previousTouchingGameObject))
  534.             {
  535.                 ResetUseState(previousTouchingGameObject);
  536.                 OnInteractableObjectUntouched(SetInteractableObjectEvent(previousTouchingGameObject));
  537.             }
  538.         }
  539.  
  540.         /// <summary>
  541.         /// The Grabbed method is called automatically when the Interactable Object is grabbed initially.
  542.         /// </summary>
  543.         /// <param name="currentGrabbingObject">The interacting object that is currently grabbing this Interactable Object.</param>
  544.         public virtual void Grabbed(VRTK_InteractGrab currentGrabbingObject = null)
  545.         {
  546.             GameObject currentGrabbingGameObject = (currentGrabbingObject != null ? currentGrabbingObject.gameObject : null);
  547.             ToggleEnableState(true);
  548.             if (!IsGrabbed() || IsSwappable())
  549.             {
  550.                 PrimaryControllerGrab(currentGrabbingGameObject);
  551.             }
  552.             else
  553.             {
  554.                 SecondaryControllerGrab(currentGrabbingGameObject);
  555.             }
  556.             OnInteractableObjectGrabbed(SetInteractableObjectEvent(currentGrabbingGameObject));
  557.         }
  558.  
  559.         /// <summary>
  560.         /// The Ungrabbed method is called automatically when the Interactable Object has stopped being grabbed.
  561.         /// </summary>
  562.         /// <param name="previousGrabbingObject">The interacting object that was previously grabbing this Interactable Object.</param>
  563.         public virtual void Ungrabbed(VRTK_InteractGrab previousGrabbingObject = null)
  564.         {
  565.             GameObject previousGrabbingGameObject = (previousGrabbingObject != null ? previousGrabbingObject.gameObject : null);
  566.             GameObject secondaryGrabbingObject = GetSecondaryGrabbingObject();
  567.             if (secondaryGrabbingObject == null || secondaryGrabbingObject != previousGrabbingGameObject)
  568.             {
  569.                 SecondaryControllerUngrab(secondaryGrabbingObject);
  570.                 PrimaryControllerUngrab(previousGrabbingGameObject, secondaryGrabbingObject);
  571.             }
  572.             else
  573.             {
  574.                 SecondaryControllerUngrab(previousGrabbingGameObject);
  575.             }
  576.             OnInteractableObjectUngrabbed(SetInteractableObjectEvent(previousGrabbingGameObject));
  577.         }
  578.  
  579.         /// <summary>
  580.         /// The StartUsing method is called automatically when the Interactable Object is used initially.
  581.         /// </summary>
  582.         /// <param name="currentUsingObject">The interacting object that is currently using this Interactable Object.</param>
  583.         public virtual void StartUsing(VRTK_InteractUse currentUsingObject = null)
  584.         {
  585.             GameObject currentUsingGameObject = (currentUsingObject != null ? currentUsingObject.gameObject : null);
  586.             ToggleEnableState(true);
  587.             if (IsUsing() && !IsUsing(currentUsingGameObject))
  588.             {
  589.                 ResetUsingObject();
  590.             }
  591.             OnInteractableObjectUsed(SetInteractableObjectEvent(currentUsingGameObject));
  592.             usingObject = currentUsingObject;
  593.         }
  594.  
  595.         /// <summary>
  596.         /// The StopUsing method is called automatically when the Interactable Object has stopped being used.
  597.         /// </summary>
  598.         /// <param name="previousUsingObject">The interacting object that was previously using this Interactable Object.</param>
  599.         /// <param name="resetUsingObjectState">Resets the using object state to reset it's using action.</param>
  600.         public virtual void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true)
  601.         {
  602.             GameObject previousUsingGameObject = (previousUsingObject != null ? previousUsingObject.gameObject : null);
  603.             OnInteractableObjectUnused(SetInteractableObjectEvent(previousUsingGameObject));
  604.             if (resetUsingObjectState)
  605.             {
  606.                 ResetUsingObject();
  607.             }
  608.             usingState = 0;
  609.             usingObject = null;
  610.         }
  611.  
  612.         /// <summary>
  613.         /// The ToggleHighlight method is used to turn on or off the highlight of the Interactable Object.
  614.         /// </summary>
  615.         /// <param name="toggle">The state to determine whether to activate or deactivate the highlight. `true` will enable the highlight and `false` will remove the highlight.</param>
  616.         [System.Obsolete("`VRTK_InteractableObject.ToggleHighlight` has been replaced with `VRTK_InteractableObject.Highlight` and `VRTK_InteractableObject.Unhighlight`. This method will be removed in a future version of VRTK.")]
  617.         public virtual void ToggleHighlight(bool toggle, Color? highlightColor = null)
  618.         {
  619.             if (toggle)
  620.             {
  621.                 Highlight((highlightColor != null ? (Color)highlightColor : Color.clear));
  622.             }
  623.             else
  624.             {
  625.                 Unhighlight();
  626.             }
  627.         }
  628.  
  629.         /// <summary>
  630.         /// The Highlight method turns on the highlighter attached to the Interactable Object with the given Color.
  631.         /// </summary>
  632.         /// <param name="highlightColor">The colour to apply to the highlighter.</param>
  633.         [System.Obsolete("`VRTK_InteractableObject.Highlight` has been replaced with `VRTK_InteractObjectHighlighter.Highlight`. This method will be removed in a future version of VRTK.")]
  634.         public virtual void Highlight(Color highlightColor)
  635.         {
  636.             VRTK_InteractObjectHighlighter interactObjectHighlighter = GetComponentInChildren<VRTK_InteractObjectHighlighter>();
  637.             if (interactObjectHighlighter != null)
  638.             {
  639.                 interactObjectHighlighter.Highlight(highlightColor);
  640.             }
  641.         }
  642.  
  643.         /// <summary>
  644.         /// The Unhighlight method turns off the highlighter attached to the Interactable Object.
  645.         /// </summary>
  646.         [System.Obsolete("`VRTK_InteractableObject.Unhighlight` has been replaced with `VRTK_InteractObjectHighlighter.Unhighlight`. This method will be removed in a future version of VRTK.")]
  647.         public virtual void Unhighlight()
  648.         {
  649.             VRTK_InteractObjectHighlighter interactObjectHighlighter = GetComponentInChildren<VRTK_InteractObjectHighlighter>();
  650.             if (interactObjectHighlighter != null)
  651.             {
  652.                 interactObjectHighlighter.Unhighlight();
  653.             }
  654.         }
  655.  
  656.         /// <summary>
  657.         /// The ResetHighlighter method is used to reset the currently attached highlighter.
  658.         /// </summary>
  659.         [System.Obsolete("`VRTK_InteractableObject.ResetHighlighter` has been replaced with `VRTK_InteractObjectHighlighter.ResetHighlighter`. This method will be removed in a future version of VRTK.")]
  660.         public virtual void ResetHighlighter()
  661.         {
  662.             VRTK_InteractObjectHighlighter interactObjectHighlighter = GetComponentInChildren<VRTK_InteractObjectHighlighter>();
  663.             if (interactObjectHighlighter != null)
  664.             {
  665.                 interactObjectHighlighter.ResetHighlighter();
  666.             }
  667.         }
  668.  
  669.         /// <summary>
  670.         /// The PauseCollisions method temporarily pauses all collisions on the Interactable Object at grab time by removing the Interactable Object's Rigidbody's ability to detect collisions.
  671.         /// </summary>
  672.         /// <param name="delay">The time in seconds to pause the collisions for.</param>
  673.         public virtual void PauseCollisions(float delay)
  674.         {
  675.             if (delay > 0f)
  676.             {
  677.                 Rigidbody[] childRigidbodies = GetComponentsInChildren<Rigidbody>();
  678.                 for (int i = 0; i < childRigidbodies.Length; i++)
  679.                 {
  680.                     childRigidbodies[i].detectCollisions = false;
  681.                 }
  682.                 Invoke("UnpauseCollisions", delay);
  683.             }
  684.         }
  685.  
  686.         /// <summary>
  687.         /// The ZeroVelocity method resets the velocity and angular velocity to zero on the Rigidbody attached to the Interactable Object.
  688.         /// </summary>
  689.         public virtual void ZeroVelocity()
  690.         {
  691.             if (interactableRigidbody != null)
  692.             {
  693.                 interactableRigidbody.velocity = Vector3.zero;
  694.                 interactableRigidbody.angularVelocity = Vector3.zero;
  695.             }
  696.         }
  697.  
  698.         /// <summary>
  699.         /// The SaveCurrentState method stores the existing Interactable Object parent and the Rigidbody kinematic setting.
  700.         /// </summary>
  701.         public virtual void SaveCurrentState()
  702.         {
  703.             if (!IsGrabbed() && !snappedInSnapDropZone)
  704.             {
  705.                 previousParent = transform.parent;
  706.                 if (!IsSwappable())
  707.                 {
  708.                     previousIsGrabbable = isGrabbable;
  709.                 }
  710.  
  711.                 if (interactableRigidbody != null)
  712.                 {
  713.                     previousKinematicState = interactableRigidbody.isKinematic;
  714.                 }
  715.             }
  716.         }
  717.  
  718.         /// <summary>
  719.         /// Returns the previous state of the Interactable Object
  720.         /// </summary>
  721.         /// <param name="previousParent">Out param for the previous parent</param>
  722.         /// <param name="previousKinematic">Out param for the previous Kinematic state</param>
  723.         /// <param name="previousGrabbable">Out param for the previous Grabbable State</param>
  724.         public virtual void GetPreviousState(out Transform previousParent, out bool previousKinematic, out bool previousGrabbable)
  725.         {
  726.             previousParent = this.previousParent;
  727.             previousKinematic = previousKinematicState;
  728.             previousGrabbable = previousIsGrabbable;
  729.         }
  730.  
  731.         /// <summary>
  732.         /// Overrides the previous state of the Interactable Object
  733.         /// </summary>
  734.         /// <param name="previousParent">value of the previous parent</param>
  735.         /// <param name="previousKinematic">value of the previous Kinematic state</param>
  736.         /// <param name="previousGrabbable">value of the previous Grabbable State</param>
  737.         public virtual void OverridePreviousState(Transform previousParent, bool previousKinematic, bool previousGrabbable)
  738.         {
  739.             this.previousParent = previousParent;
  740.             previousKinematicState = previousKinematic;
  741.             previousIsGrabbable = previousGrabbable;
  742.         }
  743.  
  744.         /// <summary>
  745.         /// The GetNearTouchingObjects method is used to return the collecetion of valid GameObjects that are currently nearly touching this Interactable Object.
  746.         /// </summary>
  747.         /// <returns>A list of GameObject of that are currently nearly touching the current Interactable Object.</returns>
  748.         public virtual List<GameObject> GetNearTouchingObjects()
  749.         {
  750.             return new List<GameObject>(nearTouchingObjects);
  751.         }
  752.  
  753.         /// <summary>
  754.         /// The GetTouchingObjects method is used to return the collecetion of valid GameObjects that are currently touching this Interactable Object.
  755.         /// </summary>
  756.         /// <returns>A list of GameObject of that are currently touching the current Interactable Object.</returns>
  757.         public virtual List<GameObject> GetTouchingObjects()
  758.         {
  759.             return new List<GameObject>(touchingObjects);
  760.         }
  761.  
  762.         /// <summary>
  763.         /// The GetGrabbingObject method is used to return the GameObject that is currently grabbing this Interactable Object.
  764.         /// </summary>
  765.         /// <returns>The GameObject of what is grabbing the current Interactable Object.</returns>
  766.         public virtual GameObject GetGrabbingObject()
  767.         {
  768.             return (IsGrabbed() ? grabbingObjects[0] : null);
  769.         }
  770.  
  771.         /// <summary>
  772.         /// The GetSecondaryGrabbingObject method is used to return the GameObject that is currently being used to influence this Interactable Object whilst it is being grabbed by a secondary influencing.
  773.         /// </summary>
  774.         /// <returns>The GameObject of the secondary influencing object of the current grabbed Interactable Object.</returns>
  775.         public virtual GameObject GetSecondaryGrabbingObject()
  776.         {
  777.             return (grabbingObjects.Count > 1 ? grabbingObjects[1] : null);
  778.         }
  779.  
  780.         /// <summary>
  781.         /// The GetUsingObject method is used to return the GameObject that is currently using this Interactable Object.
  782.         /// </summary>
  783.         /// <returns>The GameObject of what is using the current Interactable Object.</returns>
  784.         public virtual GameObject GetUsingObject()
  785.         {
  786.             return usingObject.gameObject;
  787.         }
  788.  
  789.         /// <summary>
  790.         /// The GetUsingScript method is used to return the Interact Use component that is currently using this Interactable Object.
  791.         /// </summary>
  792.         /// <returns>The Interact Use script of the interacting object that is using the current Interactable Object.</returns>
  793.         public virtual VRTK_InteractUse GetUsingScript()
  794.         {
  795.             return usingObject;
  796.         }
  797.  
  798.         /// <summary>
  799.         /// The IsValidInteractableController method is used to check to see if a controller is allowed to perform an interaction with this Interactable Object as sometimes controllers are prohibited from grabbing or using an Interactable Object depedning on the use case.
  800.         /// </summary>
  801.         /// <param name="actualController">The GameObject of the controller that is being checked.</param>
  802.         /// <param name="controllerCheck">The value of which controller is allowed to interact with this object.</param>
  803.         /// <returns>Returns `true` if the interacting controller is allowed to grab the Interactable Object.</returns>
  804.         public virtual bool IsValidInteractableController(GameObject actualController, AllowedController controllerCheck)
  805.         {
  806.             if (controllerCheck == AllowedController.Both)
  807.             {
  808.                 return true;
  809.             }
  810.  
  811.             SDK_BaseController.ControllerHand controllerHand = VRTK_DeviceFinder.GetControllerHandType(controllerCheck.ToString().Replace("Only", ""));
  812.             return (VRTK_DeviceFinder.IsControllerOfHand(actualController, controllerHand));
  813.         }
  814.  
  815.         /// <summary>
  816.         /// The ForceStopInteracting method forces the Interactable Object to no longer be interacted with and will cause an interacting object to drop the Interactable Object and stop touching it.
  817.         /// </summary>
  818.         public virtual void ForceStopInteracting()
  819.         {
  820.             if (gameObject.activeInHierarchy)
  821.             {
  822.                 forceDisabled = false;
  823.                 StartCoroutine(ForceStopInteractingAtEndOfFrame());
  824.             }
  825.  
  826.             if (!gameObject.activeInHierarchy && forceDisabled)
  827.             {
  828.                 ForceStopAllInteractions();
  829.                 forceDisabled = false;
  830.             }
  831.         }
  832.  
  833.         /// <summary>
  834.         /// The ForceStopSecondaryGrabInteraction method forces the Interactable Object to no longer be influenced by the second controller grabbing it.
  835.         /// </summary>
  836.         public virtual void ForceStopSecondaryGrabInteraction()
  837.         {
  838.             GameObject grabbingObject = GetSecondaryGrabbingObject();
  839.             if (grabbingObject != null)
  840.             {
  841.                 grabbingObject.GetComponentInChildren<VRTK_InteractGrab>().ForceRelease();
  842.             }
  843.         }
  844.  
  845.         /// <summary>
  846.         /// The RegisterTeleporters method is used to find all GameObjects that have a teleporter script and register the Interactable Object on the `OnTeleported` event.
  847.         /// </summary>
  848.         public virtual void RegisterTeleporters()
  849.         {
  850.             StartCoroutine(RegisterTeleportersAtEndOfFrame());
  851.         }
  852.  
  853.         /// <summary>
  854.         /// The UnregisterTeleporters method is used to unregister all teleporter events that are active on this Interactable Object.
  855.         /// </summary>
  856.         public virtual void UnregisterTeleporters()
  857.         {
  858.             for (int i = 0; i < VRTK_ObjectCache.registeredTeleporters.Count; i++)
  859.             {
  860.                 VRTK_BasicTeleport teleporter = VRTK_ObjectCache.registeredTeleporters[i];
  861.                 teleporter.Teleporting -= new TeleportEventHandler(OnTeleporting);
  862.                 teleporter.Teleported -= new TeleportEventHandler(OnTeleported);
  863.             }
  864.         }
  865.  
  866.         /// <summary>
  867.         /// the StoreLocalScale method saves the current transform local scale values.
  868.         /// </summary>
  869.         public virtual void StoreLocalScale()
  870.         {
  871.             previousLocalScale = transform.localScale;
  872.         }
  873.  
  874.         /// <summary>
  875.         /// The ToggleSnapDropZone method is used to set the state of whether the Interactable Object is in a Snap Drop Zone or not.
  876.         /// </summary>
  877.         /// <param name="snapDropZone">The Snap Drop Zone object that is being interacted with.</param>
  878.         /// <param name="state">The state of whether the Interactable Object is fixed in or removed from the Snap Drop Zone. `true` denotes the Interactable Object is snapped to the Snap Drop Zone and `false` denotes it has been removed from the Snap Drop Zone.</param>
  879.         public virtual void ToggleSnapDropZone(VRTK_SnapDropZone snapDropZone, bool state)
  880.         {
  881.             snappedInSnapDropZone = state;
  882.             if (state)
  883.             {
  884.                 storedSnapDropZone = snapDropZone;
  885.                 OnInteractableObjectSnappedToDropZone(SetInteractableObjectEvent(snapDropZone.gameObject));
  886.             }
  887.             else
  888.             {
  889.                 if (interactableRigidbody != null)
  890.                 {
  891.                     interactableRigidbody.WakeUp();
  892.                 }
  893.                 ResetDropSnapType();
  894.                 OnInteractableObjectUnsnappedFromDropZone(SetInteractableObjectEvent(snapDropZone.gameObject));
  895.             }
  896.         }
  897.  
  898.         /// <summary>
  899.         /// The IsInSnapDropZone method determines whether the Interactable Object is currently snapped to a Snap Drop Zone.
  900.         /// </summary>
  901.         /// <returns>Returns `true` if the Interactable Object is currently snapped in a Snap Drop Zone, returns `false` if it is not.</returns>
  902.         public virtual bool IsInSnapDropZone()
  903.         {
  904.             return snappedInSnapDropZone;
  905.         }
  906.  
  907.         /// <summary>
  908.         /// The SetSnapDropZoneHover method sets whether the Interactable Object is currently being hovered over a valid Snap Drop Zone.
  909.         /// </summary>
  910.         /// <param name="snapDropZone">The Snap Drop Zone that is being interacted with.</param>
  911.         /// <param name="state">The state of whether the Interactable Object is being hovered or not.</param>
  912.         public virtual void SetSnapDropZoneHover(VRTK_SnapDropZone snapDropZone, bool state)
  913.         {
  914.             if (state)
  915.             {
  916.                 if (hoveredSnapObjects.Add(snapDropZone.gameObject))
  917.                 {
  918.                     OnInteractableObjectEnteredSnapDropZone(SetInteractableObjectEvent(snapDropZone.gameObject));
  919.                 }
  920.             }
  921.             else
  922.             {
  923.                 if (hoveredSnapObjects.Remove(snapDropZone.gameObject))
  924.                 {
  925.                     OnInteractableObjectExitedSnapDropZone(SetInteractableObjectEvent(snapDropZone.gameObject));
  926.                 }
  927.             }
  928.             hoveredOverSnapDropZone = (hoveredSnapObjects.Count > 0);
  929.         }
  930.  
  931.         /// <summary>
  932.         /// The GetStoredSnapDropZone method returns the Snap Drop Zone that the Interactable Object is currently snapped to.
  933.         /// </summary>
  934.         /// <returns>The SnapDropZone that the Interactable Object is currently snapped to.</returns>
  935.         public virtual VRTK_SnapDropZone GetStoredSnapDropZone()
  936.         {
  937.             return storedSnapDropZone;
  938.         }
  939.  
  940.         /// <summary>
  941.         /// The IsHoveredOverSnapDropZone method returns whether the Interactable Object is currently hovering over a Snap Drop Zone.
  942.         /// </summary>
  943.         /// <returns>Returns `true` if the Interactable Object is currently hovering over a Snap Drop Zone.</returns>
  944.         public virtual bool IsHoveredOverSnapDropZone()
  945.         {
  946.             return hoveredOverSnapDropZone;
  947.         }
  948.  
  949.         /// <summary>
  950.         /// The IsDroppable method returns whether the Interactable Object can be dropped or not in it's current situation.
  951.         /// </summary>
  952.         /// <returns>Returns `true` if the Interactable Object can currently be dropped, returns `false` if it is not currently possible to drop.</returns>
  953.         public virtual bool IsDroppable()
  954.         {
  955.             switch (validDrop)
  956.             {
  957.                 case ValidDropTypes.NoDrop:
  958.                     return false;
  959.                 case ValidDropTypes.DropAnywhere:
  960.                     return true;
  961.                 case ValidDropTypes.DropValidSnapDropZone:
  962.                     return hoveredOverSnapDropZone;
  963.             }
  964.             return false;
  965.         }
  966.  
  967.         /// <summary>
  968.         /// The IsSwappable method returns whether the Interactable Object can be grabbed with one interacting object and then swapped to another interacting object by grabbing with the secondary grab action.
  969.         /// </summary>
  970.         /// <returns>Returns `true` if the Interactable Object can be grabbed by a secondary interacting object whilst already being grabbed and the Interactable Object will swap controllers. Returns `false` if the Interactable Object cannot be swapped.</returns>
  971.         public virtual bool IsSwappable()
  972.         {
  973.             return (secondaryGrabActionScript != null ? secondaryGrabActionScript.IsSwappable() : false);
  974.         }
  975.  
  976.         /// <summary>
  977.         /// The PerformSecondaryAction method returns whether the Interactable Object has a Secondary Grab Action that can be performed when grabbing the object with a secondary interacting object.
  978.         /// </summary>
  979.         /// <returns>Returns `true` if the Interactable Object has a Secondary Grab Action, returns `false` if it has no Secondary Grab Action or is swappable.</returns>
  980.         public virtual bool PerformSecondaryAction()
  981.         {
  982.             return (GetGrabbingObject() != null && GetSecondaryGrabbingObject() == null && secondaryGrabActionScript != null ? secondaryGrabActionScript.IsActionable() : false);
  983.         }
  984.  
  985.         /// <summary>
  986.         /// The ResetIgnoredColliders method is used to clear any stored ignored colliders in case the `Ignored Colliders` array parameter is changed at runtime. This needs to be called manually if changes are made at runtime.
  987.         /// </summary>
  988.         public virtual void ResetIgnoredColliders()
  989.         {
  990.             //Go through all the existing set up ignored colliders and reset their collision state
  991.             foreach (GameObject currentIgnoredCollider in new HashSet<GameObject>(currentIgnoredColliders))
  992.             {
  993.                 if (currentIgnoredCollider != null)
  994.                 {
  995.                     Collider[] touchingColliders = currentIgnoredCollider.GetComponentsInChildren<Collider>();
  996.                     if (ignoredColliders != null)
  997.                     {
  998.                         for (int i = 0; i < ignoredColliders.Length; i++)
  999.                         {
  1000.                             for (int j = 0; j < touchingColliders.Length; j++)
  1001.                             {
  1002.                                 Physics.IgnoreCollision(touchingColliders[j], ignoredColliders[i], false);
  1003.                             }
  1004.                         }
  1005.                     }
  1006.                 }
  1007.             }
  1008.             currentIgnoredColliders.Clear();
  1009.         }
  1010.  
  1011.         /// <summary>
  1012.         /// The SubscribeToInteractionEvent method subscribes a given method callback for the given Interaction Type.
  1013.         /// </summary>
  1014.         /// <param name="givenType">The Interaction Type to register the events for.</param>
  1015.         /// <param name="methodCallback">The method to execute when the Interaction Type is initiated.</param>
  1016.         public virtual void SubscribeToInteractionEvent(InteractionType givenType, InteractableObjectEventHandler methodCallback)
  1017.         {
  1018.             ManageInteractionEvent(givenType, true, methodCallback);
  1019.         }
  1020.  
  1021.         /// <summary>
  1022.         /// The UnsubscribeFromInteractionEvent method unsubscribes a previous event subscription for the given Interaction Type.
  1023.         /// </summary>
  1024.         /// <param name="givenType">The Interaction Type that the previous event subscription was under.</param>
  1025.         /// <param name="methodCallback">The method that was being executed when the Interaction Type was initiated.</param>
  1026.         public virtual void UnsubscribeFromInteractionEvent(InteractionType givenType, InteractableObjectEventHandler methodCallback)
  1027.         {
  1028.             ManageInteractionEvent(givenType, false, methodCallback);
  1029.         }
  1030.  
  1031.         /// <summary>
  1032.         /// The GetPrimaryAttachPoint returns the Transform that determines where the primary grabbing object is grabbing the Interactable Object at.
  1033.         /// </summary>
  1034.         /// <returns>A Transform that denotes where the primary grabbing object is grabbing the Interactable Object at.</returns>
  1035.         public virtual Transform GetPrimaryAttachPoint()
  1036.         {
  1037.             return primaryControllerAttachPoint;
  1038.         }
  1039.  
  1040.         /// <summary>
  1041.         /// The GetSecondaryAttachPoint returns the Transform that determines where the secondary grabbing object is grabbing the Interactable Object at.
  1042.         /// </summary>
  1043.         /// <returns>A Transform that denotes where the secondary grabbing object is grabbing the Interactable Object at.</returns>
  1044.         public virtual Transform GetSecondaryAttachPoint()
  1045.         {
  1046.             return secondaryControllerAttachPoint;
  1047.         }
  1048.  
  1049.         protected virtual void Awake()
  1050.         {
  1051.             interactableRigidbody = GetComponent<Rigidbody>();
  1052.             if (interactableRigidbody != null)
  1053.             {
  1054.                 interactableRigidbody.maxAngularVelocity = float.MaxValue;
  1055.             }
  1056.  
  1057.             if (disableWhenIdle && enabled && IsIdle())
  1058.             {
  1059.                 startDisabled = true;
  1060.                 enabled = false;
  1061.             }
  1062.  
  1063.             ///[Obsolete]
  1064. #pragma warning disable 0618
  1065.             if (touchHighlightColor != Color.clear && !GetComponent<VRTK_InteractObjectHighlighter>())
  1066.             {
  1067.                 VRTK_InteractObjectHighlighter autoGenInteractHighlighter = gameObject.AddComponent<VRTK_InteractObjectHighlighter>();
  1068.                 autoGenInteractHighlighter.touchHighlight = touchHighlightColor;
  1069.                 autoGenInteractHighlighter.objectHighlighter = (objectHighlighter == null ? Highlighters.VRTK_BaseHighlighter.GetActiveHighlighter(gameObject) : objectHighlighter);
  1070.             }
  1071. #pragma warning restore 0618
  1072.         }
  1073.  
  1074.         protected virtual void OnEnable()
  1075.         {
  1076.             RegisterTeleporters();
  1077.             forceDisabled = false;
  1078.             if (forcedDropped)
  1079.             {
  1080.                 LoadPreviousState();
  1081.             }
  1082.             forcedDropped = false;
  1083.             startDisabled = false;
  1084.             OnInteractableObjectEnabled(SetInteractableObjectEvent(null));
  1085.         }
  1086.  
  1087.         protected virtual void OnDisable()
  1088.         {
  1089.             UnregisterTeleporters();
  1090.             if (!startDisabled)
  1091.             {
  1092.                 forceDisabled = true;
  1093.                 ForceStopInteracting();
  1094.             }
  1095.             OnInteractableObjectDisabled(SetInteractableObjectEvent(null));
  1096.         }
  1097.  
  1098.         protected virtual void FixedUpdate()
  1099.         {
  1100.             if (trackPoint != null && grabAttachMechanicScript != null)
  1101.             {
  1102.                 grabAttachMechanicScript.ProcessFixedUpdate();
  1103.             }
  1104.  
  1105.             if (secondaryGrabActionScript != null)
  1106.             {
  1107.                 secondaryGrabActionScript.ProcessFixedUpdate();
  1108.             }
  1109.         }
  1110.  
  1111.         protected virtual void Update()
  1112.         {
  1113.             AttemptSetGrabMechanic();
  1114.             AttemptSetSecondaryGrabAction();
  1115.  
  1116.             if (trackPoint != null && grabAttachMechanicScript != null)
  1117.             {
  1118.                 grabAttachMechanicScript.ProcessUpdate();
  1119.             }
  1120.  
  1121.             if (secondaryGrabActionScript != null)
  1122.             {
  1123.                 secondaryGrabActionScript.ProcessUpdate();
  1124.             }
  1125.         }
  1126.  
  1127.         /// <summary>
  1128.         /// determines if this object is currently idle
  1129.         /// used to determine whether or not the script
  1130.         /// can be disabled for now
  1131.         /// </summary>
  1132.         /// <returns>whether or not the script is currently idle</returns>
  1133.         protected virtual bool IsIdle()
  1134.         {
  1135.             return !IsNearTouched() && !IsTouched() && !IsGrabbed() && !IsUsing();
  1136.         }
  1137.  
  1138.         protected virtual void LateUpdate()
  1139.         {
  1140.             if (disableWhenIdle && IsIdle())
  1141.             {
  1142.                 ToggleEnableState(false);
  1143.             }
  1144.         }
  1145.  
  1146.         protected virtual void LoadPreviousState()
  1147.         {
  1148.             if (gameObject.activeInHierarchy)
  1149.             {
  1150.                 transform.SetParent(previousParent);
  1151.                 forcedDropped = false;
  1152.             }
  1153.             if (interactableRigidbody != null)
  1154.             {
  1155.                 interactableRigidbody.isKinematic = previousKinematicState;
  1156.             }
  1157.             if (!IsSwappable())
  1158.             {
  1159.                 isGrabbable = previousIsGrabbable;
  1160.             }
  1161.         }
  1162.  
  1163.         protected virtual void IgnoreColliders(GameObject touchingObject)
  1164.         {
  1165.             if (ignoredColliders != null && !currentIgnoredColliders.Contains(touchingObject))
  1166.             {
  1167.                 bool objectIgnored = false;
  1168.                 Collider[] touchingColliders = touchingObject.GetComponentsInChildren<Collider>();
  1169.                 for (int i = 0; i < ignoredColliders.Length; i++)
  1170.                 {
  1171.                     for (int j = 0; j < touchingColliders.Length; j++)
  1172.                     {
  1173.                         Physics.IgnoreCollision(touchingColliders[j], ignoredColliders[i]);
  1174.                         objectIgnored = true;
  1175.                     }
  1176.                 }
  1177.  
  1178.                 if (objectIgnored)
  1179.                 {
  1180.                     currentIgnoredColliders.Add(touchingObject);
  1181.                 }
  1182.             }
  1183.         }
  1184.  
  1185.         protected virtual void ToggleEnableState(bool state)
  1186.         {
  1187.             if (disableWhenIdle)
  1188.             {
  1189.                 enabled = state;
  1190.             }
  1191.         }
  1192.  
  1193.         protected virtual void AttemptSetGrabMechanic()
  1194.         {
  1195.             if (isGrabbable && grabAttachMechanicScript == null)
  1196.             {
  1197.                 VRTK_BaseGrabAttach setGrabMechanic = GetComponent<VRTK_BaseGrabAttach>();
  1198.                 if (setGrabMechanic == null)
  1199.                 {
  1200.                     setGrabMechanic = gameObject.AddComponent<VRTK_FixedJointGrabAttach>();
  1201.                 }
  1202.                 grabAttachMechanicScript = setGrabMechanic;
  1203.             }
  1204.         }
  1205.  
  1206.         protected virtual void AttemptSetSecondaryGrabAction()
  1207.         {
  1208.             if (isGrabbable && secondaryGrabActionScript == null)
  1209.             {
  1210.                 secondaryGrabActionScript = GetComponent<VRTK_BaseGrabAction>();
  1211.             }
  1212.         }
  1213.  
  1214.         protected virtual void ForceReleaseGrab()
  1215.         {
  1216.             GameObject grabbingObject = GetGrabbingObject();
  1217.             if (grabbingObject != null)
  1218.             {
  1219.                 grabbingObject.GetComponentInChildren<VRTK_InteractGrab>().ForceRelease();
  1220.             }
  1221.         }
  1222.  
  1223.         protected virtual void PrimaryControllerGrab(GameObject currentGrabbingObject)
  1224.         {
  1225.             if (snappedInSnapDropZone)
  1226.             {
  1227.                 ToggleSnapDropZone(storedSnapDropZone, false);
  1228.             }
  1229.             ForceReleaseGrab();
  1230.             RemoveTrackPoint();
  1231.             VRTK_SharedMethods.AddListValue(grabbingObjects, currentGrabbingObject, true);
  1232.             SetTrackPoint(currentGrabbingObject);
  1233.             if (!IsSwappable())
  1234.             {
  1235.                 previousIsGrabbable = isGrabbable;
  1236.                 isGrabbable = false;
  1237.             }
  1238.         }
  1239.  
  1240.         protected virtual void SecondaryControllerGrab(GameObject currentGrabbingObject)
  1241.         {
  1242.             if (VRTK_SharedMethods.AddListValue(grabbingObjects, currentGrabbingObject, true))
  1243.             {
  1244.                 secondaryControllerAttachPoint = CreateAttachPoint(currentGrabbingObject.name, "Secondary", currentGrabbingObject.transform);
  1245.  
  1246.                 if (secondaryGrabActionScript != null)
  1247.                 {
  1248.                     secondaryGrabActionScript.Initialise(this, GetGrabbingObject().GetComponentInChildren<VRTK_InteractGrab>(), GetSecondaryGrabbingObject().GetComponentInChildren<VRTK_InteractGrab>(), primaryControllerAttachPoint, secondaryControllerAttachPoint);
  1249.                 }
  1250.             }
  1251.         }
  1252.  
  1253.         protected virtual void PrimaryControllerUngrab(GameObject previousGrabbingObject, GameObject previousSecondaryGrabbingObject)
  1254.         {
  1255.             UnpauseCollisions();
  1256.             RemoveTrackPoint();
  1257.             ResetUseState(previousGrabbingObject);
  1258.             grabbingObjects.Clear();
  1259.             if (secondaryGrabActionScript != null && previousSecondaryGrabbingObject != null)
  1260.             {
  1261.                 secondaryGrabActionScript.OnDropAction();
  1262.                 previousSecondaryGrabbingObject.GetComponentInChildren<VRTK_InteractGrab>().ForceRelease();
  1263.             }
  1264.             LoadPreviousState();
  1265.         }
  1266.  
  1267.         protected virtual void SecondaryControllerUngrab(GameObject previousGrabbingObject)
  1268.         {
  1269.             if (grabbingObjects.Remove(previousGrabbingObject))
  1270.             {
  1271.                 Destroy(secondaryControllerAttachPoint.gameObject);
  1272.                 secondaryControllerAttachPoint = null;
  1273.                 if (secondaryGrabActionScript != null)
  1274.                 {
  1275.                     secondaryGrabActionScript.ResetAction();
  1276.                 }
  1277.             }
  1278.         }
  1279.  
  1280.         protected virtual void UnpauseCollisions()
  1281.         {
  1282.             Rigidbody[] childRigidbodies = GetComponentsInChildren<Rigidbody>();
  1283.             for (int i = 0; i < childRigidbodies.Length; i++)
  1284.             {
  1285.                 childRigidbodies[i].detectCollisions = true;
  1286.             }
  1287.         }
  1288.  
  1289.         protected virtual void SetTrackPoint(GameObject currentGrabbingObject)
  1290.         {
  1291.             AddTrackPoint(currentGrabbingObject);
  1292.             primaryControllerAttachPoint = CreateAttachPoint(GetGrabbingObject().name, "Original", trackPoint);
  1293.  
  1294.             if (grabAttachMechanicScript != null)
  1295.             {
  1296.                 grabAttachMechanicScript.SetTrackPoint(trackPoint);
  1297.                 grabAttachMechanicScript.SetInitialAttachPoint(primaryControllerAttachPoint);
  1298.             }
  1299.         }
  1300.  
  1301.         protected virtual Transform CreateAttachPoint(string namePrefix, string nameSuffix, Transform origin)
  1302.         {
  1303.             Transform attachPoint = new GameObject(VRTK_SharedMethods.GenerateVRTKObjectName(true, namePrefix, nameSuffix, "Controller", "AttachPoint")).transform;
  1304.             attachPoint.SetParent(transform);
  1305.             attachPoint.position = origin.position;
  1306.             attachPoint.rotation = origin.rotation;
  1307.             attachPoint.localScale = Vector3.one;
  1308.             return attachPoint;
  1309.         }
  1310.  
  1311.         protected virtual void AddTrackPoint(GameObject currentGrabbingObject)
  1312.         {
  1313.             VRTK_InteractGrab grabScript = currentGrabbingObject.GetComponentInChildren<VRTK_InteractGrab>();
  1314.             Transform controllerPoint = ((grabScript && grabScript.controllerAttachPoint) ? grabScript.controllerAttachPoint.transform : currentGrabbingObject.transform);
  1315.  
  1316.             if (grabAttachMechanicScript != null)
  1317.             {
  1318.                 trackPoint = grabAttachMechanicScript.CreateTrackPoint(controllerPoint, gameObject, currentGrabbingObject, ref customTrackPoint);
  1319.             }
  1320.         }
  1321.  
  1322.         protected virtual void RemoveTrackPoint()
  1323.         {
  1324.             if (customTrackPoint && trackPoint != null)
  1325.             {
  1326.                 Destroy(trackPoint.gameObject);
  1327.             }
  1328.             else
  1329.             {
  1330.                 trackPoint = null;
  1331.             }
  1332.  
  1333.             if (primaryControllerAttachPoint != null)
  1334.             {
  1335.                 Destroy(primaryControllerAttachPoint.gameObject);
  1336.             }
  1337.         }
  1338.  
  1339.         protected virtual void OnTeleporting(object sender, DestinationMarkerEventArgs e)
  1340.         {
  1341.             if (!stayGrabbedOnTeleport)
  1342.             {
  1343.                 ZeroVelocity();
  1344.                 ForceStopAllInteractions();
  1345.             }
  1346.         }
  1347.  
  1348.         protected virtual void OnTeleported(object sender, DestinationMarkerEventArgs e)
  1349.         {
  1350.             if (grabAttachMechanicScript != null && grabAttachMechanicScript.IsTracked() && stayGrabbedOnTeleport && trackPoint != null)
  1351.             {
  1352.                 GameObject actualController = VRTK_DeviceFinder.GetActualController(GetGrabbingObject());
  1353.                 transform.position = (actualController ? actualController.transform.position : transform.position);
  1354.             }
  1355.         }
  1356.  
  1357.         protected virtual IEnumerator RegisterTeleportersAtEndOfFrame()
  1358.         {
  1359.             yield return new WaitForEndOfFrame();
  1360.             for (int i = 0; i < VRTK_ObjectCache.registeredTeleporters.Count; i++)
  1361.             {
  1362.                 VRTK_BasicTeleport teleporter = VRTK_ObjectCache.registeredTeleporters[i];
  1363.                 teleporter.Teleporting += new TeleportEventHandler(OnTeleporting);
  1364.                 teleporter.Teleported += new TeleportEventHandler(OnTeleported);
  1365.             }
  1366.         }
  1367.  
  1368.         protected virtual void ResetUseState(GameObject checkObject)
  1369.         {
  1370.             if (checkObject != null)
  1371.             {
  1372.                 VRTK_InteractUse usingObjectCheck = checkObject.GetComponentInChildren<VRTK_InteractUse>();
  1373.                 if (usingObjectCheck != null)
  1374.                 {
  1375.                     if (holdButtonToUse)
  1376.                     {
  1377.                         usingObjectCheck.ForceStopUsing();
  1378.                     }
  1379.                 }
  1380.             }
  1381.         }
  1382.  
  1383.         protected virtual IEnumerator ForceStopInteractingAtEndOfFrame()
  1384.         {
  1385.             yield return new WaitForEndOfFrame();
  1386.             ForceStopAllInteractions();
  1387.         }
  1388.  
  1389.         protected virtual void ForceStopAllInteractions()
  1390.         {
  1391.             if (touchingObjects == null)
  1392.             {
  1393.                 return;
  1394.             }
  1395.  
  1396.             StopTouchingInteractions();
  1397.             StopGrabbingInteractions();
  1398.             StopUsingInteractions();
  1399.         }
  1400.  
  1401.         protected virtual void StopTouchingInteractions()
  1402.         {
  1403.             foreach (GameObject touchingObject in new HashSet<GameObject>(touchingObjects))
  1404.             {
  1405.                 if (touchingObject.activeInHierarchy || forceDisabled)
  1406.                 {
  1407.                     touchingObject.GetComponentInChildren<VRTK_InteractTouch>().ForceStopTouching();
  1408.                 }
  1409.             }
  1410.         }
  1411.  
  1412.         protected virtual void StopGrabbingInteractions()
  1413.         {
  1414.             GameObject grabbingObject = GetGrabbingObject();
  1415.  
  1416.             if (grabbingObject != null && (grabbingObject.activeInHierarchy || forceDisabled))
  1417.             {
  1418.                 VRTK_InteractGrab grabbingObjectScript = grabbingObject.GetComponentInChildren<VRTK_InteractGrab>();
  1419.                 if (grabbingObjectScript != null && grabbingObjectScript.interactTouch != null)
  1420.                 {
  1421.                     grabbingObjectScript.interactTouch.ForceStopTouching();
  1422.                     grabbingObjectScript.ForceRelease();
  1423.                     forcedDropped = true;
  1424.                 }
  1425.             }
  1426.         }
  1427.  
  1428.         protected virtual void StopUsingInteractions()
  1429.         {
  1430.             if (usingObject != null && usingObject.interactTouch != null && (usingObject.gameObject.activeInHierarchy || forceDisabled))
  1431.             {
  1432.                 usingObject.interactTouch.ForceStopTouching();
  1433.                 usingObject.ForceStopUsing();
  1434.             }
  1435.         }
  1436.  
  1437.         protected virtual void ResetDropSnapType()
  1438.         {
  1439.             switch (storedSnapDropZone.snapType)
  1440.             {
  1441.                 case VRTK_SnapDropZone.SnapTypes.UseKinematic:
  1442.                 case VRTK_SnapDropZone.SnapTypes.UseParenting:
  1443.                     LoadPreviousState();
  1444.                     break;
  1445.                 case VRTK_SnapDropZone.SnapTypes.UseJoint:
  1446.                     Joint snapDropZoneJoint = storedSnapDropZone.GetComponent<Joint>();
  1447.                     if (snapDropZoneJoint)
  1448.                     {
  1449.                         snapDropZoneJoint.connectedBody = null;
  1450.                     }
  1451.                     break;
  1452.             }
  1453.  
  1454.             if (!previousLocalScale.Equals(Vector3.zero))
  1455.             {
  1456.                 transform.localScale = previousLocalScale;
  1457.             }
  1458.  
  1459.             storedSnapDropZone.OnObjectUnsnappedFromDropZone(storedSnapDropZone.SetSnapDropZoneEvent(gameObject));
  1460.             storedSnapDropZone = null;
  1461.         }
  1462.  
  1463.         protected virtual void ResetUsingObject()
  1464.         {
  1465.             if (usingObject != null)
  1466.             {
  1467.                 usingObject.ForceResetUsing();
  1468.             }
  1469.         }
  1470.  
  1471.         protected virtual void ManageInteractionEvent(InteractionType givenType, bool state, InteractableObjectEventHandler methodCallback)
  1472.         {
  1473.             switch (givenType)
  1474.             {
  1475.                 case InteractionType.NearTouch:
  1476.                     ManageNearTouchSubscriptions(state, methodCallback);
  1477.                     break;
  1478.                 case InteractionType.Touch:
  1479.                     ManageTouchSubscriptions(state, methodCallback);
  1480.                     break;
  1481.                 case InteractionType.Grab:
  1482.                     ManageGrabSubscriptions(state, methodCallback);
  1483.                     break;
  1484.                 case InteractionType.Use:
  1485.                     ManageUseSubscriptions(state, methodCallback);
  1486.                     break;
  1487.                 case InteractionType.NearUntouch:
  1488.                     ManageNearUntouchSubscriptions(state, methodCallback);
  1489.                     break;
  1490.                 case InteractionType.Untouch:
  1491.                     ManageUntouchSubscriptions(state, methodCallback);
  1492.                     break;
  1493.                 case InteractionType.Ungrab:
  1494.                     ManageUngrabSubscriptions(state, methodCallback);
  1495.                     break;
  1496.                 case InteractionType.Unuse:
  1497.                     ManageUnuseSubscriptions(state, methodCallback);
  1498.                     break;
  1499.             }
  1500.         }
  1501.  
  1502.         protected virtual void ManageNearTouchSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1503.         {
  1504.             if (!register)
  1505.             {
  1506.                 InteractableObjectNearTouched -= methodCallback;
  1507.             }
  1508.  
  1509.             if (register)
  1510.             {
  1511.                 InteractableObjectNearTouched += methodCallback;
  1512.             }
  1513.         }
  1514.  
  1515.         protected virtual void ManageTouchSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1516.         {
  1517.             if (!register)
  1518.             {
  1519.                 InteractableObjectTouched -= methodCallback;
  1520.             }
  1521.  
  1522.             if (register)
  1523.             {
  1524.                 InteractableObjectTouched += methodCallback;
  1525.             }
  1526.         }
  1527.  
  1528.         protected virtual void ManageGrabSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1529.         {
  1530.             if (!register)
  1531.             {
  1532.                 InteractableObjectGrabbed -= methodCallback;
  1533.             }
  1534.  
  1535.             if (register)
  1536.             {
  1537.                 InteractableObjectGrabbed += methodCallback;
  1538.             }
  1539.         }
  1540.  
  1541.         protected virtual void ManageUseSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1542.         {
  1543.             if (!register)
  1544.             {
  1545.                 InteractableObjectUsed -= methodCallback;
  1546.             }
  1547.  
  1548.             if (register)
  1549.             {
  1550.                 InteractableObjectUsed += methodCallback;
  1551.             }
  1552.         }
  1553.  
  1554.         protected virtual void ManageNearUntouchSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1555.         {
  1556.             if (!register)
  1557.             {
  1558.                 InteractableObjectNearUntouched -= methodCallback;
  1559.             }
  1560.  
  1561.             if (register)
  1562.             {
  1563.                 InteractableObjectNearUntouched += methodCallback;
  1564.             }
  1565.         }
  1566.  
  1567.         protected virtual void ManageUntouchSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1568.         {
  1569.             if (!register)
  1570.             {
  1571.                 InteractableObjectUntouched -= methodCallback;
  1572.             }
  1573.  
  1574.             if (register)
  1575.             {
  1576.                 InteractableObjectUntouched += methodCallback;
  1577.             }
  1578.         }
  1579.  
  1580.         protected virtual void ManageUngrabSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1581.         {
  1582.             if (!register)
  1583.             {
  1584.                 InteractableObjectUngrabbed -= methodCallback;
  1585.             }
  1586.  
  1587.             if (register)
  1588.             {
  1589.                 InteractableObjectUngrabbed += methodCallback;
  1590.             }
  1591.         }
  1592.  
  1593.         protected virtual void ManageUnuseSubscriptions(bool register, InteractableObjectEventHandler methodCallback)
  1594.         {
  1595.             if (!register)
  1596.             {
  1597.                 InteractableObjectUnused -= methodCallback;
  1598.             }
  1599.  
  1600.             if (register)
  1601.             {
  1602.                 InteractableObjectUnused += methodCallback;
  1603.             }
  1604.         }
  1605.     }
  1606. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement