using UnityEngine; public class ChaseRunEnemy : MonoBehaviour { public Animator m_animator; public Rigidbody rb; public float moveSpeed; public float stopDistance = 1.5f; private Transform player; private float stopDistanceSqr; private bool isStopped = false; [Header("Speed Source")] public bool usePlayerSpeed = false; // set to false so enemy won't copy the wolf's boost [SerializeField] private ParticleSystem LeftFootDustPoof, RightFootDustPoof; private void Awake() { rb = GetComponent(); GameObject playerObj = GameObject.FindGameObjectWithTag("Player"); if (playerObj != null) { player = playerObj.transform; } else { Debug.LogError("[ChaseRunEnemy] Player not found in scene."); } stopDistanceSqr = stopDistance * stopDistance; // Cache squared distance } private void OnEnable() { if (usePlayerSpeed) ChasePlayerController.OnMoveSpeedChanged += UpdateMoveSpeed; } private void OnDisable() { if (usePlayerSpeed) ChasePlayerController.OnMoveSpeedChanged += UpdateMoveSpeed; } private void UpdateMoveSpeed(float speed) { moveSpeed = speed; } private void FixedUpdate() { if (player == null) return; Vector3 toPlayer = player.position - transform.position; float distanceSqr = toPlayer.sqrMagnitude; if (distanceSqr <= stopDistanceSqr) { if (!isStopped) { m_animator.SetBool("isStop", true); isStopped = true; } return; } if (isStopped) { m_animator.SetBool("isStop", false); isStopped = false; } MoveForward(); } private void MoveForward() { rb.MovePosition(rb.position + Vector3.back * moveSpeed * Time.fixedDeltaTime); } public void LeftFootOnFloor() { Debug.Log("Left Foot on floor"); LeftFootDustPoof.Stop(); LeftFootDustPoof.Play(); } public void RightFootOnFloor() { Debug.Log("Right Foot on floor"); RightFootDustPoof.Stop(); RightFootDustPoof.Play(); } }