This commit is contained in:
2026-06-05 18:46:19 +07:00
parent 7f7139612d
commit 91183760fb
12 changed files with 357 additions and 513 deletions

View File

@@ -14,13 +14,19 @@ namespace Hallucinate.UI
private void Awake()
{
mainCameraTransform = Camera.main.transform;
canvasGroup.alpha = 0;
gameObject.SetActive(false);
if (canvasGroup != null) canvasGroup.alpha = 0;
// gameObject.SetActive(false); // Bỏ dòng này để tránh tắt nhầm NPC gốc
}
private void LateUpdate()
{
// Tìm Camera nếu chưa có (Tránh lỗi Null nếu Camera chưa spawn hoặc bị xóa)
if (mainCameraTransform == null)
{
if (Camera.main != null) mainCameraTransform = Camera.main.transform;
else return;
}
// Billboard effect
transform.LookAt(transform.position + mainCameraTransform.rotation * Vector3.forward, mainCameraTransform.rotation * Vector3.up);
}

View File

@@ -2,53 +2,51 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Linq;
// Quy trình ưu tiên: Né đòn --> Bắn hạ (Artifact) --> Đuổi theo (Vector) --> Điều tra --> Đi tuần
// Quy trình ưu tiên: Né đòn --> Bắn hạ (Artifact) --> Đuổi theo (Vector) --> Điều tra --> Nói chuyện --> Đi tuần
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(FieldOfView))]
public class EnemyAI : MonoBehaviour
{
[Header("Tham chieu he thong")]
[Header("References")]
public Transform player;
private NavMeshAgent agent;
private Rigidbody rb;
private FieldOfView fov;
[Header("Toc do & Di chuyen")]
[Header("Movement Settings")]
public float moveSpeed = 3f;
public float rotateSpeed = 10f;
[Header("He thong di tuan (Waypoints)")]
[Header("Patrol Settings")]
public Transform[] patrolWaypoints;
public int currentWaypointIndex = 0;
public float patrolWaitTime = 2f;
private float currentWaitTime = 0f;
[Header("Trang thai Co vat")]
[Header("Combat State")]
public bool playerHasArtifact;
[Header("Vu khi Laser")]
public GameObject laserPrefab;
public Transform firePoint;
public float minShootDelay = 1f;
public float maxShootDelay = 3f;
private float nextShootTime;
[Header("Co che Ne don (Vat ly)")]
[Header("Dodge Settings")]
public float dodgeForce = 10f;
public float dodgeDuration = 0.2f;
public float dodgeCooldown = 1.2f;
private bool isDodging = false;
private float nextDodgeTime = 0f;
[Header("Conversation")]
[Header("Conversation Settings")]
public string npcName = "Guard";
public string persona = "You are a grumpy guard protecting gold.";
public float talkRange = 4f;
public float talkCooldown = 30f;
[TextArea] public string persona = "You are a grumpy guard protecting gold.";
public float talkRange = 10f;
public float talkCooldown = 15f;
private float lastTalkTime;
private bool isTalking;
public bool isTalking; // Public để debug
private EnemyAI talkingPartner;
private Hallucinate.UI.ChatBubble chatBubble;
@@ -59,8 +57,9 @@ public class EnemyAI : MonoBehaviour
agent = GetComponent<NavMeshAgent>();
rb = GetComponent<Rigidbody>();
fov = GetComponent<FieldOfView>();
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
// Thiết lập Rigidbody chuẩn Unity 6 để sẵn sàng nhận lực né
// Rigidbody setup cho Unity 6
rb.isKinematic = true;
rb.freezeRotation = true;
@@ -69,48 +68,30 @@ public class EnemyAI : MonoBehaviour
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null) player = playerObj.transform;
}
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
/*
// Tạm thời comment đoạn này để tránh lỗi Tag chưa định nghĩa
if (patrolWaypoints == null || patrolWaypoints.Length == 0)
{
patrolWaypoints = GameObject.FindGameObjectsWithTag("PatrolPoint")
.Select(go => go.transform).ToArray();
}
*/
InitTree();
Debug.Log($"<color=white>[AI {npcName}] Init complete. Waypoints: {patrolWaypoints.Length}</color>");
}
void InitTree()
{
// 1. Nhánh Né đòn (Khi player bấm chuột trái tấn công)
var dodgeSequence = new Sequence(new List<Node>
{
new TaskNode(CheckDodgeConditions),
new TaskNode(ActionDodge)
});
// 2. Nhánh Tấn công Laser (Khi player lấy được cổ vật)
var laserSequence = new Sequence(new List<Node>
{
new TaskNode(CheckHasArtifact),
new TaskNode(ActionFocusAndShoot)
});
// 3. Nhánh Đuổi theo (Sử dụng agent.Move di chuyển thẳng bằng Vector)
var chaseSequence = new Sequence(new List<Node>
{
new TaskNode(CheckCanSeePlayer),
new TaskNode(ActionChasePlayer)
});
// 4. Nhánh Điều tra (Khi mất dấu, đi kiểm tra vị trí cuối cùng)
var investigateSequence = new Sequence(new List<Node>
{
new TaskNode(CheckHasInvestigateTarget),
new TaskNode(ActionInvestigate)
});
var talkSequence = new Sequence(new List<Node>
{
new TaskNode(CheckCanTalkToNPC),
new TaskNode(ActionTalk)
});
// 5. Nhánh Đi tuần mặc định
var dodgeSequence = new Sequence(new List<Node> { new TaskNode(CheckDodgeConditions), new TaskNode(ActionDodge) });
var laserSequence = new Sequence(new List<Node> { new TaskNode(CheckHasArtifact), new TaskNode(ActionFocusAndShoot) });
var chaseSequence = new Sequence(new List<Node> { new TaskNode(CheckCanSeePlayer), new TaskNode(ActionChasePlayer) });
var investigateSequence = new Sequence(new List<Node> { new TaskNode(CheckHasInvestigateTarget), new TaskNode(ActionInvestigate) });
var talkSequence = new Sequence(new List<Node> { new TaskNode(CheckCanTalkToNPC), new TaskNode(ActionTalk) });
var patrolAction = new TaskNode(ActionPatrol);
// Xây dựng cây tổng hợp theo thứ tự ưu tiên từ trên xuống dưới
rootNode = new Selector(new List<Node>
{
dodgeSequence,
@@ -125,21 +106,27 @@ public class EnemyAI : MonoBehaviour
void Update()
{
if (player == null) return;
// An toàn cho NavMeshAgent
if (!agent.isOnNavMesh)
{
Debug.LogWarning($"[AI {npcName}] NPC is NOT on NavMesh!");
return;
}
if (!isTalking && !isDodging && agent.isStopped)
agent.isStopped = false;
rootNode?.Evaluate();
}
#region CONDITIONS (CAC HAM KIEM TRA)
#region CONDITIONS
private NodeState CheckDodgeConditions()
{
// Nếu đang trong quá trình né thì luôn cho phép chạy tiếp hành động né
if (isDodging) return NodeState.Success;
// Điều kiện kích hoạt né: Thấy player + Player nhấn chuột trái + Hết cooldown
if (fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
{
if (fov != null && fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
return NodeState.Success;
}
return NodeState.Failure;
}
@@ -151,132 +138,65 @@ public class EnemyAI : MonoBehaviour
private NodeState CheckCanSeePlayer()
{
return fov.canSeePlayer ? NodeState.Success : NodeState.Failure;
bool canSee = fov != null && fov.canSeePlayer;
if (canSee) StopConversation();
return canSee ? NodeState.Success : NodeState.Failure;
}
private NodeState CheckHasInvestigateTarget()
{
return fov.lastKnownPlayerPosition != Vector3.zero ? NodeState.Success : NodeState.Failure;
return (fov != null && fov.lastKnownPlayerPosition != Vector3.zero) ? NodeState.Success : NodeState.Failure;
}
#endregion
#region ACTIONS (CAC HAM HANH DONG)
private NodeState ActionDodge()
{
if (!isDodging)
{
StartCoroutine(DodgeRollRoutine());
nextDodgeTime = Time.time + dodgeCooldown;
}
return NodeState.Running;
}
private IEnumerator DodgeRollRoutine()
{
isDodging = true;
agent.enabled = false; // Tắt định vị để nhường quyền cho Vật lý
rb.isKinematic = false; // Bật chế độ vật lý động
// Tính hướng né vuông góc với Player
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;
// Đẩy bằng lực Impulse vật lý thực tế
rb.AddForce(dodgeDirection * dodgeForce, ForceMode.Impulse);
yield return new WaitForSeconds(dodgeDuration);
// Trả lại quyền cho NavMeshAgent sau khi né xong
rb.linearVelocity = Vector3.zero; // Cú pháp chuẩn của Unity 6
rb.isKinematic = true;
agent.enabled = true;
isDodging = false;
}
private NodeState ActionChasePlayer()
{
agent.isStopped = false;
agent.speed = moveSpeed;
// 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
Vector3 movement = dir * moveSpeed * Time.deltaTime;
agent.Move(movement);
// 3. Xoay mượt mà theo hướng di chuyển của Vector đúng ý bạn
if (dir != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
}
return NodeState.Running;
}
private NodeState CheckCanTalkToNPC()
{
if (playerHasArtifact || fov.canSeePlayer) return NodeState.Failure;
if (playerHasArtifact || (fov != null && fov.canSeePlayer)) return NodeState.Failure;
if (Time.time < lastTalkTime + talkCooldown) return NodeState.Failure;
if (isTalking) return NodeState.Success;
// Tìm NPC gần nhất
// Quét tìm NPC
Collider[] hitColliders = Physics.OverlapSphere(transform.position, talkRange);
foreach (var hit in hitColliders)
{
if (hit.gameObject != gameObject && hit.CompareTag("Enemy"))
if (hit.gameObject == gameObject) continue;
EnemyAI other = hit.GetComponentInParent<EnemyAI>();
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
{
EnemyAI other = hit.GetComponent<EnemyAI>();
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
// Chỉ ID nhỏ hơn gọi để tránh trùng
if (gameObject.GetInstanceID() < other.gameObject.GetInstanceID())
{
talkingPartner = other;
return NodeState.Success;
}
}
}
return NodeState.Failure;
}
private NodeState ActionTalk()
#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);
Debug.Log($"<color=yellow>[AI {npcName}] Talking to {talkingPartner.npcName}</color>");
string prompt = $"You are {npcName}. Speak 1 short sentence in English to your colleague {talkingPartner.npcName} about the shift.";
Hallucinate.AI.GeminiService.Instance.GetResponse(persona, prompt, (response) => {
if (chatBubble != null) chatBubble.Show(response);
Invoke(nameof(EndConversation), 5f);
});
talkingPartner.OnPartnerTalked(this);
}
return NodeState.Running;
}
public void OnPartnerTalked(EnemyAI partner)
@@ -285,8 +205,6 @@ public class EnemyAI : MonoBehaviour
talkingPartner = partner;
agent.isStopped = true;
FaceTarget(partner.transform.position);
// Chờ bạn nói xong mới phản hồi
Invoke(nameof(EndConversation), 6f);
}
@@ -294,7 +212,7 @@ public class EnemyAI : MonoBehaviour
{
isTalking = false;
lastTalkTime = Time.time;
if (agent != null) agent.isStopped = false;
if (agent != null && agent.isOnNavMesh) agent.isStopped = false;
talkingPartner = null;
}
@@ -303,47 +221,37 @@ public class EnemyAI : MonoBehaviour
if (!isTalking) return;
CancelInvoke(nameof(EndConversation));
EndConversation();
if (chatBubble != null) chatBubble.Show("Suỵt! Có gì đó không ổn...", 2f);
if (chatBubble != null) chatBubble.Show("Wait, what's that?!", 2f);
}
private void FaceTarget(Vector3 targetPos)
private NodeState ActionPatrol()
{
Vector3 dir = targetPos - transform.position;
dir.y = 0;
if (dir != Vector3.zero)
if (patrolWaypoints == null || patrolWaypoints.Length == 0) return NodeState.Failure;
agent.isStopped = false;
agent.speed = moveSpeed * 0.5f;
var target = patrolWaypoints[currentWaypointIndex];
agent.SetDestination(target.position);
if (Vector3.Distance(transform.position, target.position) < 1.5f)
{
transform.rotation = Quaternion.LookRotation(dir);
currentWaitTime += Time.deltaTime;
if (currentWaitTime >= patrolWaitTime)
{
currentWaypointIndex = (currentWaypointIndex + 1) % patrolWaypoints.Length;
currentWaitTime = 0f;
}
}
}
private NodeState ActionFocusAndShoot()
{
agent.isStopped = true; // Đứng im bắn cố định khi có cổ vật
// Tập trung xoay người nhìn 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);
}
// Cơ chế đếm ngược bắn Laser ngẫu nhiên giống code cũ của bạn
if (Time.time >= nextShootTime)
{
ShootLaser();
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
return NodeState.Running;
}
private void ShootLaser()
private NodeState ActionChasePlayer()
{
if (laserPrefab == null || firePoint == null) return;
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
Debug.Log("Laser Shot!");
agent.isStopped = false;
agent.speed = moveSpeed;
agent.SetDestination(player.position);
return NodeState.Running;
}
private NodeState ActionInvestigate()
@@ -351,44 +259,67 @@ public class EnemyAI : MonoBehaviour
agent.isStopped = false;
agent.speed = moveSpeed * 0.7f;
agent.SetDestination(fov.lastKnownPlayerPosition);
// Đi tới vị trí cuối cùng nhìn thấy Player
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
if (Vector3.Distance(transform.position, fov.lastKnownPlayerPosition) < 1.5f)
{
fov.lastKnownPlayerPosition = Vector3.zero; // Xóa vị trí nghi vấn để thoát trạng thái
fov.lastKnownPlayerPosition = Vector3.zero;
return NodeState.Success;
}
return NodeState.Running;
}
private NodeState ActionPatrol()
private NodeState ActionFocusAndShoot()
{
if (patrolWaypoints == null || patrolWaypoints.Length == 0)
agent.isStopped = true;
FaceTarget(player.position);
if (Time.time >= nextShootTime)
{
return NodeState.Failure;
if (laserPrefab && firePoint) Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
agent.isStopped = false;
agent.speed = moveSpeed * 0.5f; // Đi tuần chậm rãi
var targetWaypoint = patrolWaypoints[currentWaypointIndex];
var distanceToTarget = Vector3.Distance(transform.position, targetWaypoint.position);
// Nếu đã đến gần điểm tuần tra الحالي
if (distanceToTarget < 1f)
{
currentWaitTime += Time.deltaTime;
// Chờ một khoảng thời gian trước khi chuyển sang điểm tiếp theo
if (currentWaitTime >= patrolWaitTime)
{
currentWaypointIndex = (currentWaypointIndex + 1) % patrolWaypoints.Length;
currentWaitTime = 0f;
}
}
agent.SetDestination(patrolWaypoints[currentWaypointIndex].position);
return NodeState.Running;
}
private NodeState ActionDodge()
{
if (!isDodging) StartCoroutine(DodgeRollRoutine());
return NodeState.Running;
}
private IEnumerator DodgeRollRoutine()
{
isDodging = true;
agent.enabled = false;
rb.isKinematic = false;
Vector3 dir = (player.position - transform.position).normalized;
Vector3 perp = new Vector3(-dir.z, 0, dir.x);
rb.AddForce((Random.value > 0.5f ? perp : -perp) * dodgeForce, ForceMode.Impulse);
yield return new WaitForSeconds(dodgeDuration);
rb.linearVelocity = Vector3.zero;
rb.isKinematic = true;
agent.enabled = true;
isDodging = false;
}
private void FaceTarget(Vector3 pos)
{
Vector3 dir = (pos - transform.position);
dir.y = 0;
if (dir != Vector3.zero) transform.rotation = Quaternion.LookRotation(dir);
}
#endregion
}
private void OnDrawGizmos()
{
// Vẽ vùng nói chuyện (Xanh lá)
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, talkRange);
// Vẽ đường nối tới bạn diễn nếu đang nói
if (isTalking && talkingPartner != null)
{
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position + Vector3.up, talkingPartner.transform.position + Vector3.up);
}
}
}

