using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class FollowerController : MonoBehaviour { public float wakeDistance; private NavMeshAgent agent; private AudioSource audioSource; private Animator[] anims; private bool isFollowing = false; private Transform target; void Start() { agent = GetComponent(); anims = GetComponentsInChildren(); audioSource = GetComponent(); var players = GameObject.FindGameObjectsWithTag("Player"); if (players?.Length > 0) { target = players[0].transform; } } // Update is called once per frame void Update() { if (isFollowing) { foreach (var anim in anims) { anim.SetFloat("speed", agent.velocity.magnitude); } //Debug.Log($"Velocity: {agent.velocity.magnitude}"); agent.SetDestination(target.position); } else { float dist = Vector3.Distance(target.position, transform.position); isFollowing = dist < wakeDistance; if (isFollowing) { audioSource.Play(); Debug.LogWarning("Played the audio!"); } } } }