Update AI
This commit is contained in:
@@ -1,24 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI; // Cần thiết để dùng NavMesh
|
||||
using Hallucinate.Audio;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))] // Tự động thêm component này nếu chưa có
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
public Transform player;
|
||||
|
||||
[Header("Detection")]
|
||||
public float detectRange = 10f;
|
||||
public float moveSpeed = 3f;
|
||||
public float rotateSpeed = 50f;
|
||||
[Header("Field of View")]
|
||||
[Range(0, 360)] public float viewAngle = 90f;
|
||||
public float viewRadius = 20f;
|
||||
public LayerMask targetLayerMask; // Gán layer của Player
|
||||
public LayerMask obstacleLayerMask; // Gán layer của Tường, chướng ngại vật
|
||||
|
||||
private bool canSeePlayer = false;
|
||||
private Vector3 lastKnownPlayerPosition;
|
||||
private bool isInvestigating = false;
|
||||
|
||||
[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;
|
||||
public Transform[] patrolPoints;
|
||||
private int currentPatrolIndex = 0;
|
||||
public float moveSpeed = 3f;
|
||||
public float chaseSpeed = 5f;
|
||||
|
||||
[Header("Artifact")]
|
||||
public bool playerHasArtifact;
|
||||
@@ -28,74 +35,127 @@ public class EnemyAI : MonoBehaviour
|
||||
public Transform firePoint;
|
||||
public float minShootDelay = 1f;
|
||||
public float maxShootDelay = 3f;
|
||||
public float rotateSpeed = 50f;
|
||||
|
||||
[Header("Audio")]
|
||||
public string alertSound = "Enemy_Alert";
|
||||
public string shootSound = "Enemy_Shoot";
|
||||
private bool hasSpottedPlayer; // Để chỉ kêu alert 1 lần
|
||||
|
||||
[Header("Dodge Mechanics")]
|
||||
public float dodgeForce = 8f; // Lực đẩy văng đi
|
||||
public float dodgeDuration = 0.5f; // Thời gian nhào lộn/né
|
||||
public float dodgeCooldown = 3f; // Thời gian chờ giữa 2 lần né
|
||||
|
||||
private float nextDodgeTime;
|
||||
private bool isDodging = false;
|
||||
private Rigidbody rb;
|
||||
|
||||
private float nextShootTime;
|
||||
private NavMeshAgent agent;
|
||||
|
||||
public Node behaviorTreeRoot;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
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;
|
||||
rb = GetComponent<Rigidbody>();
|
||||
// Tự động tìm các điểm tuần tra nếu chưa gán
|
||||
if (patrolPoints == null || patrolPoints.Length == 0)
|
||||
{
|
||||
patrolPoints = GameObject.FindGameObjectsWithTag("PatrolPoint")
|
||||
.Select(go => go.transform).ToArray();
|
||||
}
|
||||
|
||||
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
||||
|
||||
InitBehaviorTree();
|
||||
FindPlayer();
|
||||
StartCoroutine(FindTargetWithDelay(0.1f)); // Chạy FOV quét mục tiêu
|
||||
}
|
||||
|
||||
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)
|
||||
if (player == null) FindPlayer();
|
||||
if (Input.GetMouseButtonDown(0) && canSeePlayer && !isDodging && Time.time >= nextDodgeTime)
|
||||
{
|
||||
FindPlayer();
|
||||
StartCoroutine(DodgeRoutine());
|
||||
}
|
||||
|
||||
// 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)
|
||||
if (isDodging) return;
|
||||
behaviorTreeRoot?.Evaluate();
|
||||
}
|
||||
|
||||
private void FindPlayer()
|
||||
{
|
||||
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
|
||||
if (playerObj != null)
|
||||
if (playerObj != null) player = playerObj.transform;
|
||||
}
|
||||
|
||||
// Coroutine tối ưu việc quét mục tiêu
|
||||
private IEnumerator FindTargetWithDelay(float delay)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
player = playerObj.transform;
|
||||
yield return new WaitForSeconds(delay);
|
||||
FindVisibleTargets();
|
||||
}
|
||||
}
|
||||
|
||||
private void FindVisibleTargets()
|
||||
{
|
||||
canSeePlayer = false;
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, viewRadius, targetLayerMask);
|
||||
|
||||
foreach (var col in colliders)
|
||||
{
|
||||
Transform target = col.transform;
|
||||
Vector3 direction = (target.position - transform.position).normalized;
|
||||
|
||||
float angle = Vector3.Angle(transform.forward, direction);
|
||||
|
||||
// Nếu nằm trong góc nhìn
|
||||
if (angle < viewAngle / 2)
|
||||
{
|
||||
float distanceToTarget = Vector3.Distance(transform.position, target.position);
|
||||
|
||||
// Nếu không có vật cản che khuất
|
||||
if (!Physics.Raycast(transform.position, direction, distanceToTarget, obstacleLayerMask))
|
||||
{
|
||||
canSeePlayer = true;
|
||||
isInvestigating = true;
|
||||
lastKnownPlayerPosition = target.position;
|
||||
|
||||
Debug.DrawLine(transform.position, target.position, Color.blue, 0.1f);
|
||||
break; // Thấy player rồi thì dừng vòng lặp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitBehaviorTree()
|
||||
{
|
||||
// Player có artifact -> focus + shoot
|
||||
// 1. Cầm Artifact -> Đứng bắn
|
||||
var laserSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckHasArtifact),
|
||||
new TaskNode(ActionFocusAndShoot)
|
||||
});
|
||||
|
||||
// Thấy player -> chạy tới
|
||||
// 2. Thấy Player -> Đuổi theo
|
||||
var chaseSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanSeePlayer),
|
||||
new TaskNode(ActionMoveToPlayer)
|
||||
});
|
||||
|
||||
// Không thấy ai -> Tuần tra bằng NavMesh
|
||||
// 3. Mất dấu Player -> Đi tới vị trí cuối cùng để điều tra
|
||||
var investigateSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckShouldInvestigate),
|
||||
new TaskNode(ActionInvestigate)
|
||||
});
|
||||
|
||||
// 4. Không có gì -> Tuần tra theo điểm
|
||||
var patrolNode = new TaskNode(ActionPatrol);
|
||||
|
||||
behaviorTreeRoot = new Selector(new List<Node>
|
||||
{
|
||||
laserSequence,
|
||||
chaseSequence,
|
||||
investigateSequence,
|
||||
patrolNode
|
||||
});
|
||||
}
|
||||
@@ -109,22 +169,12 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
private NodeState CheckCanSeePlayer()
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
return canSeePlayer ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
float distance = Vector3.Distance(transform.position, player.position);
|
||||
|
||||
if (distance <= detectRange)
|
||||
{
|
||||
if (!hasSpottedPlayer)
|
||||
{
|
||||
hasSpottedPlayer = true;
|
||||
AudioManager.Instance?.Play(alertSound, position: transform.position);
|
||||
}
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
hasSpottedPlayer = false; // Reset nếu player ra khỏi tầm mắt
|
||||
return NodeState.Failure;
|
||||
private NodeState CheckShouldInvestigate()
|
||||
{
|
||||
return isInvestigating ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -133,32 +183,19 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
private NodeState ActionPatrol()
|
||||
{
|
||||
// Debug.Log("Patrolling...");
|
||||
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
|
||||
if (patrolPoints.Length == 0) return NodeState.Failure;
|
||||
|
||||
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
|
||||
Debug.Log("Patrolling...");
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed;
|
||||
|
||||
// Kiểm tra xem NPC đã đến điểm đích chưa
|
||||
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
|
||||
// Đi tới điểm tuần tra hiện tại
|
||||
agent.SetDestination(patrolPoints[currentPatrolIndex].position);
|
||||
|
||||
// Nếu đã tới nơi, chuyển sang điểm tiếp theo
|
||||
if (agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending)
|
||||
{
|
||||
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;
|
||||
}
|
||||
currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Length;
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
@@ -168,39 +205,48 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
|
||||
// Debug.Log("Chasing Player");
|
||||
|
||||
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
|
||||
|
||||
Debug.Log("Chasing Player...");
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed; // Phục hồi tốc độ rượt đuổi
|
||||
agent.speed = chaseSpeed;
|
||||
agent.SetDestination(player.position);
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionInvestigate()
|
||||
{
|
||||
Debug.Log("Investigating last known position...");
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed;
|
||||
|
||||
agent.SetDestination(lastKnownPlayerPosition);
|
||||
|
||||
// Nếu đi tới nơi mà vẫn không thấy player -> Hủy điều tra, quay về tuần tra
|
||||
if (agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending)
|
||||
{
|
||||
isInvestigating = false;
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionFocusAndShoot()
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
|
||||
// Debug.Log("Focus and Shoot!");
|
||||
agent.isStopped = true; // Đứng lại để bắn
|
||||
|
||||
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
|
||||
|
||||
// Dừng NavMeshAgent lại để đứng bắn, tránh bị trượt
|
||||
agent.isStopped = true;
|
||||
|
||||
// Focus player
|
||||
// Xoay người về phía player
|
||||
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, rotateSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Shoot with random delay
|
||||
// Bắn
|
||||
if (Time.time >= nextShootTime)
|
||||
{
|
||||
ShootLaser();
|
||||
@@ -214,9 +260,67 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
if (laserPrefab == null || firePoint == null) return;
|
||||
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
|
||||
AudioManager.Instance?.Play(shootSound, position: transform.position);
|
||||
// Debug.Log("Laser Shot!");
|
||||
Debug.Log("Laser Shot!");
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region DODGE MECHANIC
|
||||
|
||||
private IEnumerator DodgeRoutine()
|
||||
{
|
||||
Debug.Log("Dodging!");
|
||||
isDodging = true;
|
||||
nextDodgeTime = Time.time + dodgeCooldown;
|
||||
|
||||
// 1. Tắt AI tìm đường để Vật lý tiếp quản
|
||||
agent.enabled = false;
|
||||
rb.isKinematic = false; // Đảm bảo Rigidbody có thể nhận lực
|
||||
|
||||
// 2. Tính toán hướng né: Random nhảy sang Trái hoặc Phải
|
||||
int randomDirection = Random.Range(0, 2) == 0 ? -1 : 1;
|
||||
|
||||
// Lấy vector hướng ngang của NPC nhân với trái (-1) hoặc phải (1)
|
||||
Vector3 dodgeDir = transform.right * randomDirection;
|
||||
|
||||
// Có thể cộng thêm một chút lực nhảy lên (trục Y) nếu muốn NPC hơi nảy lên
|
||||
// dodgeDir.y = 0.5f;
|
||||
|
||||
// 3. Tác dụng lực đẩy tức thời (Impulse)
|
||||
rb.AddForce(dodgeDir * dodgeForce, ForceMode.Impulse);
|
||||
|
||||
// 4. Chờ NPC văng đi trong thời gian chỉ định
|
||||
yield return new WaitForSeconds(dodgeDuration);
|
||||
|
||||
// 5. Thắng gấp (Dừng toàn bộ gia tốc vật lý lại)
|
||||
// Lưu ý: Unity 6 dùng linearVelocity thay vì velocity như các bản cũ
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.angularVelocity = Vector3.zero;
|
||||
|
||||
// 6. Bật lại AI tìm đường
|
||||
rb.isKinematic = true; // Trả lại Rigidbody về trạng thái không ảnh hưởng vật lý
|
||||
agent.enabled = true;
|
||||
|
||||
isDodging = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
// Vẽ FOV trên Scene để dễ debug
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.white;
|
||||
Gizmos.DrawWireSphere(transform.position, viewRadius);
|
||||
|
||||
Vector3 viewAngleA = DirFromAngle(-viewAngle / 2);
|
||||
Vector3 viewAngleB = DirFromAngle(viewAngle / 2);
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawLine(transform.position, transform.position + viewAngleA * viewRadius);
|
||||
Gizmos.DrawLine(transform.position, transform.position + viewAngleB * viewRadius);
|
||||
}
|
||||
|
||||
private Vector3 DirFromAngle(float angleInDegrees)
|
||||
{
|
||||
angleInDegrees += transform.eulerAngles.y;
|
||||
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user