FollowLeader.cs 880 B

12345678910111213141516171819202122232425262728293031323334
  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. // Start is called before the first frame update
  9. void Start()
  10. {
  11. agent = this.GetComponent<NavMeshAgent>();
  12. anim = this.GetComponent<Animator>();
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. Vector3 leaderPos = leader.gameObject.transform.position;
  18. agent.SetDestination(leaderPos);
  19. if (agent.remainingDistance <= agent.stoppingDistance)
  20. {
  21. anim.SetBool("isWalking", false);
  22. anim.SetBool("isIdle", true);
  23. }
  24. if (agent.remainingDistance > agent.stoppingDistance)
  25. {
  26. anim.SetBool("isWalking", true);
  27. anim.SetBool("isIdle", false);
  28. }
  29. }
  30. }