71 lines
1.7 KiB
C#
71 lines
1.7 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.AI;
|
|||
|
|
|
|||
|
|
public class FlyEnemyMovement : Entity
|
|||
|
|
{
|
|||
|
|
private GameObject playerObject;
|
|||
|
|
private NavMeshAgent agent;
|
|||
|
|
[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");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
// 2. <20>Ÿ<EFBFBD><C5B8><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> Ȯ<><C8AE>
|
|||
|
|
if (distance <= detectionRange)
|
|||
|
|
{
|
|||
|
|
SetAgentPosition(); // <20>߰<EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
|
}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 OnTriggerEnter2D(Collider2D other)
|
|||
|
|
{
|
|||
|
|
if (other.CompareTag("Player"))
|
|||
|
|
{
|
|||
|
|
Die();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private void OnDrawGizmosSelected()
|
|||
|
|
{
|
|||
|
|
Gizmos.color = Color.red;
|
|||
|
|
Gizmos.DrawWireSphere(transform.position, detectionRange);
|
|||
|
|
}
|
|||
|
|
}
|