Advertisement
_EagleOwle_

AgentLinkMover

Apr 7th, 2022
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.AI;
  4.  
  5. public enum OffMeshLinkMoveMethod
  6. {
  7.     Teleport,
  8.     NormalSpeed,
  9.     Parabola,
  10.     Curve
  11. }
  12.  
  13. [RequireComponent(typeof(NavMeshAgent))]
  14. public class AgentLinkMover : MonoBehaviour
  15. {
  16.     public OffMeshLinkMoveMethod method = OffMeshLinkMoveMethod.Parabola;
  17.     public AnimationCurve curve = new AnimationCurve();
  18.     IEnumerator Start()
  19.     {
  20.         NavMeshAgent agent = GetComponent<NavMeshAgent>();
  21.         agent.autoTraverseOffMeshLink = false;
  22.         while (true)
  23.         {
  24.             if (agent.isOnOffMeshLink)
  25.             {
  26.                 if (method == OffMeshLinkMoveMethod.NormalSpeed)
  27.                     yield return StartCoroutine(NormalSpeed(agent));
  28.                 else if (method == OffMeshLinkMoveMethod.Parabola)
  29.                     yield return StartCoroutine(Parabola(agent, 2.0f, 0.5f));
  30.                 else if (method == OffMeshLinkMoveMethod.Curve)
  31.                     yield return StartCoroutine(Curve(agent, 0.5f));
  32.                 agent.CompleteOffMeshLink();
  33.             }
  34.             yield return null;
  35.         }
  36.     }
  37.     IEnumerator NormalSpeed(NavMeshAgent agent)
  38.     {
  39.         OffMeshLinkData data = agent.currentOffMeshLinkData;
  40.         Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
  41.         while (agent.transform.position != endPos)
  42.         {
  43.             agent.transform.position = Vector3.MoveTowards(agent.transform.position, endPos, agent.speed * Time.deltaTime);
  44.             yield return null;
  45.         }
  46.     }
  47.     IEnumerator Parabola(NavMeshAgent agent, float height, float duration)
  48.     {
  49.         OffMeshLinkData data = agent.currentOffMeshLinkData;
  50.         Vector3 startPos = agent.transform.position;
  51.         Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
  52.         float normalizedTime = 0.0f;
  53.         while (normalizedTime < 1.0f)
  54.         {
  55.             float yOffset = height * 4.0f * (normalizedTime - normalizedTime * normalizedTime);
  56.             agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
  57.             normalizedTime += Time.deltaTime / duration;
  58.             yield return null;
  59.         }
  60.     }
  61.     IEnumerator Curve(NavMeshAgent agent, float duration)
  62.     {
  63.         OffMeshLinkData data = agent.currentOffMeshLinkData;
  64.         Vector3 startPos = agent.transform.position;
  65.         Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
  66.         float normalizedTime = 0.0f;
  67.         while (normalizedTime < 1.0f)
  68.         {
  69.             float yOffset = curve.Evaluate(normalizedTime);
  70.             agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
  71.             normalizedTime += Time.deltaTime / duration;
  72.             yield return null;
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement