60 lines
1.4 KiB
C#
60 lines
1.4 KiB
C#
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<NavMeshAgent>();
|
|
agent.updateRotation = false;
|
|
agent.updateUpAxis = false;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
playerObject = GameObject.FindWithTag("Player");
|
|
}
|
|
|
|
public override void RunAI()
|
|
{
|
|
if (playerObject != null)
|
|
{
|
|
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);
|
|
}
|
|
} |