Files
BABA_YAGA/Assets/Scripts/AI NPC/EnemyAI.cs

260 lines
8.2 KiB
C#
Raw Normal View History

2026-06-05 14:10:16 +07:00
using System.Collections;
2026-05-30 17:41:31 +07:00
using System.Collections.Generic;
using UnityEngine;
2026-06-05 15:59:33 +07:00
using UnityEngine.AI;
using System.Linq;
2026-05-30 17:41:31 +07:00
2026-06-05 14:10:16 +07:00
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(Rigidbody))]
2026-06-05 15:59:33 +07:00
[RequireComponent(typeof(FieldOfView))]
2026-05-30 17:41:31 +07:00
public class EnemyAI : MonoBehaviour
{
2026-06-03 13:42:09 +07:00
[Header("References")]
2026-05-30 17:41:31 +07:00
public Transform player;
2026-06-05 15:59:33 +07:00
private NavMeshAgent agent;
private Rigidbody rb;
private FieldOfView fov;
2026-06-03 13:42:09 +07:00
2026-06-05 15:59:33 +07:00
[Header("Movement & Rotation")]
public float moveSpeed = 3f;
2026-06-05 16:06:59 +07:00
public float rotateSpeed = 10f; // Tốc độ xoay mượt mờ khi rượt đuổi
2026-06-03 13:42:09 +07:00
2026-06-05 15:59:33 +07:00
[Header("Patrol Waypoints")]
2026-06-05 14:10:16 +07:00
public Transform[] patrolPoints;
2026-06-05 15:59:33 +07:00
public float patrolWaitTime = 2f;
2026-06-05 14:10:16 +07:00
private int currentPatrolIndex = 0;
2026-06-05 15:59:33 +07:00
private float currentWaitTime;
2026-06-04 15:41:01 +07:00
2026-06-05 15:59:33 +07:00
[Header("Artifact State")]
2026-06-03 13:42:09 +07:00
public bool playerHasArtifact;
2026-06-05 15:59:33 +07:00
[Header("Laser Weapon")]
2026-06-03 13:42:09 +07:00
public GameObject laserPrefab;
public Transform firePoint;
public float minShootDelay = 1f;
public float maxShootDelay = 3f;
2026-06-05 15:59:33 +07:00
private float nextShootTime;
2026-06-03 13:42:09 +07:00
2026-06-05 15:59:33 +07:00
[Header("Dodge Settings (Rigidbody)")]
2026-06-05 16:06:59 +07:00
public float dodgeForce = 10f;
public float dodgeDuration = 0.2f;
public float dodgeCooldown = 1.2f;
2026-06-05 14:10:16 +07:00
private bool isDodging = false;
2026-06-05 15:59:33 +07:00
private float nextDodgeTime;
2026-06-05 16:06:59 +07:00
// Root của Behavior Tree
2026-05-30 17:41:31 +07:00
public Node behaviorTreeRoot;
2026-06-03 13:42:09 +07:00
private void Start()
2026-05-30 17:41:31 +07:00
{
2026-06-04 15:41:01 +07:00
agent = GetComponent<NavMeshAgent>();
2026-06-05 14:10:16 +07:00
rb = GetComponent<Rigidbody>();
2026-06-05 15:59:33 +07:00
fov = GetComponent<FieldOfView>();
2026-06-04 23:01:39 +07:00
2026-06-05 15:59:33 +07:00
agent.speed = moveSpeed;
2026-06-05 14:10:16 +07:00
2026-06-05 16:06:59 +07:00
// Tự động tìm tất cả điểm PatrolPoint có trên Map
2026-06-05 15:59:33 +07:00
patrolPoints = GameObject.FindGameObjectsWithTag("PatrolPoint")
.Select(go => go.transform).ToArray();
2026-06-05 16:06:59 +07:00
// Setup Rigidbody ban đầu ở chế độ Kinematic để tránh lỗi xung đột NavMesh thường ngày
2026-06-05 15:59:33 +07:00
rb.isKinematic = true;
rb.freezeRotation = true;
FindPlayer();
2026-05-30 17:41:31 +07:00
InitBehaviorTree();
}
2026-06-03 13:42:09 +07:00
private void Update()
2026-05-30 17:41:31 +07:00
{
2026-06-05 14:10:16 +07:00
if (player == null) FindPlayer();
2026-06-05 15:59:33 +07:00
2026-06-05 16:06:59 +07:00
// Luôn cập nhật cây hành vi
2026-06-03 13:42:09 +07:00
behaviorTreeRoot?.Evaluate();
2026-05-30 17:41:31 +07:00
}
2026-06-04 15:41:01 +07:00
private void FindPlayer()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
2026-06-05 14:10:16 +07:00
if (playerObj != null) player = playerObj.transform;
}
2026-06-03 13:42:09 +07:00
private void InitBehaviorTree()
2026-05-30 17:41:31 +07:00
{
2026-06-05 16:06:59 +07:00
// 1. Ưu tiên cao nhất: Kiểm tra né đòn
2026-06-05 15:59:33 +07:00
var dodgeNode = new TaskNode(CheckAndActionDodge);
2026-06-05 16:06:59 +07:00
// 2. Ưu tiên hai: Player lấy được cổ vật -> Đứng lại nhắm bắn
2026-06-03 13:42:09 +07:00
var laserSequence = new Sequence(new List<Node>
{
new TaskNode(CheckHasArtifact),
new TaskNode(ActionFocusAndShoot)
});
2026-06-05 16:06:59 +07:00
// 3. Ưu tiên ba: Tương tác đuổi theo bằng Vector hoặc Đi điều tra vết tích cuối cùng
2026-06-05 15:59:33 +07:00
var trackingSelector = new Selector(new List<Node>
2026-06-05 14:10:16 +07:00
{
2026-06-05 16:06:59 +07:00
new Sequence(new List<Node> { new TaskNode(CheckCanSeePlayer), new TaskNode(ActionChasePlayer) }),
2026-06-05 15:59:33 +07:00
new Sequence(new List<Node> { new TaskNode(CheckHasInvestigateTarget), new TaskNode(ActionInvestigate) })
2026-06-05 14:10:16 +07:00
});
2026-06-05 16:06:59 +07:00
// 4. Mặc định: Đi tuần tra theo các Waypoint
2026-06-04 15:41:01 +07:00
var patrolNode = new TaskNode(ActionPatrol);
2026-06-03 13:42:09 +07:00
2026-06-05 16:06:59 +07:00
// Gom tất cả vào bộ Selector gốc
2026-05-30 17:41:31 +07:00
behaviorTreeRoot = new Selector(new List<Node>
{
2026-06-05 15:59:33 +07:00
dodgeNode,
2026-06-03 13:42:09 +07:00
laserSequence,
2026-06-05 15:59:33 +07:00
trackingSelector,
2026-06-04 15:41:01 +07:00
patrolNode
2026-05-30 17:41:31 +07:00
});
}
2026-06-05 16:06:59 +07:00
#region CONDITIONS
2026-06-05 15:59:33 +07:00
private NodeState CheckAndActionDodge()
{
if (isDodging) return NodeState.Running;
2026-06-05 16:06:59 +07:00
// Chỉ né khi: Thấy Player VÀ Player click chuột trái VÀ đã hồi chiêu né
2026-06-05 15:59:33 +07:00
if (fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
{
StartCoroutine(DodgeRollRoutine());
nextDodgeTime = Time.time + dodgeCooldown;
return NodeState.Running;
}
return NodeState.Failure;
}
2026-06-03 13:42:09 +07:00
private NodeState CheckHasArtifact()
{
2026-06-04 15:41:01 +07:00
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
2026-06-03 13:42:09 +07:00
}
private NodeState CheckCanSeePlayer()
{
2026-06-05 15:59:33 +07:00
return fov.canSeePlayer ? NodeState.Success : NodeState.Failure;
2026-06-05 14:10:16 +07:00
}
2026-06-03 13:42:09 +07:00
2026-06-05 15:59:33 +07:00
private NodeState CheckHasInvestigateTarget()
2026-06-05 14:10:16 +07:00
{
2026-06-05 15:59:33 +07:00
return fov.lastKnownPlayerPosition != Vector3.zero ? NodeState.Success : NodeState.Failure;
2026-06-03 13:42:09 +07:00
}
#endregion
#region ACTIONS
2026-06-05 15:59:33 +07:00
private IEnumerator DodgeRollRoutine()
{
isDodging = true;
2026-06-05 16:06:59 +07:00
agent.enabled = false; // Tắt tạm thời Agent để giải phóng vật lý
rb.isKinematic = false; // Bật chế độ tương tác vật lý thực tế
2026-06-05 15:59:33 +07:00
2026-06-05 16:06:59 +07:00
// Tính toán hướng né: Vuông góc góc nhìn đối diện với Player (Né sang trái hoặc phải)
2026-06-05 15:59:33 +07:00
Vector3 directionToPlayer = (player.position - transform.position).normalized;
Vector3 perpendicularDir = new Vector3(-directionToPlayer.z, 0, directionToPlayer.x);
Vector3 dodgeDirection = (Random.Range(0, 2) == 0 ? perpendicularDir : -perpendicularDir).normalized;
2026-06-05 16:06:59 +07:00
// Búng vật lý bằng lực Impulse
2026-06-05 15:59:33 +07:00
rb.AddForce(dodgeDirection * dodgeForce, ForceMode.Impulse);
yield return new WaitForSeconds(dodgeDuration);
2026-06-05 16:06:59 +07:00
// Trả lại trạng thái cũ cho NavMesh
rb.linearVelocity = Vector3.zero; // Cú pháp Unity 6
2026-06-05 15:59:33 +07:00
rb.isKinematic = true;
agent.enabled = true;
isDodging = false;
}
2026-06-05 16:06:59 +07:00
private NodeState ActionChasePlayer()
{
if (player == null) return NodeState.Failure;
Debug.Log("Chasing Player via Direct Vector!");
agent.isStopped = false;
// 1. Tính toán hướng đi thẳng tắp (Bỏ trục Y)
Vector3 dir = player.position - transform.position;
dir.y = 0f;
dir.Normalize();
// 2. Di chuyển tịnh tiến bằng Vector tích hợp qua Agent.Move để cản xuyên tường
Vector3 movement = dir * moveSpeed * Time.deltaTime;
agent.Move(movement);
// 3. Tự xoay mượt mà theo hướng di chuyển
if (dir != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
}
return NodeState.Running;
}
2026-06-04 15:41:01 +07:00
private NodeState ActionPatrol()
2026-05-30 17:41:31 +07:00
{
2026-06-05 14:10:16 +07:00
if (patrolPoints.Length == 0) return NodeState.Failure;
2026-06-04 21:26:19 +07:00
2026-06-05 14:10:16 +07:00
agent.isStopped = false;
2026-06-05 16:06:59 +07:00
agent.speed = moveSpeed * 0.5f;
2026-06-05 14:10:16 +07:00
agent.SetDestination(patrolPoints[currentPatrolIndex].position);
2026-06-04 15:41:01 +07:00
2026-06-05 15:59:33 +07:00
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
2026-06-05 14:10:16 +07:00
{
2026-06-05 15:59:33 +07:00
currentWaitTime += Time.deltaTime;
if (currentWaitTime >= patrolWaitTime)
{
currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Length;
currentWaitTime = 0f;
}
2026-06-04 15:41:01 +07:00
}
2026-05-30 17:41:31 +07:00
return NodeState.Running;
}
2026-06-05 14:10:16 +07:00
private NodeState ActionInvestigate()
{
2026-06-04 15:41:01 +07:00
agent.isStopped = false;
2026-06-05 16:06:59 +07:00
agent.speed = moveSpeed * 0.7f;
2026-06-05 15:59:33 +07:00
agent.SetDestination(fov.lastKnownPlayerPosition);
2026-06-05 14:10:16 +07:00
2026-06-05 15:59:33 +07:00
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
2026-06-05 14:10:16 +07:00
{
2026-06-05 16:06:59 +07:00
fov.lastKnownPlayerPosition = Vector3.zero; // Không thấy ai nữa thì hủy điểm nghi vấn
2026-06-05 15:59:33 +07:00
return NodeState.Success;
2026-06-05 14:10:16 +07:00
}
2026-05-30 17:41:31 +07:00
return NodeState.Running;
}
2026-06-03 13:42:09 +07:00
private NodeState ActionFocusAndShoot()
2026-05-30 17:41:31 +07:00
{
2026-06-05 16:06:59 +07:00
agent.isStopped = true;
2026-06-04 21:26:19 +07:00
2026-06-04 15:41:01 +07:00
Vector3 dir = player.position - transform.position;
2026-06-03 13:42:09 +07:00
dir.y = 0f;
if (dir != Vector3.zero)
{
2026-06-04 15:41:01 +07:00
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
if (Time.time >= nextShootTime)
2026-05-30 17:41:31 +07:00
{
2026-06-03 13:42:09 +07:00
ShootLaser();
2026-06-04 15:41:01 +07:00
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
return NodeState.Running;
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
private void ShootLaser()
{
2026-06-04 15:41:01 +07:00
if (laserPrefab == null || firePoint == null) return;
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
2026-06-03 13:42:09 +07:00
}
#endregion
2026-06-05 16:06:59 +07:00
}