TigerProject/Assets/Scripts/Monster/FlyEnemyMovement.cs

64 lines
1.5 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.AI;
public class FlyEnemyMovement : Entity
{
private GameObject playerObject;
private NavMeshAgent agent;
2026-01-23 10:22:52 +00:00
[SerializeField] private float detectionRange = 5.0f; // <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
[SerializeField] private float EnemyTime;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
// NavMeshPlus 2D <20><><EFBFBD><EFBFBD>
agent.updateRotation = false;
agent.updateUpAxis = false;
}
private void Start()
{
playerObject = GameObject.FindWithTag("Player");
}
2026-01-23 10:22:52 +00:00
public override void RunAI()
{
if (playerObject != null)
{
// 1. <20><><EFBFBD><EFBFBD> <20><>ġ<EFBFBD><C4A1> <20>÷<EFBFBD><C3B7>̾<EFBFBD> <20><>ġ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ÿ<EFBFBD><C5B8><EFBFBD> <20><><EFBFBD><EFBFBD>
float distance = Vector3.Distance(transform.position, playerObject.transform.position);
if (distance <= detectionRange)
{
SetAgentPosition(); // <20>߰<EFBFBD> <20><><EFBFBD><EFBFBD>
2026-01-23 10:22:52 +00:00
}
else
{
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD>̶<EFBFBD><CCB6><EFBFBD> <20>̵<EFBFBD><CCB5><EFBFBD> <20><><EFBFBD><EFBFBD>
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);
}
}