using UnityEngine; using UnityEngine.AI; public class FlyEnemyMovement : Entity { private GameObject playerObject; private NavMeshAgent agent; [SerializeField] private float detectionRange = 5.0f; // °¨Áö ¹üÀ§ º¯¼ö [SerializeField] private float EnemyTime; private void Awake() { agent = GetComponent(); // NavMeshPlus 2D ¼³Á¤ agent.updateRotation = false; agent.updateUpAxis = false; } private void Start() { playerObject = GameObject.FindWithTag("Player"); } public override void RunAI() { if (playerObject != null) { // 1. ÇöÀç À§Ä¡¿Í Ç÷¹À̾î À§Ä¡ »çÀÌÀÇ °Å¸®¸¦ °è»ê float distance = Vector3.Distance(transform.position, playerObject.transform.position); if (distance <= detectionRange) { SetAgentPosition(); // Ãß°Ý ½ÃÀÛ } else { // ¹üÀ§ ¹ÛÀ̶ó¸é À̵¿À» ¸ØÃã if (!agent.isStopped) { agent.ResetPath(); } } } } private void SetAgentPosition() { if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) { return; } Vector3 targetPos = playerObject.transform.position; agent.SetDestination(new Vector3(targetPos.x, targetPos.y)); } private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, detectionRange); } }