Skip to content

Instantly share code, notes, and snippets.

@chrisforbes
Created January 22, 2017 05:03
Show Gist options
  • Save chrisforbes/f1e19fccd629f25a43433135d7d9f991 to your computer and use it in GitHub Desktop.
Save chrisforbes/f1e19fccd629f25a43433135d7d9f991 to your computer and use it in GitHub Desktop.
void Update()
{
if (controller.GetPressDown(Valve.VR.EVRButtonId.k_EButton_Grip) && pickUp != null)
{
var rb = pickUp.GetComponent<Rigidbody>();
if (rb.isKinematic)
{
// this constraint will allow us to pull/push ourselves through the space.
var rootGameObject = GetRoot(gameObject).gameObject;
if (joint2 == null)
joint2 = rootGameObject.AddComponent<ConfigurableJoint>();
joint2.connectedBody = null;
joint2.anchor = transform.localPosition;
grabPoint = transform.localPosition;
grabOrientation = transform.localRotation;
joint2.connectedAnchor = new Vector3(0, 0, 0);
joint2.autoConfigureConnectedAnchor = false;
joint2.configuredInWorldSpace = false;
ConfigureJoint(rootGameObject.GetComponent<Rigidbody>().mass, true, joint2);
}
else
{
// this constraint will allow us to hold a free object.
if (joint == null)
joint = gameObject.AddComponent<ConfigurableJoint>();
joint.connectedBody = rb;
ConfigureJoint(rb.mass, true, joint);
}
}
if (joint2 != null)
{
joint2.targetPosition = transform.localPosition - grabPoint;
joint2.targetRotation = transform.localRotation * Quaternion.Inverse(grabOrientation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment