protected void UpdateActionInput(SixenseInput.Controller controller) { Vector3 currentPosition = GetCurrentColliderPosition(); Quaternion currentRotation = GetCurrentColliderRotation(); Velocity(); if (isHoldingObject && !controller.GetButton(SixenseButtons.TRIGGER)) { Throw(); isHoldingObject = false; } if (!isHoldingObject) { foreach (GameObject o in GameObject.FindGameObjectsWithTag("Grabbable")) { float dist = Vector3.Distance(o.transform.position, currentPosition); if (dist < minGrabDistance) { closestObject = o; } } } if ((isHoldingObject && controller.GetButton(SixenseButtons.TRIGGER)) || (closestObject != null && Vector3.Distance(closestObject.transform.position, currentPosition) < minGrabDistance && controller.GetButton(SixenseButtons.TRIGGER))) { if (closestObject.GetComponent <Rigidbody>() && closestObject.GetComponent <Rigidbody>().isKinematic) { return; } grabObject = closestObject.GetComponent <GrabObject>(); if (grabObject && grabObject.isEnabled) { grabObject.SetRigidbodyDetectionCollisions(false); closestObject.transform.position = currentPosition + closestObject.transform.TransformDirection(grabObject.GetPosition(Hand)); closestObject.transform.rotation = currentRotation * Quaternion.Euler(grabObject.GetRotation(Hand)); } else { closestObject.transform.position = currentPosition; closestObject.transform.rotation = currentRotation; } isHoldingObject = true; } }
// Throw the held object once player lets go based on hand velocity // TODO: Take into account weight (1 unit = kilogram) of held object protected void Throw() { if (closestObject.GetComponent <Rigidbody>()) { grabObject = closestObject.GetComponent <GrabObject>(); float additionalThrowForce = 0; if (grabObject) { additionalThrowForce += grabObject.additionalThrowForce; grabObject.SetRigidbodyDetectionCollisions(true); } closestObject.GetComponent <Rigidbody>().AddForce(handDirection * handVelocity * (throwForce + additionalThrowForce)); } }