MecanimBridge.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. namespace Pathfinding.Examples {
  3. using Pathfinding.Util;
  4. /// <summary>
  5. /// Example of how to use Mecanim with the included movement scripts.
  6. ///
  7. /// This script will use Mecanim to apply root motion to move the character
  8. /// instead of allowing the movement script to do the movement.
  9. ///
  10. /// It assumes that the Mecanim controller uses 3 input variables
  11. /// - InputMagnitude which is simply 1 when the character should be moving and 0 when it should stop.
  12. /// - X which is component of the desired movement direction along the left/right axis.
  13. /// - Y which is component of the desired movement direction along the forward/backward axis.
  14. ///
  15. /// It works with AIPath and RichAI.
  16. ///
  17. /// See: <see cref="Pathfinding.IAstarAI"/>
  18. /// See: <see cref="Pathfinding.AIPath"/>
  19. /// See: <see cref="Pathfinding.RichAI"/>
  20. /// </summary>
  21. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_mecanim_bridge.php")]
  22. public class MecanimBridge : VersionedMonoBehaviour {
  23. public float velocitySmoothing = 1;
  24. /// <summary>Cached reference to the movement script</summary>
  25. IAstarAI ai;
  26. /// <summary>Cached Animator component</summary>
  27. Animator anim;
  28. /// <summary>Cached Transform component</summary>
  29. Transform tr;
  30. Vector3 smoothedVelocity;
  31. /// <summary>Position of the left and right feet during the previous frame</summary>
  32. Vector3[] prevFootPos = new Vector3[2];
  33. /// <summary>Cached reference to the left and right feet</summary>
  34. Transform[] footTransforms;
  35. protected override void Awake () {
  36. base.Awake();
  37. ai = GetComponent<IAstarAI>();
  38. anim = GetComponent<Animator>();
  39. tr = transform;
  40. // Find the feet of the character
  41. footTransforms = new [] { anim.GetBoneTransform(HumanBodyBones.LeftFoot), anim.GetBoneTransform(HumanBodyBones.RightFoot) };
  42. }
  43. /// <summary>Update is called once per frame</summary>
  44. void Update () {
  45. var aiBase = ai as AIBase;
  46. aiBase.canMove = false;
  47. // aiBase.updatePosition = false;
  48. // aiBase.updateRotation = false;
  49. }
  50. /// <summary>Calculate position of the currently grounded foot</summary>
  51. Vector3 CalculateBlendPoint () {
  52. // Fall back to rotating around the transform position if no feet could be found
  53. if (footTransforms[0] == null || footTransforms[1] == null) return tr.position;
  54. var leftFootPos = footTransforms[0].position;
  55. var rightFootPos = footTransforms[1].position;
  56. // This is the same calculation that Unity uses for
  57. // Animator.pivotWeight and Animator.pivotPosition
  58. // but those properties do not work for all animations apparently.
  59. var footVelocity1 = (leftFootPos - prevFootPos[0]) / Time.deltaTime;
  60. var footVelocity2 = (rightFootPos - prevFootPos[1]) / Time.deltaTime;
  61. float denominator = footVelocity1.magnitude + footVelocity2.magnitude;
  62. var pivotWeight = denominator > 0 ? footVelocity1.magnitude / denominator : 0.5f;
  63. prevFootPos[0] = leftFootPos;
  64. prevFootPos[1] = rightFootPos;
  65. var pivotPosition = Vector3.Lerp(leftFootPos, rightFootPos, pivotWeight);
  66. return pivotPosition;
  67. }
  68. void OnAnimatorMove () {
  69. Vector3 nextPosition;
  70. Quaternion nextRotation;
  71. ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
  72. //var desiredVelocity = (ai.steeringTarget - tr.position).normalized * 2;//ai.desiredVelocity;
  73. var desiredVelocity = ai.desiredVelocity;
  74. var desiredVelocityWithoutGrav = desiredVelocity;
  75. desiredVelocityWithoutGrav.y = 0;
  76. anim.SetFloat("InputMagnitude", ai.reachedEndOfPath || desiredVelocityWithoutGrav.magnitude < 0.1f ? 0f : 1f);
  77. // Calculate the desired velocity relative to the character (+Z = forward, +X = right)
  78. var localDesiredVelocity = tr.InverseTransformDirection(desiredVelocityWithoutGrav);
  79. smoothedVelocity = Vector3.Lerp(smoothedVelocity, localDesiredVelocity, velocitySmoothing > 0 ? Time.deltaTime / velocitySmoothing : 1);
  80. if (smoothedVelocity.magnitude < 0.4f) {
  81. smoothedVelocity = smoothedVelocity.normalized * 0.4f;
  82. }
  83. anim.SetFloat("X", smoothedVelocity.x);
  84. anim.SetFloat("Y", smoothedVelocity.z);
  85. // The IAstarAI interface doesn't expose rotation speeds right now, so we have to do this ugly thing.
  86. // In case this is an unknown movement script, we fall back to a reasonable value.
  87. var rotationSpeed = 360f;
  88. if (ai is AIPath aipath) {
  89. rotationSpeed = aipath.rotationSpeed;
  90. } else if (ai is RichAI richai) {
  91. rotationSpeed = richai.rotationSpeed;
  92. }
  93. // Calculate how much the agent should rotate during this frame
  94. var newRot = RotateTowards(desiredVelocityWithoutGrav, Time.deltaTime * rotationSpeed);
  95. // Rotate the character around the currently grounded foot to prevent foot sliding
  96. nextPosition = ai.position;
  97. nextRotation = ai.rotation;
  98. nextPosition = RotatePointAround(nextPosition, CalculateBlendPoint(), newRot * Quaternion.Inverse(nextRotation));
  99. nextRotation = newRot;
  100. // Apply rotational root motion
  101. nextRotation = anim.deltaRotation * nextRotation;
  102. // Use gravity from the movement script, not from animation
  103. var deltaPos = anim.deltaPosition;
  104. deltaPos.y = desiredVelocity.y * Time.deltaTime;
  105. nextPosition += deltaPos;
  106. // Call the movement script to perform the final movement
  107. ai.FinalizeMovement(nextPosition, nextRotation);
  108. }
  109. static Vector3 RotatePointAround (Vector3 point, Vector3 around, Quaternion rotation) {
  110. return rotation * (point - around) + around;
  111. }
  112. /// <summary>
  113. /// Calculates a rotation closer to the desired direction.
  114. /// Returns: The new rotation for the character
  115. /// </summary>
  116. /// <param name="direction">Direction in the movement plane to rotate toward.</param>
  117. /// <param name="maxDegrees">Maximum number of degrees to rotate this frame.</param>
  118. protected virtual Quaternion RotateTowards (Vector3 direction, float maxDegrees) {
  119. if (direction != Vector3.zero) {
  120. Quaternion targetRotation = Quaternion.LookRotation(direction);
  121. return Quaternion.RotateTowards(tr.rotation, targetRotation, maxDegrees);
  122. } else {
  123. return tr.rotation;
  124. }
  125. }
  126. }
  127. }