Skip to content

Commit

Permalink
fix(Interaction): ensure touched object is always set after quick grab
Browse files Browse the repository at this point in the history
There was an issue when grabbing and releasing an object then quickly
grabbing again would leave the touchedObject in a delayed state of
not being touched, meaning the use method on an object would not
trigger because it thought the object being grabbed wasn't being
touched. It would catch up a second or so later, but meant that
objects would feel like they couldn't be used on first pick up.

This commit now resolves the issue by checking on the use method if
the touched object is null then do a sanity check on the grabbed
object.
  • Loading branch information
thestonefox committed Jun 28, 2016
1 parent 7187fed commit f30ac8c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
51 changes: 31 additions & 20 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractTouch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public struct ObjectInteractEventArgs
[RequireComponent(typeof(VRTK_ControllerActions))]
public class VRTK_InteractTouch : MonoBehaviour
{

public bool hideControllerOnTouch = false;
public float hideControllerDelay = 0f;
public Color globalTouchHighlightColor = Color.clear;
Expand Down Expand Up @@ -102,6 +101,11 @@ private void Start()
CreateControllerRigidBody();
}

private void OnTriggerEnter(Collider collider)
{
OnTriggerStay(collider);
}

private void OnTriggerStay(Collider collider)
{
if (touchedObject == null && IsObjectInteractable(collider.gameObject))
Expand Down Expand Up @@ -140,35 +144,42 @@ private void OnTriggerStay(Collider collider)
}
}

private void OnTriggerExit(Collider collider)
private bool IsColliderChildOfTouchedObject(GameObject collider)
{
if (touchedObject == null)
if (touchedObject != null && collider.GetComponentInParent<VRTK_InteractableObject>() && collider.GetComponentInParent<VRTK_InteractableObject>().gameObject == touchedObject)
{
return;
return true;
}
return false;
}

if (IsObjectInteractable(collider.gameObject))
private void OnTriggerExit(Collider collider)
{
if(touchedObject != null && (touchedObject == collider.gameObject || IsColliderChildOfTouchedObject(collider.gameObject)))
{
GameObject untouched;
if (collider.gameObject.GetComponent<VRTK_InteractableObject>())
if (IsObjectInteractable(collider.gameObject))
{
untouched = collider.gameObject;
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);
}
else

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

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;
}
touchedObject = null;
}

private void CreateTouchCollider(GameObject obj)
Expand Down
14 changes: 14 additions & 0 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_InteractUse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,23 @@ private void UnuseInteractedObject()
}
}

private GameObject GetFromGrab()
{
if (this.GetComponent<VRTK_InteractGrab>())
{
return this.GetComponent<VRTK_InteractGrab>().GetGrabbedObject();
}
return null;
}

private void DoStartUseObject(object sender, ControllerInteractionEventArgs e)
{
GameObject touchedObject = interactTouch.GetTouchedObject();
if(touchedObject == null)
{
touchedObject = GetFromGrab();
}

if (touchedObject != null && interactTouch.IsObjectInteractable(touchedObject))
{
UseInteractedObject(touchedObject);
Expand Down

0 comments on commit f30ac8c

Please sign in to comment.