45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.AI; // NavMesh 기능을 위해 필수
|
|
|
|
public class EnemyAI : MonoBehaviour
|
|
{
|
|
private NavMeshAgent _agent;
|
|
private Transform _target;
|
|
|
|
void Awake()
|
|
{
|
|
_agent = GetComponent<NavMeshAgent>();
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
// --- 2번 방법: 위치 보정 로직 시작 ---
|
|
// 현재 위치에서 반경 2.0f 이내에 가장 가까운 NavMesh가 있는지 검사합니다.
|
|
if (NavMesh.SamplePosition(transform.position, out NavMeshHit hit, 2.0f, NavMesh.AllAreas))
|
|
{
|
|
// 에이전트를 해당 위치로 강제 순간이동(Warp) 시킵니다.
|
|
// 이 작업은 에러를 방지하고 에이전트를 활성화합니다.
|
|
_agent.Warp(hit.position);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"{gameObject.name} 근처에 NavMesh를 찾을 수 없습니다! 스폰 위치를 확인하세요.");
|
|
}
|
|
// --- 2번 방법 끝 ---
|
|
|
|
// 기존 타겟(Core) 설정 로직
|
|
GameObject coreObj = GameObject.FindWithTag("Core");
|
|
if (coreObj != null)
|
|
{
|
|
_target = coreObj.transform;
|
|
}
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if (_target != null && _agent.isOnNavMesh)
|
|
{
|
|
_agent.SetDestination(_target.position);
|
|
}
|
|
}
|
|
} |