2026-01-22 13:18:27 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class FlyEnemyMovement : Entity
|
|
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
[SerializeField] private float detectionRange = 8.0f;
|
|
|
|
|
[SerializeField] private float moveSpeed = 3.0f;
|
|
|
|
|
[SerializeField] private LayerMask obstacleLayer;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
2026-01-27 12:06:26 +00:00
|
|
|
private GameObject playerObject;
|
2026-01-23 10:22:52 +00:00
|
|
|
|
2026-01-27 12:06:26 +00:00
|
|
|
protected override void Awake()
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
base.Awake();
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
playerObject = GameObject.FindWithTag("Player");
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 10:22:52 +00:00
|
|
|
public override void RunAI()
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
|
|
|
|
if (playerObject != null)
|
|
|
|
|
{
|
|
|
|
|
float distance = Vector3.Distance(transform.position, playerObject.transform.position);
|
|
|
|
|
if (distance <= detectionRange)
|
|
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
MoveTowardsPlayer();
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 12:06:26 +00:00
|
|
|
private void MoveTowardsPlayer()
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
Vector3 direction = (playerObject.transform.position - transform.position).normalized;
|
|
|
|
|
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 1.0f, obstacleLayer);
|
|
|
|
|
|
|
|
|
|
if (hit.collider != null)
|
|
|
|
|
{
|
|
|
|
|
Vector2 tangent = Vector2.Perpendicular(hit.normal);
|
|
|
|
|
direction = Vector3.Lerp(direction, tangent, 0.5f).normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transform.position += direction * moveSpeed * Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
if (direction.x != 0)
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
transform.localScale = new Vector3(direction.x > 0 ? 1 : -1, 1, 1);
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
|
{
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, detectionRange);
|
|
|
|
|
}
|
|
|
|
|
}
|