View File

@@ -40,11 +40,16 @@ namespace Hallucinate.AI
{
var jsonBody = $@"{{
""systemInstruction"": {{""parts"": [{{ ""text"": ""{persona}"" }}]}},
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}]
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}],
""generationConfig"": {{
""maxOutputTokens"": 60,
""temperature"": 0.7
}}
}}";
var requestURL = $"{geminiURL}?key={apiKey}";
using (var request = new UnityWebRequest(requestURL, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
@@ -56,15 +61,28 @@ namespace Hallucinate.AI
if (request.result == UnityWebRequest.Result.Success)
{
string rawResponse = request.downloadHandler.text;
try
{
var response = JsonUtility.FromJson<GeminiResponse>(request.downloadHandler.text);
if (response?.candidates != null && response.candidates.Length > 0)
var response = JsonUtility.FromJson<GeminiResponse>(rawResponse);
if (response != null &&
response.candidates != null &&
response.candidates.Length > 0 &&
response.candidates[0].content != null &&
response.candidates[0].content.parts != null &&
response.candidates[0].content.parts.Length > 0)
{
onComplete?.Invoke(response.candidates[0].content.parts[0].text);
}
else
{
Debug.LogWarning($"[Gemini] Response structure invalid or blocked. Raw: {rawResponse}");
}
}
catch (Exception e)
{
Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}\nRaw Response: {rawResponse}");
}
catch (Exception e) { Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}"); }
}
else
{

View File

@@ -0,0 +1,23 @@
using UnityEngine;
using Hallucinate.AI;
public class GeminiTest : MonoBehaviour
{
void Start()
{
Debug.Log("<color=cyan>[Gemini Test]</color> Bắt đầu kiểm tra kết nối API...");
if (GeminiService.Instance == null)
{
Debug.LogError("<color=red>[Gemini Test]</color> Không tìm thấy GeminiService Instance! Hãy đảm bảo bạn đã kéo script GeminiService vào một GameObject trong Scene.");
return;
}
string testPersona = "Bạn là một robot kiểm tra hệ thống.";
string testPrompt = "Chào bạn, nếu bạn nhận được tin nhắn này, hãy trả lời: 'Kết nối Gemini thành công!'";
GeminiService.Instance.GetResponse(testPersona, testPrompt, (response) => {
Debug.Log($"<color=green>[Gemini Test] Phản hồi từ API:</color> {response}");
});
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e61a7aa4c1a936a43a97cf67a6e6a559

View File

@@ -12,4 +12,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2f22f37f63bcec14080b11ce5e381ce6, type: 3}
m_Name: vEditorStartupPrefs
m_EditorClassIdentifier:
displayWelcomeScreen: 1
displayWelcomeScreen: 0