Merge branch 'main' of https://scove-vault.duckdns.org/scove/HALLUCINATION
This commit is contained in:
16
Assets/Scripts/AI NPC/AutoDestroy.cs
Normal file
16
Assets/Scripts/AI NPC/AutoDestroy.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class AutoDestroy : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/AutoDestroy.cs.meta
Normal file
2
Assets/Scripts/AI NPC/AutoDestroy.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 950ee3c6c086a3b4fa9a7f1e544c1651
|
||||
@@ -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,255 +35,167 @@ 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("Conversation")]
|
||||
public string npcName = "Guard";
|
||||
public string persona = "You are a grumpy guard protecting gold.";
|
||||
public float talkRange = 4f;
|
||||
public float talkCooldown = 30f;
|
||||
private float lastTalkTime;
|
||||
private bool isTalking;
|
||||
private EnemyAI talkingPartner;
|
||||
private Hallucinate.UI.ChatBubble chatBubble;
|
||||
|
||||
[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>();
|
||||
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
|
||||
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()
|
||||
{
|
||||
// Ưu tiên 1: Player có artifact -> focus + shoot (Cao nhất)
|
||||
// 1. Cầm Artifact -> Đứng bắn
|
||||
var laserSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckHasArtifact),
|
||||
new TaskNode(ActionFocusAndShoot)
|
||||
});
|
||||
|
||||
// Ưu tiên 2: Thấy player -> rượt đuổi
|
||||
// 2. Thấy Player -> Đuổi theo
|
||||
var chaseSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanSeePlayer),
|
||||
new TaskNode(ActionMoveToPlayer)
|
||||
});
|
||||
|
||||
// Ưu tiên 3: Gần NPC khác -> nói chuyện (Mới)
|
||||
var talkSequence = new Sequence(new List<Node>
|
||||
// 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(CheckCanTalkToNPC),
|
||||
new TaskNode(ActionTalk)
|
||||
new TaskNode(CheckShouldInvestigate),
|
||||
new TaskNode(ActionInvestigate)
|
||||
});
|
||||
|
||||
// Ưu tiên cuối: Tuần tra
|
||||
// 4. Không có gì -> Tuần tra theo điểm
|
||||
var patrolNode = new TaskNode(ActionPatrol);
|
||||
|
||||
behaviorTreeRoot = new Selector(new List<Node>
|
||||
{
|
||||
laserSequence,
|
||||
chaseSequence,
|
||||
talkSequence,
|
||||
investigateSequence,
|
||||
patrolNode
|
||||
});
|
||||
}
|
||||
|
||||
#region CONDITIONS
|
||||
|
||||
private NodeState CheckCanTalkToNPC()
|
||||
{
|
||||
if (playerHasArtifact || hasSpottedPlayer) return NodeState.Failure;
|
||||
if (Time.time < lastTalkTime + talkCooldown) return NodeState.Failure;
|
||||
if (isTalking) return NodeState.Success;
|
||||
|
||||
// Tìm NPC gần nhất
|
||||
Collider[] hitColliders = Physics.OverlapSphere(transform.position, talkRange);
|
||||
foreach (var hit in hitColliders)
|
||||
{
|
||||
if (hit.gameObject != gameObject && hit.CompareTag("Enemy"))
|
||||
{
|
||||
EnemyAI other = hit.GetComponent<EnemyAI>();
|
||||
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
|
||||
{
|
||||
talkingPartner = other;
|
||||
return NodeState.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NodeState.Failure;
|
||||
}
|
||||
|
||||
private NodeState CheckHasArtifact()
|
||||
{
|
||||
// Khi bị phát hiện hoặc player có artifact, ngắt lời ngay
|
||||
if (playerHasArtifact || hasSpottedPlayer) StopConversation();
|
||||
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
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);
|
||||
StopConversation(); // Ngắt hội thoại khi thấy player
|
||||
}
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
hasSpottedPlayer = false;
|
||||
return NodeState.Failure;
|
||||
private NodeState CheckShouldInvestigate()
|
||||
{
|
||||
return isInvestigating ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ACTIONS
|
||||
|
||||
private NodeState ActionTalk()
|
||||
{
|
||||
if (talkingPartner == null) return NodeState.Failure;
|
||||
|
||||
if (!isTalking)
|
||||
{
|
||||
isTalking = true;
|
||||
agent.isStopped = true;
|
||||
|
||||
// Xoay về phía bạn
|
||||
FaceTarget(talkingPartner.transform.position);
|
||||
|
||||
// Bắt đầu hội thoại qua Gemini
|
||||
StartNPCConversation();
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private void StartNPCConversation()
|
||||
{
|
||||
string prompt = $"You are {npcName}. You are talking to your fellow guard {talkingPartner.npcName}. " +
|
||||
"Keep it short (1 sentence). Topic: gold security or complaining about work.";
|
||||
|
||||
Hallucinate.AI.GeminiService.Instance.GetResponse(persona, prompt, (response) => {
|
||||
if (chatBubble != null) chatBubble.Show(response);
|
||||
|
||||
// Hẹn giờ kết thúc hội thoại
|
||||
Invoke(nameof(EndConversation), 5f);
|
||||
});
|
||||
|
||||
// Thông báo cho bạn diễn cũng dừng lại để "nghe"
|
||||
talkingPartner.OnPartnerTalked(this);
|
||||
}
|
||||
|
||||
public void OnPartnerTalked(EnemyAI partner)
|
||||
{
|
||||
isTalking = true;
|
||||
talkingPartner = partner;
|
||||
agent.isStopped = true;
|
||||
FaceTarget(partner.transform.position);
|
||||
|
||||
// Chờ bạn nói xong mới phản hồi (Tùy chọn: có thể thêm logic phản hồi ở đây)
|
||||
Invoke(nameof(EndConversation), 6f);
|
||||
}
|
||||
|
||||
private void EndConversation()
|
||||
{
|
||||
isTalking = false;
|
||||
lastTalkTime = Time.time;
|
||||
if (agent != null) agent.isStopped = false;
|
||||
talkingPartner = null;
|
||||
}
|
||||
|
||||
private void StopConversation()
|
||||
{
|
||||
if (!isTalking) return;
|
||||
CancelInvoke(nameof(EndConversation));
|
||||
EndConversation();
|
||||
if (chatBubble != null) chatBubble.Show("Suỵt! Có gì đó không ổn...", 2f);
|
||||
}
|
||||
|
||||
private void FaceTarget(Vector3 targetPos)
|
||||
{
|
||||
Vector3 dir = targetPos - transform.position;
|
||||
dir.y = 0;
|
||||
if (dir != Vector3.zero)
|
||||
{
|
||||
transform.rotation = Quaternion.LookRotation(dir);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -286,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();
|
||||
@@ -332,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));
|
||||
}
|
||||
}
|
||||
198
Assets/Scripts/AI NPC/KamikazeAI.cs
Normal file
198
Assets/Scripts/AI NPC/KamikazeAI.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class KamikazeAI : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
public Transform player;
|
||||
|
||||
[Header("Detection")]
|
||||
public float detectRange = 15f;
|
||||
private bool canSeePlayer = false;
|
||||
|
||||
[Header("Movement & Random Patrol")]
|
||||
public float patrolSpeed = 2.5f;
|
||||
public float chaseSpeed = 7f;
|
||||
public float patrolRadius = 12f; // Bán kính của khu vực tuần tra ngẫu nhiên
|
||||
public float patrolWaitTime = 2f; // Thời gian đứng nghỉ trước khi đổi sang điểm ngẫu nhiên mới
|
||||
|
||||
private Vector3 startPosition; // Tâm của khu vực tuần tra (Vị trí ban đầu)
|
||||
private float currentWaitTime;
|
||||
private NavMeshAgent agent;
|
||||
private bool isExploding = false;
|
||||
public Node behaviorTreeRoot;
|
||||
public GameObject explosionEffectPrefab;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
|
||||
// Lưu lại vị trí xuất phát để làm tâm, NPC sẽ chỉ đi loay hoay quanh khu vực này
|
||||
startPosition = transform.position;
|
||||
|
||||
InitBehaviorTree();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isExploding) return;
|
||||
|
||||
if (player == null) FindPlayer();
|
||||
else CheckVision();
|
||||
|
||||
behaviorTreeRoot?.Evaluate();
|
||||
}
|
||||
|
||||
private void FindPlayer()
|
||||
{
|
||||
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
|
||||
if (playerObj != null) player = playerObj.transform;
|
||||
}
|
||||
|
||||
private void CheckVision()
|
||||
{
|
||||
if (Vector3.Distance(transform.position, player.position) <= detectRange)
|
||||
canSeePlayer = true;
|
||||
else
|
||||
canSeePlayer = false;
|
||||
}
|
||||
|
||||
private void InitBehaviorTree()
|
||||
{
|
||||
var explodeSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckIsCloseEnoughToExplode),
|
||||
new TaskNode(ActionTriggerExplosion)
|
||||
});
|
||||
|
||||
var chaseSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanSeePlayer),
|
||||
new TaskNode(ActionChase)
|
||||
});
|
||||
|
||||
// Hành động tuần tra ngẫu nhiên
|
||||
var patrolNode = new TaskNode(ActionRandomPatrol);
|
||||
|
||||
behaviorTreeRoot = new Selector(new List<Node>
|
||||
{
|
||||
explodeSequence,
|
||||
chaseSequence,
|
||||
patrolNode
|
||||
});
|
||||
}
|
||||
|
||||
#region CONDITIONS
|
||||
|
||||
private NodeState CheckCanSeePlayer()
|
||||
{
|
||||
return canSeePlayer ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
private NodeState CheckIsCloseEnoughToExplode()
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
float dist = Vector3.Distance(transform.position, player.position);
|
||||
return dist <= 3f ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ACTIONS
|
||||
|
||||
// HÀM TUẦN TRA NGẪU NHIÊN MỚI
|
||||
private NodeState ActionRandomPatrol()
|
||||
{
|
||||
Debug.Log("Wandering randomly...");
|
||||
agent.isStopped = false;
|
||||
agent.speed = patrolSpeed;
|
||||
|
||||
// Kiểm tra xem NPC đã đi đến điểm ngẫu nhiên hiện tại chưa
|
||||
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
|
||||
{
|
||||
currentWaitTime += Time.deltaTime;
|
||||
|
||||
// Đứng đợi hết thời gian quy định rồi mới tìm đường mới
|
||||
if (currentWaitTime >= patrolWaitTime)
|
||||
{
|
||||
// 1. Lấy một điểm ngẫu nhiên trong không gian hình cầu dựa trên bán kính
|
||||
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
|
||||
randomDirection += startPosition; // Cộng với tâm ban đầu để giới hạn khu vực
|
||||
|
||||
NavMeshHit hit;
|
||||
// 2. Ép tọa độ ngẫu nhiên đó phải nằm TRÊN bề mặt xanh của NavMesh (tránh kẹt tường)
|
||||
// Số 1 ở cuối là Area Mask (thường là Walkable)
|
||||
if (NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1))
|
||||
{
|
||||
agent.SetDestination(hit.position);
|
||||
}
|
||||
|
||||
currentWaitTime = 0f; // Reset thời gian chờ
|
||||
}
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionChase()
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
|
||||
Debug.Log("Kamikaze is rushing you!");
|
||||
agent.isStopped = false;
|
||||
agent.speed = chaseSpeed;
|
||||
agent.SetDestination(player.position);
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionTriggerExplosion()
|
||||
{
|
||||
StartCoroutine(ExplosionRoutine());
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region EXPLOSION LOGIC
|
||||
|
||||
private IEnumerator ExplosionRoutine()
|
||||
{
|
||||
isExploding = true;
|
||||
agent.isStopped = true;
|
||||
agent.velocity = Vector3.zero;
|
||||
|
||||
Debug.Log("BOMB ARMED!");
|
||||
yield return new WaitForSeconds(1.5f);
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
float distToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
if (distToPlayer <= 4f)
|
||||
{
|
||||
Debug.Log("BOOM! Player took damage!");
|
||||
}
|
||||
}
|
||||
|
||||
if (explosionEffectPrefab != null)
|
||||
{
|
||||
Instantiate(explosionEffectPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Vẽ vùng giới hạn tuần tra màu xanh lá cây trên Scene để bạn dễ căn chỉnh độ rộng
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
// Nếu game đang chạy thì vẽ quanh tâm startPosition, nếu chưa chạy thì vẽ quanh vị trí hiện tại
|
||||
Vector3 center = Application.isPlaying ? startPosition : transform.position;
|
||||
Gizmos.DrawWireSphere(center, patrolRadius);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/KamikazeAI.cs.meta
Normal file
2
Assets/Scripts/AI NPC/KamikazeAI.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6008ec58fb909034abd7293b55f0d558
|
||||
Reference in New Issue
Block a user