Skip to content

Commit

Permalink
fix(Interaction): force stop interacting on object when disabled
Browse files Browse the repository at this point in the history
The Interactable Object script will now sever any interactions if the
game object is disabled. So if an object is being touched, grabbed or
used and then disabled, all of these interactions will be cancelled.

This means when the object is re-enabled it will be dropped as it is
no longer being grabbed for example.

This interaction server is a public method on the Interactable Object
so other scripts call call `ForceStopInteracting` on an object to
auto force interactions to end.

To accompany this, two additional cancel interaction methods have been
added to Interact Touch and Interact Use:

  * `ForceStopTouching` will cancel any touch that is taking place.
  * `ForceStopUsing` will cancel any use action that is taking place.

The Interactable Object will also check in it's `Update` routine to
see if it is still active in the hierarchy and if a parent is disabled
then the `ForceStopInteracting` method is called.
  • Loading branch information
thestonefox committed Jun 30, 2016
1 parent a219347 commit 7820b53
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractGrab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public virtual void OnControllerUngrabInteractableObject(ObjectInteractEventArgs

public void ForceRelease()
{
if (grabbedObject && grabbedObject.GetComponent<VRTK_InteractableObject>() && grabbedObject.GetComponent<VRTK_InteractableObject>().AttachIsTrackObject())
if (grabbedObject != null && grabbedObject.GetComponent<VRTK_InteractableObject>() && grabbedObject.GetComponent<VRTK_InteractableObject>().AttachIsTrackObject())
{
UngrabTrackedObject();
}
Expand Down
51 changes: 32 additions & 19 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractTouch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public void ToggleControllerRigidBody(bool state)
}
}

public void ForceStopTouching()
{
if (touchedObject != null)
{
StopTouching(touchedObject);
}
}

private void Awake()
{
trackedController = GetComponent<SteamVR_TrackedObject>();
Expand Down Expand Up @@ -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<VRTK_InteractableObject>())
{
GameObject untouched;
if (collider.gameObject.GetComponent<VRTK_InteractableObject>())
{
untouched = collider.gameObject;
}
else
{
untouched = collider.gameObject.GetComponentInParent<VRTK_InteractableObject>().gameObject;
}

OnControllerUntouchInteractableObject(SetControllerInteractEvent(untouched.gameObject));
untouched.GetComponent<VRTK_InteractableObject>().ToggleHighlight(false);
untouched.GetComponent<VRTK_InteractableObject>().StopTouching(this.gameObject);
untouched = obj;
}

if (hideControllerOnTouch)
else
{
controllerActions.ToggleControllerModel(true, touchedObject);
untouched = obj.GetComponentInParent<VRTK_InteractableObject>().gameObject;
}
touchedObject = null;

OnControllerUntouchInteractableObject(SetControllerInteractEvent(untouched.gameObject));
untouched.GetComponent<VRTK_InteractableObject>().ToggleHighlight(false);
untouched.GetComponent<VRTK_InteractableObject>().StopTouching(this.gameObject);
}

if (hideControllerOnTouch)
{
controllerActions.ToggleControllerModel(true, touchedObject);
}
touchedObject = null;
}

private void CreateTouchCollider(GameObject obj)
Expand Down
17 changes: 15 additions & 2 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractUse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public GameObject GetUsingObject()
return usingObject;
}

public void ForceStopUsing()
{
if(usingObject != null)
{
StopUsing();
}
}

private void Awake()
{
if (GetComponent<VRTK_InteractTouch>() == null)
Expand Down Expand Up @@ -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();
Expand All @@ -181,8 +195,7 @@ private void DoStopUseObject(object sender, ControllerInteractionEventArgs e)
{
if (IsObjectHoldOnUse(usingObject) || GetObjectUsingState(usingObject) >= 2)
{
SetObjectUsingState(usingObject, 0);
UnuseInteractedObject();
StopUsing();
}
}
}
Expand Down
54 changes: 41 additions & 13 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Color> originalObjectColours;

Expand Down Expand Up @@ -138,7 +138,7 @@ public InteractableObjectEventArgs SetInteractableObjectEvent(GameObject interac

public bool IsTouched()
{
return isTouched;
return (touchingObject != null);
}

public bool IsGrabbed()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -293,6 +293,24 @@ public bool IsValidInteractableController(GameObject actualController, AllowedCo
return (DeviceFinder.IsControllerOfHand(actualController, controllerHand));
}

public void ForceStopInteracting()
{
if (touchingObject != null)
{
touchingObject.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
}

if (grabbingObject != null)
{
grabbingObject.GetComponent<VRTK_InteractGrab>().ForceRelease();
}

if (usingObject != null)
{
usingObject.GetComponent<VRTK_InteractUse>().ForceStopUsing();
}
}

protected virtual void Awake()
{
rb = this.GetComponent<Rigidbody>();
Expand All @@ -312,6 +330,11 @@ protected virtual void Start()

protected virtual void Update()
{
if(! this.gameObject.activeInHierarchy)
{
ForceStopInteracting();
}

if (grabAttachMechanic == GrabAttachType.Track_Object)
{
CheckBreakDistance();
Expand All @@ -326,6 +349,11 @@ protected virtual void FixedUpdate()
}
}

protected virtual void OnDisable()
{
ForceStopInteracting();
}

protected virtual void OnJointBreak(float force)
{
ForceReleaseGrab();
Expand Down

0 comments on commit 7820b53

Please sign in to comment.