Skip to content

Commit

Permalink
fix(Controller): enhance AutoGrab script
Browse files Browse the repository at this point in the history
AutoGrab would fail if the controllers initially already touched
something. This is now fixed.

Also stronger typing has been introduced and the script flow fixed so
that now error messages also appear in the console.
  • Loading branch information
rwetzold committed Aug 17, 2016
1 parent ffe142f commit ba26ec5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Assets/SteamVR_Unity_Toolkit/Scripts/VRTK_ObjectAutoGrab.cs
Expand Up @@ -5,36 +5,38 @@

public class VRTK_ObjectAutoGrab : MonoBehaviour
{
public GameObject objectToGrab;
public VRTK_InteractableObject objectToGrab;
public bool cloneGrabbedObject;

private VRTK_InteractGrab controller;

private IEnumerator Start()
{
controller = GetComponent<VRTK_InteractGrab>();

if (!controller)
{
Debug.LogError("The VRTK_InteractGrab script is required to be attached to the controller along with this script.");
yield break;
}

if (!objectToGrab || !objectToGrab.GetComponent<VRTK_InteractableObject>())
if (!objectToGrab)
{
Debug.LogError("The objectToGrab Game Object must have the VRTK_InteractableObject script applied to it.");
Debug.LogError("You have to assign an object that should be grabbed.");
yield break;
}

while (controller.controllerAttachPoint == null)
{
yield return true;
}

var grabbableObject = objectToGrab;
VRTK_InteractableObject grabbableObject = objectToGrab;
if (cloneGrabbedObject)
{
grabbableObject = Instantiate(objectToGrab);
}
controller.GetComponent<VRTK_InteractTouch>().ForceTouch(grabbableObject);
controller.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
controller.GetComponent<VRTK_InteractTouch>().ForceTouch(grabbableObject.gameObject);
controller.AttemptGrab();
}
}
Expand Down

0 comments on commit ba26ec5

Please sign in to comment.