AgentLinkMover.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections;
  4. namespace Unity.AI.Navigation.Samples
  5. {
  6. public enum OffMeshLinkMoveMethod
  7. {
  8. Teleport,
  9. NormalSpeed,
  10. Parabola,
  11. Curve
  12. }
  13. /// <summary>
  14. /// Move an agent when traversing a OffMeshLink given specific animated methods
  15. /// </summary>
  16. [RequireComponent(typeof(NavMeshAgent))]
  17. public class AgentLinkMover : MonoBehaviour
  18. {
  19. public OffMeshLinkMoveMethod m_Method = OffMeshLinkMoveMethod.Parabola;
  20. public AnimationCurve m_Curve = new AnimationCurve();
  21. IEnumerator Start()
  22. {
  23. NavMeshAgent agent = GetComponent<NavMeshAgent>();
  24. agent.autoTraverseOffMeshLink = false;
  25. while (true)
  26. {
  27. if (agent.isOnOffMeshLink)
  28. {
  29. if (m_Method == OffMeshLinkMoveMethod.NormalSpeed)
  30. yield return StartCoroutine(NormalSpeed(agent));
  31. else if (m_Method == OffMeshLinkMoveMethod.Parabola)
  32. yield return StartCoroutine(Parabola(agent, 2.0f, 0.5f));
  33. else if (m_Method == OffMeshLinkMoveMethod.Curve)
  34. yield return StartCoroutine(Curve(agent, 0.5f));
  35. agent.CompleteOffMeshLink();
  36. }
  37. yield return null;
  38. }
  39. }
  40. IEnumerator NormalSpeed(NavMeshAgent agent)
  41. {
  42. OffMeshLinkData data = agent.currentOffMeshLinkData;
  43. Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
  44. while (agent.transform.position != endPos)
  45. {
  46. agent.transform.position =
  47. Vector3.MoveTowards(agent.transform.position, endPos, agent.speed * Time.deltaTime);
  48. yield return null;
  49. }
  50. }
  51. IEnumerator Parabola(NavMeshAgent agent, float height, float duration)
  52. {
  53. OffMeshLinkData data = agent.currentOffMeshLinkData;
  54. Vector3 startPos = agent.transform.position;
  55. Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
  56. float normalizedTime = 0.0f;
  57. while (normalizedTime < 1.0f)
  58. {
  59. float yOffset = height * 4.0f * (normalizedTime - normalizedTime * normalizedTime);
  60. agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
  61. normalizedTime += Time.deltaTime / duration;
  62. yield return null;
  63. }
  64. }
  65. IEnumerator Curve(NavMeshAgent agent, float duration)
  66. {
  67. OffMeshLinkData data = agent.currentOffMeshLinkData;
  68. Vector3 startPos = agent.transform.position;
  69. Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
  70. float normalizedTime = 0.0f;
  71. while (normalizedTime < 1.0f)
  72. {
  73. float yOffset = m_Curve.Evaluate(normalizedTime);
  74. agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
  75. normalizedTime += Time.deltaTime / duration;
  76. yield return null;
  77. }
  78. }
  79. }
  80. }