GrounderBipedIK.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.FinalIK {
  4. /// <summary>
  5. /// Grounding for BipedIK characters.
  6. /// </summary>
  7. [HelpURL("http://www.root-motion.com/finalikdox/html/page9.html")]
  8. [AddComponentMenu("Scripts/RootMotion.FinalIK/Grounder/Grounder Biped")]
  9. public class GrounderBipedIK: Grounder {
  10. // Open the User Manual URL
  11. [ContextMenu("User Manual")]
  12. protected override void OpenUserManual() {
  13. Application.OpenURL("http://www.root-motion.com/finalikdox/html/page9.html");
  14. }
  15. // Open the Script Reference URL
  16. [ContextMenu("Scrpt Reference")]
  17. protected override void OpenScriptReference() {
  18. Application.OpenURL("http://www.root-motion.com/finalikdox/html/class_root_motion_1_1_final_i_k_1_1_grounder_biped_i_k.html");
  19. }
  20. #region Main Interface
  21. /// <summary>
  22. /// The BipedIK componet.
  23. /// </summary>
  24. [Tooltip("The BipedIK componet.")]
  25. public BipedIK ik;
  26. /// <summary>
  27. /// The amount of spine bending towards upward slopes.
  28. /// </summary>
  29. [Tooltip("The amount of spine bending towards upward slopes.")]
  30. public float spineBend = 7f;
  31. /// <summary>
  32. /// The interpolation speed of spine bending.
  33. /// </summary>
  34. [Tooltip("The interpolation speed of spine bending.")]
  35. public float spineSpeed = 3f;
  36. #endregion Main Interface
  37. public override void ResetPosition() {
  38. solver.Reset();
  39. spineOffset = Vector3.zero;
  40. }
  41. private Transform[] feet = new Transform[2];
  42. private Quaternion[] footRotations = new Quaternion[2];
  43. private Vector3 animatedPelvisLocalPosition, solvedPelvisLocalPosition;
  44. private Vector3 spineOffset;
  45. private float lastWeight;
  46. // Can we initiate the Grounding?
  47. private bool IsReadyToInitiate() {
  48. if (ik == null) return false;
  49. if (!ik.solvers.leftFoot.initiated) return false;
  50. if (!ik.solvers.rightFoot.initiated) return false;
  51. return true;
  52. }
  53. // Initiate once we have a BipedIK component
  54. void Update() {
  55. weight = Mathf.Clamp(weight, 0f, 1f);
  56. if (weight <= 0f) return;
  57. if (initiated) return;
  58. if (!IsReadyToInitiate()) return;
  59. Initiate();
  60. }
  61. private void Initiate() {
  62. // Gathering both foot bones from the BipedIK
  63. feet = new Transform[2];
  64. footRotations = new Quaternion[2];
  65. feet[0] = ik.references.leftFoot;
  66. feet[1] = ik.references.rightFoot;
  67. footRotations[0] = Quaternion.identity;
  68. footRotations[1] = Quaternion.identity;
  69. // Adding to the delegates to get call at certain points in the solving process
  70. ik.solvers.spine.OnPreUpdate += OnSolverUpdate;
  71. ik.solvers.rightFoot.OnPostUpdate += OnPostSolverUpdate;
  72. // Store the default localPosition of the pelvis
  73. animatedPelvisLocalPosition = ik.references.pelvis.localPosition;
  74. // Initiate the Grounding
  75. solver.Initiate(ik.references.root, feet);
  76. initiated = true;
  77. }
  78. // Weigh out the limb solvers properly when the component is disabled
  79. void OnDisable() {
  80. if (!initiated) return;
  81. ik.solvers.leftFoot.IKPositionWeight = 0f;
  82. ik.solvers.rightFoot.IKPositionWeight = 0f;
  83. }
  84. // Called before updating the spine IK solver
  85. private void OnSolverUpdate() {
  86. if (!enabled) return;
  87. if (weight <= 0f) {
  88. if (lastWeight <= 0f) return;
  89. // Weigh out the limb solvers properly
  90. OnDisable();
  91. }
  92. lastWeight = weight;
  93. if (OnPreGrounder != null) OnPreGrounder();
  94. // If the pelvis local position has not changed since last solved state, consider it unanimated
  95. if (ik.references.pelvis.localPosition != solvedPelvisLocalPosition) animatedPelvisLocalPosition = ik.references.pelvis.localPosition;
  96. else ik.references.pelvis.localPosition = animatedPelvisLocalPosition;
  97. // Update the Grounding
  98. solver.Update();
  99. // Move the pelvis
  100. ik.references.pelvis.position += solver.pelvis.IKOffset * weight;
  101. // Update IKPositions and IKPositionWeights of the feet
  102. SetLegIK(ik.solvers.leftFoot, 0);
  103. SetLegIK(ik.solvers.rightFoot, 1);
  104. // Bending the spine
  105. if (spineBend != 0f && ik.references.spine.Length > 0) {
  106. spineSpeed = Mathf.Clamp(spineSpeed, 0f, spineSpeed);
  107. Vector3 spineOffseTarget = GetSpineOffsetTarget() * weight;
  108. spineOffset = Vector3.Lerp(spineOffset, spineOffseTarget * spineBend, Time.deltaTime * spineSpeed);
  109. // Store upper arm rotations to revert them after we rotate the spine
  110. Quaternion leftArmRotation = ik.references.leftUpperArm.rotation;
  111. Quaternion rightArmRotation = ik.references.rightUpperArm.rotation;
  112. // Get the offset rotation for the spine
  113. Vector3 up = solver.up;
  114. Quaternion f = Quaternion.FromToRotation(up, up + spineOffset);
  115. // Rotate the spine
  116. ik.references.spine[0].rotation = f * ik.references.spine[0].rotation;
  117. // Revert the upper arms
  118. ik.references.leftUpperArm.rotation = leftArmRotation;
  119. ik.references.rightUpperArm.rotation = rightArmRotation;
  120. ik.solvers.lookAt.SetDirty();
  121. }
  122. if (OnPostGrounder != null) OnPostGrounder();
  123. }
  124. // Set the IK position and weight for a limb
  125. private void SetLegIK(IKSolverLimb limb, int index) {
  126. footRotations[index] = feet[index].rotation;
  127. limb.IKPosition = solver.legs[index].IKPosition;
  128. limb.IKPositionWeight = weight;
  129. }
  130. // Rotating the feet after IK has finished
  131. private void OnPostSolverUpdate() {
  132. if (weight <= 0f) return;
  133. if (!enabled) return;
  134. for (int i = 0; i < feet.Length; i++) {
  135. feet[i].rotation = Quaternion.Slerp(Quaternion.identity, solver.legs[i].rotationOffset, weight) * footRotations[i];
  136. }
  137. // Store the local position of the pelvis so we know it it changes
  138. solvedPelvisLocalPosition = ik.references.pelvis.localPosition;
  139. }
  140. // Cleaning up the delegates
  141. void OnDestroy() {
  142. if (initiated && ik != null) {
  143. ik.solvers.spine.OnPreUpdate -= OnSolverUpdate;
  144. ik.solvers.rightFoot.OnPostUpdate -= OnPostSolverUpdate;
  145. }
  146. }
  147. }
  148. }