private void FollowHead() { Vector3 oldControllerPosition = controllerPosition; Vector3 spineDirection = ivr.transform.InverseTransformDirection(ivr.headTarget.position - ivr.hipTarget.position); Vector3 spineAngles = (Quaternion.FromToRotation(Vector3.up, spineDirection)).eulerAngles; Vector3 clampedSpineAngles = Angles.ClampVector3(spineAngles, romSpineNegative, romSpinePositive); Vector3 spine = ivr.transform.rotation * Quaternion.Euler(clampedSpineAngles) * (Vector3.down * spineLength); Vector3 hipPosition = ivr.headTarget.position + spine; controllerPosition = Quaternion.Inverse(ivr.transform.rotation) * (hipPosition - ivr.transform.position); Vector3 headPosition = ivr.headTarget.position - ivr.transform.position; Vector3 bodyStretch = headPosition - headStartPosition; if (bodyStretch.y >= -0.01F) { Vector3 movementDirection = new Vector3(controllerPosition.x - oldControllerPosition.x, 0, controllerPosition.z - oldControllerPosition.z).normalized; float angle = Vector3.Angle(movementDirection, ivr.hitNormal); if (ivr.collisions && ivr.collided && angle > 90) { controllerPosition = oldControllerPosition; } else { //controllerPosition = new Vector3(controllerPosition.x, oldControllerPosition.y, controllerPosition.z); } } }
void CalculateHeadOrientation(Transform neck, Transform neckTarget) { Vector3 localHeadAngles = (Quaternion.Inverse(hipTarget.rotation) * neckTarget.rotation).eulerAngles; localHeadAngles = Angles.ClampVector3(localHeadAngles, minHeadAngles, maxHeadAngles); neck.rotation = hipTarget.rotation * Quaternion.Euler(localHeadAngles) * torso.fromNormNeck; }
private void HandRotation() { float dOrientation = 0; float dOrientationL = Angles.Difference(ivr.hipTarget.eulerAngles.y, ivr.leftHandTarget.eulerAngles.y); float dOrientationR = Angles.Difference(ivr.hipTarget.eulerAngles.y, ivr.rightHandTarget.eulerAngles.y); if (Mathf.Sign(dOrientationL) == Mathf.Sign(dOrientationR)) { if (Mathf.Abs(dOrientationL) < Mathf.Abs(dOrientationR)) { dOrientation = dOrientationL; } else { dOrientation = dOrientationR; } } float neckOrientation = Angles.Difference(ivr.headTarget.eulerAngles.y, ivr.hipTarget.eulerAngles.y + dOrientation); if (neckOrientation < 90 && neckOrientation > -90) // head cannot turn more than 90 degrees { controllerRotation *= Quaternion.AngleAxis(dOrientation, Vector3.up); } }