Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 26.62 KB | None | 0 0
  1. // Interact Grab|Interactors|30040
  2. namespace VRTK
  3. {
  4.     using UnityEngine;
  5.  
  6.     /// <summary>
  7.     /// Determines if the Interact Touch can initiate a grab with the touched Interactable Object.
  8.     /// </summary>
  9.     /// <remarks>
  10.     /// **Required Components:**
  11.     ///  * `VRTK_InteractTouch` - The touch component to determine when a valid touch has taken place to denote a grab can occur. This must be applied on the same GameObject as this script if one is not provided via the `Interact Touch` parameter.
  12.     ///
  13.     /// **Optional Components:**
  14.     ///  * `VRTK_ControllerEvents` - The events component to listen for the button presses on. This must be applied on the same GameObject as this script if one is not provided via the `Controller Events` parameter.
  15.     ///
  16.     /// **Script Usage:**
  17.     ///  * Place the `VRTK_InteractGrab` script on either:
  18.     ///    * The GameObject with the Interact Touch and Controller Events scripts.
  19.     ///    * Any other scene GameObject and provide a valid `VRTK_ControllerEvents` component to the `Controller Events` parameter and a valid `VRTK_InteractTouch` component to the `Interact Touch` parameter of this script.
  20.     /// </remarks>
  21.     /// <example>
  22.     /// `VRTK/Examples/005_Controller/BasicObjectGrabbing` demonstrates the grabbing of interactable objects that have the `VRTK_InteractableObject` script attached to them. The objects can be picked up and thrown around.
  23.     ///
  24.     /// `VRTK/Examples/013_Controller_UsingAndGrabbingMultipleObjects` demonstrates that each controller can grab and use objects independently and objects can also be toggled to their use state simultaneously.
  25.     ///
  26.     /// `VRTK/Examples/014_Controller_SnappingObjectsOnGrab` demonstrates the different mechanisms for snapping a grabbed object to the controller.
  27.     /// </example>
  28.     [AddComponentMenu("VRTK/Scripts/Interactions/Interactors/VRTK_InteractGrab")]
  29.     public class VRTK_InteractGrab : MonoBehaviour
  30.     {
  31.         [Header("Grab Settings")]
  32.  
  33.         [Tooltip("The button used to grab/release a touched Interactable Object.")]
  34.         public VRTK_ControllerEvents.ButtonAlias grabButton = VRTK_ControllerEvents.ButtonAlias.GripPress;
  35.         [Tooltip("An amount of time between when the grab button is pressed to when the controller is touching an Interactable Object to grab it.")]
  36.         public float grabPrecognition = 0f;
  37.         [Tooltip("An amount to multiply the velocity of any Interactable Object being thrown.")]
  38.         public float throwMultiplier = 1f;
  39.         [Tooltip("If this is checked and the Interact Touch is not touching an Interactable Object when the grab button is pressed then a Rigidbody is added to the interacting object to allow it to push other Rigidbody objects around.")]
  40.         public bool createRigidBodyWhenNotTouching = false;
  41.  
  42.         [Header("Custom Settings")]
  43.  
  44.         [Tooltip("The rigidbody point on the controller model to snap the grabbed Interactable Object to. If blank it will be set to the SDK default.")]
  45.         public Rigidbody controllerAttachPoint = null;
  46.         [Tooltip("The Controller Events to listen for the events on. If the script is being applied onto a controller then this parameter can be left blank as it will be auto populated by the controller the script is on at runtime.")]
  47.         public VRTK_ControllerEvents controllerEvents;
  48.         [Tooltip("The Interact Touch to listen for touches on. If the script is being applied onto a controller then this parameter can be left blank as it will be auto populated by the controller the script is on at runtime.")]
  49.         public VRTK_InteractTouch interactTouch;
  50.  
  51.         /// <summary>
  52.         /// Emitted when the grab button is pressed.
  53.         /// </summary>
  54.         public event ControllerInteractionEventHandler GrabButtonPressed;
  55.         /// <summary>
  56.         /// Emitted when the grab button is released.
  57.         /// </summary>
  58.         public event ControllerInteractionEventHandler GrabButtonReleased;
  59.  
  60.         /// <summary>
  61.         /// Emitted when a grab of a valid object is started.
  62.         /// </summary>
  63.         public event ObjectInteractEventHandler ControllerStartGrabInteractableObject;
  64.         /// <summary>
  65.         /// Emitted when a valid object is grabbed.
  66.         /// </summary>
  67.         public event ObjectInteractEventHandler ControllerGrabInteractableObject;
  68.         /// <summary>
  69.         /// Emitted when a ungrab of a valid object is started.
  70.         /// </summary>
  71.         public event ObjectInteractEventHandler ControllerStartUngrabInteractableObject;
  72.         /// <summary>
  73.         /// Emitted when a valid object is released from being grabbed.
  74.         /// </summary>
  75.         public event ObjectInteractEventHandler ControllerUngrabInteractableObject;
  76.  
  77.         protected VRTK_ControllerEvents.ButtonAlias subscribedGrabButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  78.         protected VRTK_ControllerEvents.ButtonAlias savedGrabButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  79.         protected bool grabPressed;
  80.  
  81.         protected GameObject grabbedObject = null;
  82.         protected bool influencingGrabbedObject = false;
  83.         protected int grabEnabledState = 0;
  84.         protected float grabPrecognitionTimer = 0f;
  85.         protected GameObject undroppableGrabbedObject;
  86.         protected Rigidbody originalControllerAttachPoint;
  87.  
  88.         protected VRTK_ControllerReference controllerReference
  89.         {
  90.             get
  91.             {
  92.                 return VRTK_ControllerReference.GetControllerReference((interactTouch != null ? interactTouch.gameObject : null));
  93.             }
  94.         }
  95.  
  96.         public virtual void OnControllerStartGrabInteractableObject(ObjectInteractEventArgs e)
  97.         {
  98.             if (ControllerStartGrabInteractableObject != null)
  99.             {
  100.                 ControllerStartGrabInteractableObject(this, e);
  101.             }
  102.         }
  103.  
  104.         public virtual void OnControllerGrabInteractableObject(ObjectInteractEventArgs e)
  105.         {
  106.             if (ControllerGrabInteractableObject != null)
  107.             {
  108.                 ControllerGrabInteractableObject(this, e);
  109.             }
  110.         }
  111.  
  112.         public virtual void OnControllerStartUngrabInteractableObject(ObjectInteractEventArgs e)
  113.         {
  114.             if (ControllerStartUngrabInteractableObject != null)
  115.             {
  116.                 ControllerStartUngrabInteractableObject(this, e);
  117.             }
  118.         }
  119.  
  120.         public virtual void OnControllerUngrabInteractableObject(ObjectInteractEventArgs e)
  121.         {
  122.             if (ControllerUngrabInteractableObject != null)
  123.             {
  124.                 ControllerUngrabInteractableObject(this, e);
  125.             }
  126.         }
  127.  
  128.         public virtual void OnGrabButtonPressed(ControllerInteractionEventArgs e)
  129.         {
  130.             if (GrabButtonPressed != null)
  131.             {
  132.                 GrabButtonPressed(this, e);
  133.             }
  134.         }
  135.  
  136.         public virtual void OnGrabButtonReleased(ControllerInteractionEventArgs e)
  137.         {
  138.             if (GrabButtonReleased != null)
  139.             {
  140.                 GrabButtonReleased(this, e);
  141.             }
  142.         }
  143.  
  144.         /// <summary>
  145.         /// The IsGrabButtonPressed method determines whether the current grab alias button is being pressed down.
  146.         /// </summary>
  147.         /// <returns>Returns `true` if the grab alias button is being held down.</returns>
  148.         public virtual bool IsGrabButtonPressed()
  149.         {
  150.             return grabPressed;
  151.         }
  152.  
  153.         /// <summary>
  154.         /// The ForceRelease method will force the Interact Grab to stop grabbing the currently grabbed Interactable Object.
  155.         /// </summary>
  156.         /// <param name="applyGrabbingObjectVelocity">If this is true then upon releasing the Interactable Object any velocity on the Interact Touch GameObject will be applied to the Interactable Object to essentiall throw it. Defaults to `false`.</param>
  157.         public virtual void ForceRelease(bool applyGrabbingObjectVelocity = false)
  158.         {
  159.             InitUngrabbedObject(applyGrabbingObjectVelocity);
  160.         }
  161.  
  162.         /// <summary>
  163.         /// The AttemptGrab method will attempt to grab the currently touched Interactable Object without needing to press the grab button on the controller.
  164.         /// </summary>
  165.         public virtual void AttemptGrab()
  166.         {
  167.             AttemptGrabObject();
  168.         }
  169.  
  170.         /// <summary>
  171.         /// The GetGrabbedObject method returns the current Interactable Object being grabbed by the this Interact Grab.
  172.         /// </summary>
  173.         /// <returns>The game object of what is currently being grabbed by this controller.</returns>
  174.         public virtual GameObject GetGrabbedObject()
  175.         {
  176.             return grabbedObject;
  177.         }
  178.  
  179.         /// <summary>
  180.         /// The ForceControllerAttachPoint method updates the rigidbody being used as the controller grab attach position.
  181.         /// </summary>
  182.         /// <param name="forcedAttachPoint">The rigidbody to use as the controller attach point.</param>
  183.         public virtual void ForceControllerAttachPoint(Rigidbody forcedAttachPoint)
  184.         {
  185.             originalControllerAttachPoint = forcedAttachPoint;
  186.             controllerAttachPoint = forcedAttachPoint;
  187.         }
  188.  
  189.         protected virtual void Awake()
  190.         {
  191.             originalControllerAttachPoint = controllerAttachPoint;
  192.             controllerEvents = (controllerEvents != null ? controllerEvents : GetComponentInParent<VRTK_ControllerEvents>());
  193.             interactTouch = (interactTouch != null ? interactTouch : GetComponentInParent<VRTK_InteractTouch>());
  194.             if (interactTouch == null)
  195.             {
  196.                 VRTK_Logger.Error(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_NOT_INJECTED, "VRTK_InteractGrab", "VRTK_InteractTouch", "interactTouch", "the same or parent"));
  197.             }
  198.  
  199.             VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
  200.         }
  201.  
  202.         protected virtual void OnEnable()
  203.         {
  204.             RegrabUndroppableObject();
  205.             ManageGrabListener(true);
  206.             ManageInteractTouchListener(true);
  207.             if (controllerEvents != null)
  208.             {
  209.                 controllerEvents.ControllerIndexChanged += DoControllerModelUpdate;
  210.                 controllerEvents.ControllerModelAvailable += DoControllerModelUpdate;
  211.             }
  212.             SetControllerAttachPoint();
  213.         }
  214.  
  215.         protected virtual void OnDisable()
  216.         {
  217.             SetUndroppableObject();
  218.             ForceRelease();
  219.             ManageGrabListener(false);
  220.             ManageInteractTouchListener(false);
  221.             if (controllerEvents != null)
  222.             {
  223.                 controllerEvents.ControllerIndexChanged -= DoControllerModelUpdate;
  224.                 controllerEvents.ControllerModelAvailable -= DoControllerModelUpdate;
  225.             }
  226.         }
  227.  
  228.         protected virtual void OnDestroy()
  229.         {
  230.             VRTK_SDKManager.AttemptRemoveBehaviourToToggleOnLoadedSetupChange(this);
  231.         }
  232.  
  233.         protected virtual void Update()
  234.         {
  235.             ManageGrabListener(true);
  236.             CheckControllerAttachPointSet();
  237.             CreateNonTouchingRigidbody();
  238.             CheckPrecognitionGrab();
  239.         }
  240.  
  241.         protected virtual void DoControllerModelUpdate(object sender, ControllerInteractionEventArgs e)
  242.         {
  243.             SetControllerAttachPoint();
  244.         }
  245.  
  246.         protected virtual void ManageInteractTouchListener(bool state)
  247.         {
  248.             if (interactTouch != null && !state)
  249.             {
  250.                 interactTouch.ControllerTouchInteractableObject -= ControllerTouchInteractableObject;
  251.                 interactTouch.ControllerUntouchInteractableObject -= ControllerUntouchInteractableObject;
  252.             }
  253.  
  254.             if (interactTouch != null && state)
  255.             {
  256.                 interactTouch.ControllerTouchInteractableObject += ControllerTouchInteractableObject;
  257.                 interactTouch.ControllerUntouchInteractableObject += ControllerUntouchInteractableObject;
  258.             }
  259.         }
  260.  
  261.         protected virtual void ControllerTouchInteractableObject(object sender, ObjectInteractEventArgs e)
  262.         {
  263.             if (e.target != null)
  264.             {
  265.                 VRTK_InteractableObject touchedObjectScript = e.target.GetComponent<VRTK_InteractableObject>();
  266.                 if (touchedObjectScript != null && touchedObjectScript.grabOverrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  267.                 {
  268.                     savedGrabButton = subscribedGrabButton;
  269.                     grabButton = touchedObjectScript.grabOverrideButton;
  270.                     ManageGrabListener(true);
  271.                 }
  272.             }
  273.         }
  274.  
  275.         protected virtual void ControllerUntouchInteractableObject(object sender, ObjectInteractEventArgs e)
  276.         {
  277.             if (e.target != null)
  278.             {
  279.                 VRTK_InteractableObject touchedObjectScript = e.target.GetComponent<VRTK_InteractableObject>();
  280.                 if (!touchedObjectScript.IsGrabbed() && savedGrabButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
  281.                 {
  282.                     grabButton = savedGrabButton;
  283.                     savedGrabButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  284.                     ManageGrabListener(true);
  285.                 }
  286.             }
  287.         }
  288.  
  289.         protected virtual void ManageGrabListener(bool state)
  290.         {
  291.             if (controllerEvents != null && subscribedGrabButton != VRTK_ControllerEvents.ButtonAlias.Undefined && (!state || grabButton != subscribedGrabButton))
  292.             {
  293.                 controllerEvents.UnsubscribeToButtonAliasEvent(subscribedGrabButton, true, DoGrabObject);
  294.                 controllerEvents.UnsubscribeToButtonAliasEvent(subscribedGrabButton, false, DoReleaseObject);
  295.                 subscribedGrabButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  296.             }
  297.  
  298.             if (controllerEvents != null && state && grabButton != VRTK_ControllerEvents.ButtonAlias.Undefined && grabButton != subscribedGrabButton)
  299.             {
  300.                 controllerEvents.SubscribeToButtonAliasEvent(grabButton, true, DoGrabObject);
  301.                 controllerEvents.SubscribeToButtonAliasEvent(grabButton, false, DoReleaseObject);
  302.                 subscribedGrabButton = grabButton;
  303.             }
  304.         }
  305.  
  306.         protected virtual void RegrabUndroppableObject()
  307.         {
  308.             if (undroppableGrabbedObject != null)
  309.             {
  310.                 VRTK_InteractableObject undroppableGrabbedObjectScript = undroppableGrabbedObject.GetComponent<VRTK_InteractableObject>();
  311.                 if (interactTouch != null && undroppableGrabbedObjectScript != null && !undroppableGrabbedObjectScript.IsGrabbed())
  312.                 {
  313.                     undroppableGrabbedObject.SetActive(true);
  314.                     interactTouch.ForceTouch(undroppableGrabbedObject);
  315.                     AttemptGrab();
  316.                 }
  317.             }
  318.             else
  319.             {
  320.                 undroppableGrabbedObject = null;
  321.             }
  322.         }
  323.  
  324.         protected virtual void SetUndroppableObject()
  325.         {
  326.             if (undroppableGrabbedObject != null)
  327.             {
  328.                 VRTK_InteractableObject undroppableGrabbedObjectScript = undroppableGrabbedObject.GetComponent<VRTK_InteractableObject>();
  329.                 if (undroppableGrabbedObjectScript != null && undroppableGrabbedObjectScript.IsDroppable())
  330.                 {
  331.                     undroppableGrabbedObject = null;
  332.                 }
  333.                 else
  334.                 {
  335.                     undroppableGrabbedObject.SetActive(false);
  336.                 }
  337.             }
  338.         }
  339.  
  340.         protected virtual void SetControllerAttachPoint()
  341.         {
  342.             //If no attach point has been specified then just use the tip of the controller
  343.             if (controllerReference.model != null && originalControllerAttachPoint == null)
  344.             {
  345.                 //attempt to find the attach point on the controller
  346.                 SDK_BaseController.ControllerHand handType = VRTK_DeviceFinder.GetControllerHand(interactTouch.gameObject);
  347.                 string elementPath = VRTK_SDK_Bridge.GetControllerElementPath(SDK_BaseController.ControllerElements.AttachPoint, handType);
  348.                 Transform defaultAttachPoint = controllerReference.model.transform.Find(elementPath);
  349.  
  350.                 if (defaultAttachPoint != null)
  351.                 {
  352.                     controllerAttachPoint = defaultAttachPoint.GetComponent<Rigidbody>();
  353.  
  354.                     if (controllerAttachPoint == null)
  355.                     {
  356.                         Rigidbody autoGenRB = defaultAttachPoint.gameObject.AddComponent<Rigidbody>();
  357.                         autoGenRB.isKinematic = true;
  358.                         controllerAttachPoint = autoGenRB;
  359.                     }
  360.                 }
  361.             }
  362.         }
  363.  
  364.         protected virtual bool IsObjectGrabbable(GameObject obj)
  365.         {
  366.             VRTK_InteractableObject objScript = obj.GetComponent<VRTK_InteractableObject>();
  367.             return (interactTouch != null && interactTouch.IsObjectInteractable(obj) && objScript != null && (objScript.isGrabbable || objScript.PerformSecondaryAction()));
  368.         }
  369.  
  370.         protected virtual bool IsObjectHoldOnGrab(GameObject obj)
  371.         {
  372.             if (obj != null)
  373.             {
  374.                 VRTK_InteractableObject objScript = obj.GetComponent<VRTK_InteractableObject>();
  375.                 return (objScript != null && objScript.holdButtonToGrab);
  376.             }
  377.             return false;
  378.         }
  379.  
  380.         protected virtual void ChooseGrabSequence(VRTK_InteractableObject grabbedObjectScript)
  381.         {
  382.             if (!grabbedObjectScript.IsGrabbed() || grabbedObjectScript.IsSwappable())
  383.             {
  384.                 InitPrimaryGrab(grabbedObjectScript);
  385.             }
  386.             else
  387.             {
  388.                 InitSecondaryGrab(grabbedObjectScript);
  389.             }
  390.         }
  391.  
  392.         protected virtual void ToggleControllerVisibility(bool visible)
  393.         {
  394.             if (grabbedObject != null)
  395.             {
  396.                 ///[Obsolete]
  397. #pragma warning disable 0618
  398.                 VRTK_InteractControllerAppearance[] controllerAppearanceScript = grabbedObject.GetComponentsInParent<VRTK_InteractControllerAppearance>(true);
  399. #pragma warning restore 0618
  400.                 if (controllerAppearanceScript.Length > 0)
  401.                 {
  402.                     controllerAppearanceScript[0].ToggleControllerOnGrab(visible, controllerReference.model, grabbedObject);
  403.                 }
  404.             }
  405.             else if (visible)
  406.             {
  407.                 VRTK_ObjectAppearance.SetRendererVisible(controllerReference.model, grabbedObject);
  408.             }
  409.         }
  410.  
  411.         protected virtual void InitGrabbedObject()
  412.         {
  413.             grabbedObject = (interactTouch != null ? interactTouch.GetTouchedObject() : null);
  414.             if (grabbedObject != null)
  415.             {
  416.                 OnControllerStartGrabInteractableObject(interactTouch.SetControllerInteractEvent(grabbedObject));
  417.                 VRTK_InteractableObject grabbedObjectScript = grabbedObject.GetComponent<VRTK_InteractableObject>();
  418.                 ChooseGrabSequence(grabbedObjectScript);
  419.                 ToggleControllerVisibility(false);
  420.                 OnControllerGrabInteractableObject(interactTouch.SetControllerInteractEvent(grabbedObject));
  421.             }
  422.         }
  423.  
  424.         protected virtual void InitPrimaryGrab(VRTK_InteractableObject currentGrabbedObject)
  425.         {
  426.             if (!currentGrabbedObject.IsValidInteractableController(gameObject, currentGrabbedObject.allowedGrabControllers))
  427.             {
  428.                 grabbedObject = null;
  429.                 if (interactTouch != null && currentGrabbedObject.IsGrabbed(gameObject))
  430.                 {
  431.                     interactTouch.ForceStopTouching();
  432.                 }
  433.                 return;
  434.             }
  435.  
  436.             influencingGrabbedObject = false;
  437.             currentGrabbedObject.SaveCurrentState();
  438.             currentGrabbedObject.Grabbed(this);
  439.             currentGrabbedObject.ZeroVelocity();
  440.             currentGrabbedObject.isKinematic = false;
  441.         }
  442.  
  443.         protected virtual void InitSecondaryGrab(VRTK_InteractableObject currentGrabbedObject)
  444.         {
  445.             influencingGrabbedObject = true;
  446.             currentGrabbedObject.Grabbed(this);
  447.         }
  448.  
  449.         protected virtual void CheckInfluencingObjectOnRelease()
  450.         {
  451.             if (!influencingGrabbedObject && interactTouch != null)
  452.             {
  453.                 interactTouch.ForceStopTouching();
  454.                 ToggleControllerVisibility(true);
  455.             }
  456.             influencingGrabbedObject = false;
  457.         }
  458.  
  459.         protected virtual void InitUngrabbedObject(bool applyGrabbingObjectVelocity)
  460.         {
  461.             if (grabbedObject != null && interactTouch != null)
  462.             {
  463.                 OnControllerStartUngrabInteractableObject(interactTouch.SetControllerInteractEvent(grabbedObject));
  464.                 VRTK_InteractableObject grabbedObjectScript = grabbedObject.GetComponent<VRTK_InteractableObject>();
  465.                 if (grabbedObjectScript != null)
  466.                 {
  467.                     if (!influencingGrabbedObject)
  468.                     {
  469.                         grabbedObjectScript.grabAttachMechanicScript.StopGrab(applyGrabbingObjectVelocity);
  470.                     }
  471.                     grabbedObjectScript.Ungrabbed(this);
  472.                     ToggleControllerVisibility(true);
  473.  
  474.                     OnControllerUngrabInteractableObject(interactTouch.SetControllerInteractEvent(grabbedObject));
  475.                 }
  476.             }
  477.  
  478.             CheckInfluencingObjectOnRelease();
  479.  
  480.             grabEnabledState = 0;
  481.             grabbedObject = null;
  482.         }
  483.  
  484.         protected virtual GameObject GetGrabbableObject()
  485.         {
  486.             GameObject obj = (interactTouch != null ? interactTouch.GetTouchedObject() : null);
  487.             if (obj != null && interactTouch.IsObjectInteractable(obj))
  488.             {
  489.                 return obj;
  490.             }
  491.             return grabbedObject;
  492.         }
  493.  
  494.         protected virtual void IncrementGrabState()
  495.         {
  496.             if (interactTouch != null && !IsObjectHoldOnGrab(interactTouch.GetTouchedObject()))
  497.             {
  498.                 grabEnabledState++;
  499.             }
  500.         }
  501.  
  502.         protected virtual GameObject GetUndroppableObject()
  503.         {
  504.             if (grabbedObject != null)
  505.             {
  506.                 VRTK_InteractableObject grabbedObjectScript = grabbedObject.GetComponent<VRTK_InteractableObject>();
  507.                 return (grabbedObjectScript != null && !grabbedObjectScript.IsDroppable() ? grabbedObject : null);
  508.             }
  509.             return null;
  510.         }
  511.  
  512.         protected virtual void AttemptGrabObject()
  513.         {
  514.             GameObject objectToGrab = GetGrabbableObject();
  515.             if (objectToGrab != null)
  516.             {
  517.                 PerformGrabAttempt(objectToGrab);
  518.             }
  519.             else
  520.             {
  521.                 grabPrecognitionTimer = Time.time + grabPrecognition;
  522.             }
  523.         }
  524.  
  525.         protected virtual void PerformGrabAttempt(GameObject objectToGrab)
  526.         {
  527.             IncrementGrabState();
  528.             IsValidGrabAttempt(objectToGrab);
  529.             undroppableGrabbedObject = GetUndroppableObject();
  530.         }
  531.  
  532.         protected virtual bool ScriptValidGrab(VRTK_InteractableObject objectToGrabScript)
  533.         {
  534.             return (objectToGrabScript != null && objectToGrabScript.grabAttachMechanicScript != null && objectToGrabScript.grabAttachMechanicScript.ValidGrab(controllerAttachPoint));
  535.         }
  536.  
  537.         protected virtual bool IsValidGrabAttempt(GameObject objectToGrab)
  538.         {
  539.             bool initialGrabAttempt = false;
  540.             VRTK_InteractableObject objectToGrabScript = (objectToGrab != null ? objectToGrab.GetComponent<VRTK_InteractableObject>() : null);
  541.             if (grabbedObject == null && interactTouch != null && IsObjectGrabbable(interactTouch.GetTouchedObject()) && ScriptValidGrab(objectToGrabScript))
  542.             {
  543.                 InitGrabbedObject();
  544.                 if (!influencingGrabbedObject)
  545.                 {
  546.                     initialGrabAttempt = objectToGrabScript.grabAttachMechanicScript.StartGrab(gameObject, grabbedObject, controllerAttachPoint);
  547.                 }
  548.             }
  549.             return initialGrabAttempt;
  550.         }
  551.  
  552.         protected virtual bool CanRelease()
  553.         {
  554.             if (grabbedObject != null)
  555.             {
  556.                 VRTK_InteractableObject objectToGrabScript = grabbedObject.GetComponent<VRTK_InteractableObject>();
  557.                 return (objectToGrabScript != null && objectToGrabScript.IsDroppable());
  558.             }
  559.             return false;
  560.         }
  561.  
  562.         protected virtual void AttemptReleaseObject()
  563.         {
  564.             if (CanRelease() && (IsObjectHoldOnGrab(grabbedObject) || grabEnabledState >= 2))
  565.             {
  566.                 InitUngrabbedObject(true);
  567.             }
  568.         }
  569.  
  570.         protected virtual void DoGrabObject(object sender, ControllerInteractionEventArgs e)
  571.         {
  572.             OnGrabButtonPressed(controllerEvents.SetControllerEvent(ref grabPressed, true));
  573.             AttemptGrabObject();
  574.         }
  575.  
  576.         protected virtual void DoReleaseObject(object sender, ControllerInteractionEventArgs e)
  577.         {
  578.             AttemptReleaseObject();
  579.             OnGrabButtonReleased(controllerEvents.SetControllerEvent(ref grabPressed, false));
  580.         }
  581.  
  582.         protected virtual void CheckControllerAttachPointSet()
  583.         {
  584.             if (controllerAttachPoint == null)
  585.             {
  586.                 SetControllerAttachPoint();
  587.             }
  588.         }
  589.  
  590.         protected virtual void CreateNonTouchingRigidbody()
  591.         {
  592.             if (createRigidBodyWhenNotTouching && grabbedObject == null && interactTouch != null)
  593.             {
  594.                 if (!interactTouch.IsRigidBodyForcedActive() && interactTouch.IsRigidBodyActive() != grabPressed)
  595.                 {
  596.                     interactTouch.ToggleControllerRigidBody(grabPressed);
  597.                 }
  598.             }
  599.         }
  600.  
  601.         protected virtual void CheckPrecognitionGrab()
  602.         {
  603.             if (grabPrecognitionTimer >= Time.time)
  604.             {
  605.                 if (GetGrabbableObject() != null)
  606.                 {
  607.                     AttemptGrabObject();
  608.                     if (GetGrabbedObject() != null)
  609.                     {
  610.                         grabPrecognitionTimer = 0f;
  611.                     }
  612.                 }
  613.             }
  614.         }
  615.     }
  616. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement