Skip to content

Commit

Permalink
fix(Interaction): ensure autograb always happens on enable
Browse files Browse the repository at this point in the history
Previously, Autograb would only happen on Start which meant that after
a controller was disabled and re-enabled the Autograb would not take
place again. This has been rectified by moving the Autograb logic into
the `OnEnable` method.
  • Loading branch information
thestonefox committed Aug 24, 2016
1 parent 5872489 commit 823c4d9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 43 deletions.
5 changes: 5 additions & 0 deletions Assets/VRTK/Scripts/VRTK_InteractableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ public bool AttachIsClimbObject()
return (grabAttachMechanic == GrabAttachType.Climbable);
}

public bool AttachIsKinematicObject()
{
return (grabAttachMechanic == GrabAttachType.Child_Of_Controller);
}

public bool AttachIsStaticObject()
{
return AttachIsClimbObject(); // only one at the moment
Expand Down
37 changes: 31 additions & 6 deletions Assets/VRTK/Scripts/VRTK_ObjectAutoGrab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ public class VRTK_ObjectAutoGrab : MonoBehaviour
public bool cloneGrabbedObject;

private VRTK_InteractGrab controller;
private VRTK_InteractableObject grabbableObject;
private VRTK_InteractableObject previousClonedObject;

private IEnumerator Start()
private void OnEnable()
{
StartCoroutine(AutoGrab());
}

private IEnumerator AutoGrab()
{
controller = GetComponent<VRTK_InteractGrab>();
if (!controller)
Expand All @@ -30,14 +37,32 @@ private IEnumerator Start()
yield return true;
}

VRTK_InteractableObject grabbableObject = objectToGrab;
grabbableObject = objectToGrab;
if (cloneGrabbedObject)
{
grabbableObject = Instantiate(objectToGrab);
if (previousClonedObject == null)
{
grabbableObject = Instantiate(objectToGrab);
previousClonedObject = grabbableObject;
}
else
{
grabbableObject = previousClonedObject;
}
}

if (grabbableObject.isGrabbable && !grabbableObject.IsGrabbed())
{
if (grabbableObject.AttachIsKinematicObject())
{
grabbableObject.ToggleKinematic(true);
}

grabbableObject.transform.position = transform.position;
controller.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
controller.GetComponent<VRTK_InteractTouch>().ForceTouch(grabbableObject.gameObject);
controller.AttemptGrab();
}
controller.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
controller.GetComponent<VRTK_InteractTouch>().ForceTouch(grabbableObject.gameObject);
controller.AttemptGrab();
}
}
}
Loading

0 comments on commit 823c4d9

Please sign in to comment.