NavigationLoop.cs 857 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. namespace Unity.AI.Navigation.Samples
  4. {
  5. /// <summary>
  6. /// Use physics raycast hit from mouse click to set agent destination
  7. /// </summary>
  8. [RequireComponent(typeof(NavMeshAgent))]
  9. public class NavigationLoop : MonoBehaviour
  10. {
  11. NavMeshAgent m_Agent;
  12. public Transform[] goals = new Transform[3];
  13. private int m_NextGoal = 1;
  14. void Start()
  15. {
  16. m_Agent = GetComponent<NavMeshAgent>();
  17. }
  18. void Update()
  19. {
  20. float distance = Vector3.Distance(m_Agent.transform.position, goals[m_NextGoal].position);
  21. if (distance < 0.5f)
  22. {
  23. m_NextGoal = m_NextGoal != 2 ? m_NextGoal + 1 : 0;
  24. }
  25. m_Agent.destination = goals[m_NextGoal].position;
  26. }
  27. }
  28. }