/* Start Grabbing Object - Added */ //Calculate Distance private Collider GrabSearch(Vector3 leftHandCoordinate, Vector3 rightHandCoordinate) { Vector3 grabPosition = (leftHandCoordinate + rightHandCoordinate) / 2.0f; float closest_sqr_distance = grabObjectDistance * grabObjectDistance; Collider closest = null; Collider[] close_things = Physics.OverlapSphere(grabPosition, grabObjectDistance, grabLayers); for (int j = 0; j < close_things.Length; ++j) { float sqr_distance = (grabPosition - close_things[j].transform.position).sqrMagnitude; //float grab_hand_distance = ( leftHandCoordinate - rightHandCoordinate ).sqrMagnitude; // detect certain amount of distance and regard as grabbed Debug.Log("Searching closest objects"); if (close_things[j].GetComponent <Rigidbody>() != null && sqr_distance < closest_sqr_distance && !close_things[j].transform.IsChildOf(transform) && close_things[j].tag != "NotGrabbable") { Holdable_object holdable = close_things[j].GetComponent <Holdable_object>(); Debug.Log("Holdable Object detected"); if (holdable == null || !holdable.IsGrabbed()) { closest = close_things[j]; closest_sqr_distance = sqr_distance; } } } return(closest); }
protected void OnRelease() { if (activeObject != null) { Holdable_object hold = activeObject.GetComponent <Holdable_object>(); if (hold != null) { hold.OnRelease(); } if (hold == null) { activeObject.GetComponent <Rigidbody>().maxAngularVelocity = Mathf.Infinity; } } activeObject = null; StartGrabbing(CurrentBody); }
protected void StartGrabbing(Kinect.Body body) { Debug.Log("Start Grabbing"); //Kinect.Joint sourceJoint = body.Joints [Kinect.JointType.HandLeft]; Vector3 leftHandCoordinate = GetVector3FromJoint(body.Joints [Kinect.JointType.HandLeft]); Vector3 rightHandCoordinate = GetVector3FromJoint(body.Joints [Kinect.JointType.HandRight]); Vector3 curGrabPosition = (leftHandCoordinate + rightHandCoordinate) / 2.0f; activeObject = GrabSearch(leftHandCoordinate, rightHandCoordinate); //Debug.Log ("Grab Position: " + curGrabPosition); if (activeObject == null) { Debug.Log("No Active Object"); return; } Holdable_object holdable = activeObject.GetComponent <Holdable_object>(); grab_offset = Vector3.zero; if (holdable == null) { Vector3 delta_position = activeObject.transform.position - curGrabPosition; Ray grab_ray = new Ray(curGrabPosition, delta_position); RaycastHit grab_hit; if (activeObject.Raycast(grab_ray, out grab_hit, grabObjectDistance)) { grab_offset = activeObject.transform.position - grab_hit.point; } else { grab_offset = activeObject.transform.position - curGrabPosition; } } //wow... Quaternion if (holdable != null) { holdable.OnGrab(); } }