Skip to content

Commit

Permalink
fix(Interaction): ensure undroppable objects cannot be dropped
Browse files Browse the repository at this point in the history
If the Interactable Object is set as `isDroppable = false` then it
should not be able to drop the object by the joint breaking or
the object/controller distance being too far. It should also not
drop if the controller is disabled but it will drop if the
Interactable Object is disabled.

The way the prevent drop works on controller disable is by storing
a reference to the grabbed object then auto grabbing it again
when the controller is re-enabled.
  • Loading branch information
thestonefox committed Sep 9, 2016
1 parent dcb8f0f commit 0eeb7a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 26 additions & 1 deletion Assets/VRTK/Scripts/VRTK_InteractGrab.cs
Expand Up @@ -57,6 +57,7 @@ public class VRTK_InteractGrab : MonoBehaviour
private VRTK_ControllerEvents controllerEvents;
private int grabEnabledState = 0;
private float grabPrecognitionTimer = 0f;
private GameObject undroppableGrabbedObject;

public virtual void OnControllerGrabInteractableObject(ObjectInteractEventArgs e)
{
Expand Down Expand Up @@ -127,6 +128,17 @@ private void OnEnable()
return;
}

if (undroppableGrabbedObject && !undroppableGrabbedObject.GetComponent<VRTK_InteractableObject>().IsGrabbed())
{
undroppableGrabbedObject.SetActive(true);
interactTouch.ForceTouch(undroppableGrabbedObject);
AttemptGrab();
}
else
{
undroppableGrabbedObject = null;
}

GetComponent<VRTK_ControllerEvents>().AliasGrabOn += new ControllerInteractionEventHandler(DoGrabObject);
GetComponent<VRTK_ControllerEvents>().AliasGrabOff += new ControllerInteractionEventHandler(DoReleaseObject);

Expand All @@ -135,6 +147,17 @@ private void OnEnable()

private void OnDisable()
{
if (undroppableGrabbedObject)
{
if (undroppableGrabbedObject.GetComponent<VRTK_InteractableObject>().isDroppable)
{
undroppableGrabbedObject = null;
}
else
{
undroppableGrabbedObject.SetActive(false);
}
}
ForceRelease();
GetComponent<VRTK_ControllerEvents>().AliasGrabOn -= new ControllerInteractionEventHandler(DoGrabObject);
GetComponent<VRTK_ControllerEvents>().AliasGrabOff -= new ControllerInteractionEventHandler(DoReleaseObject);
Expand Down Expand Up @@ -252,7 +275,7 @@ private void CreateJoint(GameObject obj)
}
controllerAttachJoint = tempSpringJoint;
}
controllerAttachJoint.breakForce = objectScript.detachThreshold;
controllerAttachJoint.breakForce = (objectScript.isDroppable ? objectScript.detachThreshold : Mathf.Infinity);
controllerAttachJoint.connectedBody = controllerAttachPoint;
}

Expand Down Expand Up @@ -489,6 +512,8 @@ private void AttemptGrabObject()
initialGrabAttempt = GrabInteractedObject();
}

undroppableGrabbedObject = (grabbedObject && grabbedObject.GetComponent<VRTK_InteractableObject>() && !grabbedObject.GetComponent<VRTK_InteractableObject>().isDroppable ? grabbedObject : null);

if (grabbedObject && initialGrabAttempt)
{
var rumbleAmount = grabbedObject.GetComponent<VRTK_InteractableObject>().rumbleOnGrab;
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/Scripts/VRTK_InteractableObject.cs
Expand Up @@ -766,7 +766,7 @@ private void ChangeColor(Dictionary<string, Color[]> colors)

private void CheckBreakDistance()
{
if (trackPoint)
if (trackPoint && isDroppable)
{
float distance = Vector3.Distance(trackPoint.position, originalControllerAttachPoint.position);
if (distance > (detachThreshold / 1000))
Expand Down

0 comments on commit 0eeb7a3

Please sign in to comment.