MineBotAnimation.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. namespace Pathfinding.Examples {
  3. /// <summary>
  4. /// Animation helper specifically made for the spider robot in the example scenes.
  5. /// The spider robot (or mine-bot) which has been copied from the Unity Example Project
  6. /// can have this script attached to be able to pathfind around with animations working properly.
  7. /// This script should be attached to a parent GameObject however since the original bot has Z+ as up.
  8. /// This component requires Z+ to be forward and Y+ to be up.
  9. ///
  10. /// A movement script (e.g AIPath) must also be attached to the same GameObject to actually move the unit.
  11. ///
  12. /// Animation is handled by this component. The Animator component refered to in <see cref="anim"/> should have a single parameter called NormalizedSpeed.
  13. /// When the end of path is reached, if the <see cref="endOfPathEffect"/> is not null, it will be instantiated at the current position. However a check will be
  14. /// done so that it won't spawn effects too close to the previous spawn-point.
  15. /// [Open online documentation to see images]
  16. /// </summary>
  17. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_mine_bot_animation.php")]
  18. public class MineBotAnimation : VersionedMonoBehaviour {
  19. /// <summary>
  20. /// Animation component.
  21. /// Should hold animations "awake" and "forward"
  22. /// </summary>
  23. public Animator anim;
  24. /// <summary>
  25. /// Effect which will be instantiated when end of path is reached.
  26. /// See: <see cref="OnTargetReached"/>
  27. /// </summary>
  28. public GameObject endOfPathEffect;
  29. bool isAtDestination;
  30. IAstarAI ai;
  31. Transform tr;
  32. protected override void Awake () {
  33. base.Awake();
  34. ai = GetComponent<IAstarAI>();
  35. tr = GetComponent<Transform>();
  36. }
  37. /// <summary>Point for the last spawn of <see cref="endOfPathEffect"/></summary>
  38. protected Vector3 lastTarget;
  39. /// <summary>
  40. /// Called when the end of path has been reached.
  41. /// An effect (<see cref="endOfPathEffect)"/> is spawned when this function is called
  42. /// However, since paths are recalculated quite often, we only spawn the effect
  43. /// when the current position is some distance away from the previous spawn-point
  44. /// </summary>
  45. void OnTargetReached () {
  46. if (endOfPathEffect != null && Vector3.Distance(tr.position, lastTarget) > 1) {
  47. GameObject.Instantiate(endOfPathEffect, tr.position, tr.rotation);
  48. lastTarget = tr.position;
  49. }
  50. }
  51. protected void Update () {
  52. if (ai.reachedEndOfPath) {
  53. if (!isAtDestination) OnTargetReached();
  54. isAtDestination = true;
  55. } else isAtDestination = false;
  56. // Calculate the velocity relative to this transform's orientation
  57. Vector3 relVelocity = tr.InverseTransformDirection(ai.velocity);
  58. relVelocity.y = 0;
  59. // Speed relative to the character size
  60. anim.SetFloat("NormalizedSpeed", relVelocity.magnitude / anim.transform.lossyScale.x);
  61. }
  62. }
  63. }