//Perform IK clear IEnumerator PerformIKClear(int id, IKAttachment ik, float delay, bool smooth, float speed) { if (delay > 0) { yield return(new WaitForSeconds(delay)); } float initialWeight = 1; float finalWeight = 0; ik.iKAttachment.localPosition = ik.initPos; ik.iKAttachment.localRotation = ik.initRot; if (smooth) { float i = 0; while (i < 1) { while (animator.speed == 0) { yield return(0); } i += Time.deltaTime / speed; ik.weight = Mathf.Lerp(initialWeight, finalWeight, i); yield return(0); } } stateIKs[id].isActive = false; }
//Perform IK (Single) IEnumerator PerformSingleIK(int id, IKAttachment ik, float delay, bool smooth, float speed) { if (delay > 0) { yield return(new WaitForSeconds(delay)); } float initialWeight = 0; float finalWeight = 1; playing = true; if (smooth) { float i = 0; while (i < 1) { i += Time.deltaTime / speed; ik.weight = Mathf.Lerp(initialWeight, finalWeight, i); yield return(0); } } }
void SetIK(IKType type, IKAttachment ik) { Vector3 finalPos = ik.iKAttachment.position; Quaternion finalRot = ik.iKAttachment.rotation; switch (type) { case IKType.RightHand: if (ik.useLocation) { animator.SetIKPositionWeight(AvatarIKGoal.RightHand, ik.weight); animator.SetIKPosition(AvatarIKGoal.RightHand, finalPos); } if (ik.useRotation) { animator.SetIKRotationWeight(AvatarIKGoal.RightHand, ik.weight); animator.SetIKRotation(AvatarIKGoal.RightHand, finalRot); } break; case IKType.LeftHand: if (ik.useLocation) { animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, ik.weight); animator.SetIKPosition(AvatarIKGoal.LeftHand, finalPos); } if (ik.useRotation) { animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, ik.weight); animator.SetIKRotation(AvatarIKGoal.LeftHand, finalRot); } break; case IKType.RightFoot: if (ik.useLocation) { animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, ik.weight); animator.SetIKPosition(AvatarIKGoal.RightFoot, finalPos); } if (ik.useRotation) { animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, ik.weight); animator.SetIKRotation(AvatarIKGoal.RightFoot, finalRot); } break; case IKType.LeftFoot: if (ik.useLocation) { animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, ik.weight); animator.SetIKPosition(AvatarIKGoal.LeftFoot, finalPos); } if (ik.useRotation) { animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, ik.weight); animator.SetIKRotation(AvatarIKGoal.LeftFoot, finalRot); } break; } }
//Perform IK (Sequence) IEnumerator PerformSequence(int id, List <IKSequence> iKSequence, bool smoothEntry, bool isLoop, int counts) { playing = true; int activeAttachment = 0; IKAttachment ik = stateIKs[id].IKs[activeAttachment]; ik.iKAttachment.localPosition = ik.initPos; ik.iKAttachment.localRotation = ik.initRot; if (!iKSequence[0].useDefault) { if (smoothEntry && counts == 0) { float i = 0; while (i < 1) { while (animator.speed == 0) { yield return(0); } i += Time.deltaTime / iKSequence[activeAttachment].speed; ik.weight = Mathf.Lerp(0, 1, i); yield return(0); } } ik.weight = 1; } else { if (smoothEntry && counts == 0) { float i = 0; while (i < 1) { while (animator.speed == 0) { yield return(0); } i += Time.deltaTime / iKSequence[activeAttachment].speed; ik.weight = Mathf.Lerp(1, 0, i); yield return(0); } } ik.weight = 0; } Vector3 endPos = Vector3.zero; Quaternion endRot = Quaternion.identity; for (int j = 1; j < iKSequence.Count; j++) { if (stateIKs[id].IKs.Count <= iKSequence[j].attachment) { Debug.Log("No IK attachments with ID:" + (iKSequence[j].attachment.ToString("00")) + " found in State IK: " + stateIKs[id].iKName + " - " + this.gameObject.name); break; } if (stateIKs[id].IKs[iKSequence[j].attachment].iKAttachment == null) { Debug.Log("Missing attachment Transform with ID:" + (iKSequence[j].attachment.ToString("00")) + " found in State IK: " + stateIKs[id].iKName + " - " + this.gameObject.name); break; } if (counts == 0) { if (j == 1) { yield return(new WaitForSeconds(iKSequence[j].time - iKSequence[0].speed)); } else { yield return(new WaitForSeconds(iKSequence[j].time)); } } else { yield return(new WaitForSeconds(iKSequence[j].time)); } if (ik.weight == 0) {//Follow default animation without IK (first IK Attachment) activeAttachment = iKSequence[j].attachment; stateIKs[id].activeAttachment = activeAttachment; ik = stateIKs[id].IKs[activeAttachment]; ik.weight = 0; float h = 0; while (h < 1) { while (animator.speed == 0) { yield return(0); } h += Time.deltaTime / iKSequence[activeAttachment].speed; ik.weight = Mathf.Lerp(0, 1, h); yield return(0); } } else { if (!iKSequence[j].useDefault) { //To next Transform IK attachment IKAttachment previousIKAttachment = ik; endPos = stateIKs[id].IKs[iKSequence[j].attachment].iKAttachment.localPosition; endRot = stateIKs[id].IKs[iKSequence[j].attachment].iKAttachment.localRotation; float k = 0; while (k < 1) { while (animator.speed == 0) { yield return(0); } k += Time.deltaTime / iKSequence[j].speed; ik.iKAttachment.localPosition = Vector3.Lerp(ik.initPos, endPos, k); ik.iKAttachment.localRotation = Quaternion.Lerp(ik.initRot, endRot, k); yield return(0); } activeAttachment = iKSequence[j].attachment; stateIKs[id].activeAttachment = activeAttachment; ik = stateIKs[id].IKs[activeAttachment]; ik.weight = 1; previousIKAttachment.iKAttachment.localPosition = previousIKAttachment.initPos; previousIKAttachment.iKAttachment.localRotation = previousIKAttachment.initRot; } else { //Follow default animation without IK ik.weight = 1; float h = 0; while (h < 1) { while (animator.speed == 0) { yield return(0); } h += Time.deltaTime / iKSequence[j].speed; ik.weight = Mathf.Lerp(1, 0, h); yield return(0); } } } } if (isLoop) { AnimatorStateInfo animationState = animator.GetCurrentAnimatorStateInfo(0); AnimatorClipInfo[] animClip = animator.GetCurrentAnimatorClipInfo(0); float animationTime = animationState.normalizedTime - (1 * counts); if (animationTime < 1) { while (animationTime < 1) { animationState = animator.GetCurrentAnimatorStateInfo(0); animClip = animator.GetCurrentAnimatorClipInfo(0); animationTime = animationState.normalizedTime - (1 * counts); yield return(0); } } counts++; stateIKs[id].isActive = true; stateIKs[id].stateCoroutine = PerformSequence(id, iKSequence, smoothEntry, isLoop, counts); StartCoroutine(stateIKs[id].stateCoroutine); } }
//Perform IK (Sequence) IEnumerator PerformSequence(int id, List <IKSequence> iKSequence, bool smoothEntry, bool clearOnExit) { playing = true; int activeAttachment = 0; IKAttachment ik = stateIKs[id].IKs[activeAttachment]; if (!iKSequence[0].useDefault) { if (smoothEntry) { float i = 0; while (i < 1) { i += Time.deltaTime / iKSequence[activeAttachment].speed; ik.weight = Mathf.Lerp(0, 1, i); yield return(0); } } ik.weight = 1; } else { if (smoothEntry) { float i = 0; while (i < 1) { i += Time.deltaTime / iKSequence[activeAttachment].speed; ik.weight = Mathf.Lerp(1, 0, i); yield return(0); } } ik.weight = 0; } Vector3 endPos = Vector3.zero; Quaternion endRot = Quaternion.identity; for (int j = 1; j < iKSequence.Count; j++) { if (stateIKs[id].IKs.Count <= iKSequence[j].attachment) { Debug.Log("No IK attachments with ID:" + (iKSequence[j].attachment.ToString("00")) + " found in State IK: " + stateIKs[id].iKName + " - " + this.gameObject.name); break; } if (stateIKs[id].IKs[iKSequence[j].attachment].iKAttachment == null) { Debug.Log("Missing attachment Transform with ID:" + (iKSequence[j].attachment.ToString("00")) + " found in State IK: " + stateIKs[id].iKName + " - " + this.gameObject.name); break; } yield return(new WaitForSeconds(iKSequence[j].time)); if (ik.weight == 0) {//Follow default animation without IK (first IK Attachment) activeAttachment = iKSequence[j].attachment; stateIKs[id].activeAttachment = activeAttachment; ik = stateIKs[id].IKs[activeAttachment]; ik.weight = 0; float h = 0; while (h < 1) { h += Time.deltaTime / iKSequence[activeAttachment].speed; ik.weight = Mathf.Lerp(0, 1, h); yield return(0); } } else { if (!iKSequence[j].useDefault) { //To next Transform IK attachment IKAttachment previousIKAttachment = ik; endPos = stateIKs[id].IKs[iKSequence[j].attachment].iKAttachment.localPosition; endRot = stateIKs[id].IKs[iKSequence[j].attachment].iKAttachment.localRotation; float k = 0; while (k < 1) { k += Time.deltaTime / iKSequence[j].speed; ik.iKAttachment.localPosition = Vector3.Lerp(ik.initPos, endPos, k); ik.iKAttachment.localRotation = Quaternion.Lerp(ik.initRot, endRot, k); yield return(0); } activeAttachment = iKSequence[j].attachment; stateIKs[id].activeAttachment = activeAttachment; ik = stateIKs[id].IKs[activeAttachment]; ik.weight = 1; previousIKAttachment.iKAttachment.localPosition = previousIKAttachment.initPos; previousIKAttachment.iKAttachment.localRotation = previousIKAttachment.initRot; } else { //Follow default animation without IK ik.weight = 1; float h = 0; while (h < 1) { h += Time.deltaTime / iKSequence[j].speed; ik.weight = Mathf.Lerp(1, 0, h); yield return(0); } } } } if (clearOnExit) { stateIKs[id].isActive = false; } }