diff --git a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractGrab.cs b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractGrab.cs index 22454f825..eb7b774ec 100644 --- a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractGrab.cs +++ b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractGrab.cs @@ -53,7 +53,7 @@ public virtual void OnControllerUngrabInteractableObject(ObjectInteractEventArgs public void ForceRelease() { - if (grabbedObject && grabbedObject.GetComponent() && grabbedObject.GetComponent().AttachIsTrackObject()) + if (grabbedObject != null && grabbedObject.GetComponent() && grabbedObject.GetComponent().AttachIsTrackObject()) { UngrabTrackedObject(); } diff --git a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractTouch.cs b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractTouch.cs index f5420729c..e27626e4f 100644 --- a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractTouch.cs +++ b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractTouch.cs @@ -83,6 +83,14 @@ public void ToggleControllerRigidBody(bool state) } } + public void ForceStopTouching() + { + if (touchedObject != null) + { + StopTouching(touchedObject); + } + } + private void Awake() { trackedController = GetComponent(); @@ -155,31 +163,36 @@ private bool IsColliderChildOfTouchedObject(GameObject collider) private void OnTriggerExit(Collider collider) { - if(touchedObject != null && (touchedObject == collider.gameObject || IsColliderChildOfTouchedObject(collider.gameObject))) + if (touchedObject != null && (touchedObject == collider.gameObject || IsColliderChildOfTouchedObject(collider.gameObject))) { - if (IsObjectInteractable(collider.gameObject)) + StopTouching(collider.gameObject); + } + } + + private void StopTouching(GameObject obj) + { + if (IsObjectInteractable(obj)) + { + GameObject untouched; + if (obj.GetComponent()) { - GameObject untouched; - if (collider.gameObject.GetComponent()) - { - untouched = collider.gameObject; - } - else - { - untouched = collider.gameObject.GetComponentInParent().gameObject; - } - - OnControllerUntouchInteractableObject(SetControllerInteractEvent(untouched.gameObject)); - untouched.GetComponent().ToggleHighlight(false); - untouched.GetComponent().StopTouching(this.gameObject); + untouched = obj; } - - if (hideControllerOnTouch) + else { - controllerActions.ToggleControllerModel(true, touchedObject); + untouched = obj.GetComponentInParent().gameObject; } - touchedObject = null; + + OnControllerUntouchInteractableObject(SetControllerInteractEvent(untouched.gameObject)); + untouched.GetComponent().ToggleHighlight(false); + untouched.GetComponent().StopTouching(this.gameObject); + } + + if (hideControllerOnTouch) + { + controllerActions.ToggleControllerModel(true, touchedObject); } + touchedObject = null; } private void CreateTouchCollider(GameObject obj) diff --git a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractUse.cs b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractUse.cs index b3b4727fc..89a5ec3c1 100644 --- a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractUse.cs +++ b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractUse.cs @@ -45,6 +45,14 @@ public GameObject GetUsingObject() return usingObject; } + public void ForceStopUsing() + { + if(usingObject != null) + { + StopUsing(); + } + } + private void Awake() { if (GetComponent() == null) @@ -159,6 +167,12 @@ private GameObject GetFromGrab() return null; } + private void StopUsing() + { + SetObjectUsingState(usingObject, 0); + UnuseInteractedObject(); + } + private void DoStartUseObject(object sender, ControllerInteractionEventArgs e) { GameObject touchedObject = interactTouch.GetTouchedObject(); @@ -181,8 +195,7 @@ private void DoStopUseObject(object sender, ControllerInteractionEventArgs e) { if (IsObjectHoldOnUse(usingObject) || GetObjectUsingState(usingObject) >= 2) { - SetObjectUsingState(usingObject, 0); - UnuseInteractedObject(); + StopUsing(); } } } diff --git a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractableObject.cs b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractableObject.cs index f5c35f89a..60eff673a 100644 --- a/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractableObject.cs +++ b/Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractableObject.cs @@ -79,10 +79,10 @@ public enum AllowedController public event InteractableObjectEventHandler InteractableObjectUnused; protected Rigidbody rb; + protected GameObject touchingObject = null; protected GameObject grabbingObject = null; + protected GameObject usingObject = null; - private bool isTouched = false; - private bool isUsing = false; private int usingState = 0; private Dictionary originalObjectColours; @@ -138,7 +138,7 @@ public InteractableObjectEventArgs SetInteractableObjectEvent(GameObject interac public bool IsTouched() { - return isTouched; + return (touchingObject != null); } public bool IsGrabbed() @@ -148,19 +148,19 @@ public bool IsGrabbed() public bool IsUsing() { - return isUsing; + return (usingObject != null); } - public virtual void StartTouching(GameObject touchingObject) + public virtual void StartTouching(GameObject currentTouchingObject) { - OnInteractableObjectTouched(SetInteractableObjectEvent(touchingObject)); - isTouched = true; + OnInteractableObjectTouched(SetInteractableObjectEvent(currentTouchingObject)); + touchingObject = currentTouchingObject; } public virtual void StopTouching(GameObject previousTouchingObject) { OnInteractableObjectUntouched(SetInteractableObjectEvent(previousTouchingObject)); - isTouched = false; + touchingObject = null; } public virtual void Grabbed(GameObject currentGrabbingObject) @@ -185,16 +185,16 @@ public virtual void Ungrabbed(GameObject previousGrabbingObject) LoadPreviousState(); } - public virtual void StartUsing(GameObject usingObject) + public virtual void StartUsing(GameObject currentUsingObject) { - OnInteractableObjectUsed(SetInteractableObjectEvent(usingObject)); - isUsing = true; + OnInteractableObjectUsed(SetInteractableObjectEvent(currentUsingObject)); + usingObject = currentUsingObject; } public virtual void StopUsing(GameObject previousUsingObject) { OnInteractableObjectUnused(SetInteractableObjectEvent(previousUsingObject)); - isUsing = false; + usingObject = null; } public virtual void ToggleHighlight(bool toggle) @@ -206,7 +206,7 @@ public virtual void ToggleHighlight(bool toggle, Color globalHighlightColor) { if (highlightOnTouch) { - if (toggle && !IsGrabbed() && !isUsing) + if (toggle && !IsGrabbed() && !IsUsing()) { Color color = (touchHighlightColor != Color.clear ? touchHighlightColor : globalHighlightColor); if (color != Color.clear) @@ -293,6 +293,24 @@ public bool IsValidInteractableController(GameObject actualController, AllowedCo return (DeviceFinder.IsControllerOfHand(actualController, controllerHand)); } + public void ForceStopInteracting() + { + if (touchingObject != null) + { + touchingObject.GetComponent().ForceStopTouching(); + } + + if (grabbingObject != null) + { + grabbingObject.GetComponent().ForceRelease(); + } + + if (usingObject != null) + { + usingObject.GetComponent().ForceStopUsing(); + } + } + protected virtual void Awake() { rb = this.GetComponent(); @@ -312,6 +330,11 @@ protected virtual void Start() protected virtual void Update() { + if(! this.gameObject.activeInHierarchy) + { + ForceStopInteracting(); + } + if (grabAttachMechanic == GrabAttachType.Track_Object) { CheckBreakDistance(); @@ -326,6 +349,11 @@ protected virtual void FixedUpdate() } } + protected virtual void OnDisable() + { + ForceStopInteracting(); + } + protected virtual void OnJointBreak(float force) { ForceReleaseGrab();