FollowLeader.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. public class FollowLeader : MonoBehaviour
  4. {
  5. public GameObject leader;
  6. private NavMeshAgent agent;
  7. private Animator anim;
  8. //private Animator animLeader;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. agent = this.GetComponent<NavMeshAgent>();
  13. anim = this.GetComponent<Animator>();
  14. //animLeader = leader.GetComponent<Animator>();
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. Vector3 leaderPos = leader.gameObject.transform.position;
  20. agent.SetDestination(leaderPos);
  21. if (agent.remainingDistance <= agent.stoppingDistance)
  22. {
  23. anim.SetBool("isWalking", false);
  24. anim.SetBool("isIdle", true);
  25. }
  26. if (agent.remainingDistance > agent.stoppingDistance)
  27. {
  28. anim.SetBool("isWalking", true);
  29. anim.SetBool("isIdle", false);
  30. }
  31. //if (!agent.pathPending)
  32. //{
  33. // if (agent.remainingDistance <= agent.stoppingDistance)
  34. // {
  35. // if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
  36. // {
  37. // anim.SetBool("isWalking", false);
  38. // anim.SetBool("isIdle", true);
  39. // }
  40. // }
  41. //}
  42. //if (agent.pathPending)
  43. //{
  44. // if (agent.remainingDistance > agent.stoppingDistance)
  45. // {
  46. // if (agent.hasPath || agent.velocity.sqrMagnitude != 0f)
  47. // {
  48. // anim.SetBool("isWalking", true);
  49. // anim.SetBool("isIdle", false);
  50. // }
  51. // }
  52. //}
  53. }
  54. }