Update NPC: Nav, find Player

This commit is contained in:
manhduyhoang90
2026-06-04 15:41:01 +07:00
parent 6c8c372f9d
commit f970c78192
6 changed files with 280 additions and 86 deletions

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; // Cần thiết để dùng NavMesh
[RequireComponent(typeof(NavMeshAgent))] // Tự động thêm component này nếu chưa có
public class EnemyAI : MonoBehaviour
{
[Header("References")]
@@ -11,6 +13,12 @@ public class EnemyAI : MonoBehaviour
public float moveSpeed = 3f;
public float rotateSpeed = 50f;
[Header("Patrol Area")]
public float patrolRadius = 15f; // Bán kính khu vực tuần tra
public float patrolWaitTime = 2f; // Thời gian đứng chờ trước khi đi điểm khác
private Vector3 startPosition;
private float currentWaitTime;
[Header("Artifact")]
public bool playerHasArtifact;
@@ -21,23 +29,44 @@ public class EnemyAI : MonoBehaviour
public float maxShootDelay = 3f;
private float nextShootTime;
private NavMeshAgent agent;
public Node behaviorTreeRoot;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
agent = GetComponent<NavMeshAgent>();
agent.speed = moveSpeed;
// Lưu lại vị trí ban đầu để làm tâm của khu vực tuần tra
startPosition = transform.position;
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
InitBehaviorTree();
FindPlayer();
}
private void Update()
{
// Nếu mất reference (Player chết hoặc chưa spawn), liên tục tìm lại
if (player == null)
{
FindPlayer();
}
// Chỉ chạy AI nếu đã tìm thấy player (hoặc bạn có thể cho tuần tra ngay cả khi chưa có player tùy logic game)
behaviorTreeRoot?.Evaluate();
}
private void FindPlayer()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null)
{
player = playerObj.transform;
}
}
private void InitBehaviorTree()
{
// Player có artifact -> focus + shoot
@@ -54,14 +83,14 @@ public class EnemyAI : MonoBehaviour
new TaskNode(ActionMoveToPlayer)
});
// Không thấy ai -> scan
var scanNode = new TaskNode(ActionRotationScan);
// Không thấy ai -> Tuần tra bằng NavMesh
var patrolNode = new TaskNode(ActionPatrol);
behaviorTreeRoot = new Selector(new List<Node>
{
laserSequence,
chaseSequence,
scanNode
patrolNode
});
}
@@ -69,18 +98,14 @@ public class EnemyAI : MonoBehaviour
private NodeState CheckHasArtifact()
{
return playerHasArtifact
? NodeState.Success
: NodeState.Failure;
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
}
private NodeState CheckCanSeePlayer()
{
if (player == null)
return NodeState.Failure;
if (player == null) return NodeState.Failure;
float distance =
Vector3.Distance(transform.position, player.position);
float distance = Vector3.Distance(transform.position, player.position);
if (distance <= detectRange)
{
@@ -94,66 +119,74 @@ public class EnemyAI : MonoBehaviour
#region ACTIONS
private NodeState ActionRotationScan()
private NodeState ActionPatrol()
{
Debug.Log("Scanning...");
Debug.Log("Patrolling...");
agent.isStopped = false; // Đảm bảo NPC được phép di chuyển
agent.speed = moveSpeed * 0.5f; // Đi dạo nên đi chậm lại một chút
transform.Rotate(
Vector3.up,
rotateSpeed * Time.deltaTime);
// Kiểm tra xem NPC đã đến điểm đích chưa
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
{
currentWaitTime += Time.deltaTime;
// Chờ một lúc rồi mới chọn điểm mới
if (currentWaitTime >= patrolWaitTime)
{
// Tìm một điểm ngẫu nhiên trong bán kính cho trước
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
randomDirection += startPosition;
NavMeshHit hit;
// Đảm bảo điểm ngẫu nhiên nằm trên bề mặt NavMesh hợp lệ
if (NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1))
{
agent.SetDestination(hit.position);
}
currentWaitTime = 0f;
}
}
return NodeState.Running;
}
private NodeState ActionMoveToPlayer()
{
if (player == null)
return NodeState.Failure;
if (player == null) return NodeState.Failure;
Debug.Log("Chasing Player");
Vector3 dir =
(player.position - transform.position).normalized;
transform.position +=
dir *
moveSpeed *
Time.deltaTime;
agent.isStopped = false;
agent.speed = moveSpeed; // Phục hồi tốc độ rượt đuổi
agent.SetDestination(player.position);
return NodeState.Running;
}
private NodeState ActionFocusAndShoot()
{
if (player == null)
return NodeState.Failure;
if (player == null) return NodeState.Failure;
Debug.Log("Focus and Shoot!");
// Dừng NavMeshAgent lại để đứng bắn, tránh bị trượt
agent.isStopped = true;
// Focus player
Vector3 dir =
player.position - transform.position;
Vector3 dir = player.position - transform.position;
dir.y = 0f;
if (dir != Vector3.zero)
{
Quaternion targetRotation =
Quaternion.LookRotation(dir);
transform.rotation =
Quaternion.Slerp(
transform.rotation,
targetRotation,
5f * Time.deltaTime);
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
}
// Shoot with random delay
if (Time.time >= nextShootTime)
{
ShootLaser();
nextShootTime =
Time.time +
Random.Range(minShootDelay, maxShootDelay);
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
return NodeState.Running;
@@ -161,14 +194,8 @@ public class EnemyAI : MonoBehaviour
private void ShootLaser()
{
if (laserPrefab == null || firePoint == null)
return;
Instantiate(
laserPrefab,
firePoint.position,
firePoint.rotation);
if (laserPrefab == null || firePoint == null) return;
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
Debug.Log("Laser Shot!");
}