Complete Update NPC AI
This commit is contained in:
@@ -10,9 +10,16 @@ using Invector.vCharacterController;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[Serializable]
|
||||
public class DialogueResult { public string text; public float speedMod; public float suspicionMod; }
|
||||
public class DialogueResult
|
||||
{
|
||||
public string text;
|
||||
public float speedMod;
|
||||
public float suspicionMod;
|
||||
public float aggressionMod; // Ảnh hưởng delay bắn (0.1 -> 1.0)
|
||||
public float braveryMod; // Ảnh hưởng ngưỡng Panic
|
||||
public float healthMod; // Hồi máu hoặc mất máu tâm lý
|
||||
}
|
||||
|
||||
// Quy trình ưu tiên: Né đòn (Bị bắn) --> Panic (Thấp máu) --> Bắn hạ (Chiến đấu) --> Đuổi theo --> Điều tra --> Nói chuyện --> Đi tuần
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(vHealthController))]
|
||||
@@ -42,30 +49,36 @@ public class EnemyAI : MonoBehaviour
|
||||
public bool isAggroedBySound;
|
||||
public GameObject laserPrefab;
|
||||
public Transform firePoint;
|
||||
public float minShootDelay = 1.5f;
|
||||
public float maxShootDelay = 3.5f;
|
||||
public float minShootDelay = 1.8f; // Tăng nhẹ delay để đỡ khó
|
||||
public float maxShootDelay = 4.0f;
|
||||
private float nextShootTime;
|
||||
|
||||
[Header("Dodge Settings")]
|
||||
public float dodgeForce = 10f;
|
||||
public float dodgeForce = 8f;
|
||||
public float dodgeDuration = 0.2f;
|
||||
public float dodgeCooldown = 1.2f;
|
||||
public float dodgeCooldown = 2.0f; // Tăng cooldown né đòn
|
||||
private bool isDodging = false;
|
||||
private float nextDodgeTime = 0f;
|
||||
|
||||
[Header("Advanced AI States (New)")]
|
||||
[Header("Advanced AI States")]
|
||||
public bool isPanicking = false;
|
||||
public bool isEnraged = false;
|
||||
public float panicHealthThreshold = 40f; // Dưới 40% máu sẽ panic
|
||||
public float regenRate = 2f; // Hồi 2 máu mỗi giây
|
||||
public float regenDelay = 5f; // Cần 5s không nhận dame để bắt đầu hồi
|
||||
public float panicHealthThreshold = 50f; // Chạy ngược lại khi < 50% máu
|
||||
public float regenRate = 1.5f;
|
||||
public float regenDelay = 5f;
|
||||
private float lastDamageTime;
|
||||
|
||||
[Header("Personality (Randomized)")]
|
||||
private float personalApproachWeight;
|
||||
private float personalMinCombatDistance;
|
||||
private float personalBurstMax;
|
||||
private float personalStrafeIntensity;
|
||||
|
||||
[Header("Artifact Combat Upgrades")]
|
||||
public float minStrafeDuration = 0.5f;
|
||||
public float maxStrafeDuration = 2.2f;
|
||||
public float maxSpreadAngle = 6f;
|
||||
public float burstInterval = 0.12f;
|
||||
public float maxSpreadAngle = 7f;
|
||||
public float burstInterval = 0.15f;
|
||||
|
||||
public float approachWeight = 0.35f;
|
||||
public float minCombatDistance = 5.0f;
|
||||
@@ -79,7 +92,7 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
[Header("Conversation Settings")]
|
||||
public string npcName = "Guard";
|
||||
[TextArea] public string persona = "You are a bored security guard. You love coffee and hate night shifts.";
|
||||
[TextArea] public string persona = "You are a bored security guard.";
|
||||
public float talkRange = 12f;
|
||||
public float talkCooldown = 60f;
|
||||
private float lastTalkTime;
|
||||
@@ -91,7 +104,7 @@ public class EnemyAI : MonoBehaviour
|
||||
public float suspicionLevel = 0f;
|
||||
public float investigationThreshold = 30f;
|
||||
public float alertNeighborsThreshold = 70f;
|
||||
public float alertRange = 25f; // Tăng tầm gọi hội
|
||||
public float alertRange = 30f;
|
||||
|
||||
public Node rootNode;
|
||||
|
||||
@@ -104,13 +117,28 @@ public class EnemyAI : MonoBehaviour
|
||||
mainCollider = GetComponent<Collider>();
|
||||
health = GetComponent<vHealthController>();
|
||||
|
||||
// Thiết lập Invector Health
|
||||
// RANDOM TÍNH CÁCH
|
||||
personalApproachWeight = Random.Range(0.2f, 0.7f);
|
||||
personalMinCombatDistance = Random.Range(3f, 8f);
|
||||
personalBurstMax = Random.Range(2, 5);
|
||||
personalStrafeIntensity = Random.Range(0.5f, 1.5f);
|
||||
|
||||
health.onReceiveDamage.AddListener(OnReceiveDamage);
|
||||
health.onDead.AddListener(OnDead);
|
||||
|
||||
// Tự động gán Layer Enemy
|
||||
if (gameObject.layer == LayerMask.NameToLayer("Default"))
|
||||
gameObject.layer = LayerMask.NameToLayer("Enemy");
|
||||
{
|
||||
int enemyLayer = LayerMask.NameToLayer("Enemy");
|
||||
if (enemyLayer != -1)
|
||||
{
|
||||
gameObject.layer = enemyLayer;
|
||||
Debug.Log($"[AI {npcName}] Đã chuyển sang Layer: Enemy");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[AI {npcName}] CẢNH BÁO: Không tìm thấy Layer 'Enemy' trong Project! Hãy tạo Layer 'Enemy' để súng có thể bắn trúng.");
|
||||
}
|
||||
}
|
||||
|
||||
rb.isKinematic = true;
|
||||
rb.freezeRotation = true;
|
||||
@@ -127,16 +155,9 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
void InitTree()
|
||||
{
|
||||
// 1. Phản xạ tức thì (Dodge)
|
||||
var dodgeSequence = new Sequence(new List<Node> { new TaskNode(CheckDodgeConditions), new TaskNode(ActionDodge) });
|
||||
|
||||
// 2. Hoảng loạn (Panic)
|
||||
var panicSequence = new Sequence(new List<Node> { new TaskNode(CheckPanicConditions), new TaskNode(ActionPanic) });
|
||||
|
||||
// 3. Tấn công (Laser)
|
||||
var panicSequence = new Sequence(new List<Node> { new TaskNode(CheckPanicConditions), new TaskNode(ActionRetreat) }); // Thay Panic bằng Retreat
|
||||
var laserSequence = new Sequence(new List<Node> { new TaskNode(CheckCombatConditions), new TaskNode(ActionFocusAndShoot) });
|
||||
|
||||
// 4. Các hành vi khác
|
||||
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) });
|
||||
@@ -158,13 +179,11 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
if (player == null || health.isDead) return;
|
||||
|
||||
// Đảm bảo Collider luôn bật
|
||||
if (mainCollider != null && !mainCollider.enabled) mainCollider.enabled = true;
|
||||
|
||||
HandleHealthRegen();
|
||||
|
||||
suspicionLevel = Mathf.Max(0, suspicionLevel - Time.deltaTime * 0.5f);
|
||||
|
||||
if (suspicionLevel <= 0f && !isEnraged) isAggroedBySound = false;
|
||||
|
||||
if (!isTalking && !isDodging && !isPanicking && agent.isStopped)
|
||||
@@ -177,7 +196,16 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
if (Time.time > lastDamageTime + regenDelay && health.currentHealth < health.maxHealth)
|
||||
{
|
||||
health.AddHealth((int)(regenRate * Time.deltaTime));
|
||||
float currentRegenSpeed = regenRate;
|
||||
float healthPercent = (float)health.currentHealth / health.maxHealth;
|
||||
|
||||
// Tăng tốc hồi máu khi máu cực thấp (< 25%)
|
||||
if (healthPercent < 0.25f)
|
||||
currentRegenSpeed *= 4f;
|
||||
else if (healthPercent < 0.5f)
|
||||
currentRegenSpeed *= 2f;
|
||||
|
||||
health.AddHealth((int)(currentRegenSpeed * Time.deltaTime));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,17 +218,20 @@ public class EnemyAI : MonoBehaviour
|
||||
suspicionLevel = 100f;
|
||||
StopConversation();
|
||||
|
||||
// Gọi hội ngay khi bị bắn
|
||||
// PHẢN ỨNG TỨC THÌ: Alert toàn bộ lân cận
|
||||
AlertNeighbors(damage.hitPosition);
|
||||
|
||||
// Né đòn ngay lập tức khi bị trúng dame (phản xạ Elden Ring)
|
||||
if (Time.time > nextDodgeTime && !isDodging)
|
||||
// PHẢN ỨNG TỨC THÌ: Reset delay bắn để phản công nhanh hoặc né
|
||||
nextShootTime = Time.time + 0.5f;
|
||||
|
||||
// Né đòn Elden Ring (Tăng tỉ lệ né khi trúng dame)
|
||||
if (Time.time > nextDodgeTime && !isDodging && Random.value < 0.7f)
|
||||
{
|
||||
StartCoroutine(DodgeRollRoutine());
|
||||
}
|
||||
|
||||
// Kiểm tra Enrage (Phase 2)
|
||||
if (health.currentHealth < health.maxHealth * 0.5f && !isEnraged)
|
||||
// Tự động Enrage nếu bị dồn vào đường cùng (nhưng đồng thời vẫn có thể bỏ chạy nếu không phẫn nộ)
|
||||
if (health.currentHealth < health.maxHealth * 0.2f && !isEnraged)
|
||||
{
|
||||
EnterEnrageMode();
|
||||
}
|
||||
@@ -209,15 +240,22 @@ public class EnemyAI : MonoBehaviour
|
||||
private void OnDead(GameObject killer)
|
||||
{
|
||||
Debug.Log($"<color=black>[AI {npcName}] DIED.</color>");
|
||||
// Khi chết, làm những con xung quanh Enraged gắt hơn
|
||||
|
||||
// 1. Vô hiệu hóa va chạm và di chuyển ngay lập tức
|
||||
if (mainCollider != null) mainCollider.enabled = false;
|
||||
agent.enabled = false;
|
||||
|
||||
// 2. Kích hoạt Enrage cho đồng đội xung quanh
|
||||
Collider[] hitColliders = Physics.OverlapSphere(transform.position, alertRange);
|
||||
foreach (var hit in hitColliders)
|
||||
{
|
||||
EnemyAI ally = hit.GetComponentInParent<EnemyAI>();
|
||||
if (ally != null && ally != this) ally.EnterEnrageMode();
|
||||
}
|
||||
|
||||
agent.enabled = false;
|
||||
|
||||
// 3. Tự hủy sau 3 giây (để kịp chạy animation chết hoặc hiệu ứng)
|
||||
Destroy(gameObject, 3f);
|
||||
|
||||
this.enabled = false;
|
||||
}
|
||||
|
||||
@@ -225,18 +263,13 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
if (isEnraged) return;
|
||||
isEnraged = true;
|
||||
isPanicking = false; // Hết sợ, chuyển sang liều mạng
|
||||
isPanicking = false;
|
||||
|
||||
// Buff stats mạnh mẽ như Elden Ring boss phase 2
|
||||
moveSpeed *= 1.5f;
|
||||
minShootDelay *= 0.5f;
|
||||
maxShootDelay *= 0.5f;
|
||||
maxSpreadAngle *= 0.6f; // Bắn chính xác hơn
|
||||
approachWeight = 0.8f; // Áp sát cực gắt
|
||||
minCombatDistance = 2.0f; // Đứng sát mặt Player để bắn
|
||||
moveSpeed *= 1.3f; // Giảm nhẹ buff speed
|
||||
minShootDelay *= 0.6f;
|
||||
maxShootDelay *= 0.6f;
|
||||
|
||||
if (chatBubble != null) chatBubble.Show("FOR THE BROTHERHOOD! DIE!", 3f);
|
||||
Debug.Log($"<color=red>[AI {npcName}] ENRAGED!</color> Stats boosted.");
|
||||
if (chatBubble != null) chatBubble.Show("I'LL TAKE YOU DOWN WITH ME!", 2f);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -246,7 +279,7 @@ public class EnemyAI : MonoBehaviour
|
||||
private NodeState CheckDodgeConditions()
|
||||
{
|
||||
if (isDodging) return NodeState.Success;
|
||||
// Tự né khi thấy Player click chuột trái (đang bắn)
|
||||
// Tự né khi thấy Player đang bắn
|
||||
if (fov != null && fov.canSeePlayer && Mouse.current.leftButton.isPressed && Time.time > nextDodgeTime)
|
||||
return NodeState.Success;
|
||||
return NodeState.Failure;
|
||||
@@ -254,11 +287,15 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
private NodeState CheckPanicConditions()
|
||||
{
|
||||
if (isEnraged) return NodeState.Failure; // Đang điên thì không sợ
|
||||
if (isEnraged) return NodeState.Failure; // Đang điên thì không sợ, chiến đến chết
|
||||
|
||||
// Nếu máu dưới ngưỡng thiết lập (ví dụ 50%), kích hoạt trạng thái tháo chạy
|
||||
if (health.currentHealth < (health.maxHealth * (panicHealthThreshold / 100f)))
|
||||
{
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
isPanicking = false;
|
||||
return NodeState.Failure;
|
||||
}
|
||||
|
||||
@@ -312,7 +349,7 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
public void HearNoise(Vector3 location, float volume)
|
||||
{
|
||||
suspicionLevel += volume * 15f;
|
||||
suspicionLevel += volume * 20f;
|
||||
if (fov != null) fov.lastKnownPlayerPosition = location;
|
||||
if (suspicionLevel >= investigationThreshold) isAggroedBySound = true;
|
||||
if (suspicionLevel >= alertNeighborsThreshold) AlertNeighbors(location);
|
||||
@@ -341,24 +378,35 @@ public class EnemyAI : MonoBehaviour
|
||||
StopConversation();
|
||||
}
|
||||
|
||||
private NodeState ActionPanic()
|
||||
// Hành động tháo chạy khi máu thấp
|
||||
private NodeState ActionRetreat()
|
||||
{
|
||||
isPanicking = true;
|
||||
agent.speed = moveSpeed * 1.3f;
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed * 1.3f; // Chạy nhanh hơn bình thường để thoát thân
|
||||
|
||||
// Tính toán hướng ngược lại với Player
|
||||
Vector3 retreatDir = (transform.position - player.position).normalized;
|
||||
Vector3 targetPos = transform.position + retreatDir * 15f + Random.insideUnitSphere * 5f;
|
||||
|
||||
// Chạy trốn khỏi Player đến một điểm ngẫu nhiên xa Player
|
||||
if (!agent.pathPending && agent.remainingDistance < 1f)
|
||||
{
|
||||
Vector3 runDir = (transform.position - player.position).normalized;
|
||||
Vector3 escapePoint = transform.position + runDir * 10f + Random.insideUnitSphere * 5f;
|
||||
|
||||
NavMeshHit hit;
|
||||
if (NavMesh.SamplePosition(escapePoint, out hit, 10f, 1))
|
||||
if (NavMesh.SamplePosition(targetPos, out hit, 10f, 1))
|
||||
{
|
||||
agent.SetDestination(hit.position);
|
||||
}
|
||||
}
|
||||
|
||||
if (chatBubble != null && Random.value < 0.005f) chatBubble.Show("I NEED TO RECOVER!", 1.5f);
|
||||
|
||||
// Khi máu đã hồi phục trên 50%, dừng chạy và quay lại tấn công
|
||||
if (health.currentHealth >= health.maxHealth * 0.5f)
|
||||
{
|
||||
isPanicking = false;
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
if (chatBubble != null && Random.value < 0.01f) chatBubble.Show("HELP! HE'S KILLING US!", 1f);
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
@@ -428,13 +476,11 @@ public class EnemyAI : MonoBehaviour
|
||||
if (!playerHasArtifact && fov != null && !fov.canSeePlayer && fov.lastKnownPlayerPosition != Vector3.zero)
|
||||
targetPos = fov.lastKnownPlayerPosition;
|
||||
|
||||
// Xoay về phía mục tiêu
|
||||
Vector3 bodyDir = (targetPos - transform.position);
|
||||
bodyDir.y = 0f;
|
||||
if (bodyDir != Vector3.zero)
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(bodyDir), rotateSpeed * Time.deltaTime);
|
||||
|
||||
// Strafe di chuyển linh hoạt
|
||||
if (Time.time >= nextStrafeChangeTime)
|
||||
{
|
||||
strafeDirectionSign = new int[] { -1, 1, 0 }[Random.Range(0, 3)];
|
||||
@@ -445,8 +491,10 @@ public class EnemyAI : MonoBehaviour
|
||||
if (strafeDirectionSign != 0 && bodyDir != Vector3.zero)
|
||||
{
|
||||
Vector3 normal = bodyDir.normalized;
|
||||
moveDir = new Vector3(-normal.z, 0, normal.x) * strafeDirectionSign;
|
||||
if (Vector3.Distance(transform.position, targetPos) > minCombatDistance) moveDir += normal * approachWeight;
|
||||
moveDir = new Vector3(-normal.z, 0, normal.x) * strafeDirectionSign * personalStrafeIntensity;
|
||||
|
||||
float dist = Vector3.Distance(transform.position, targetPos);
|
||||
if (dist > personalMinCombatDistance) moveDir += normal * personalApproachWeight;
|
||||
}
|
||||
|
||||
if (moveDir != Vector3.zero)
|
||||
@@ -460,7 +508,8 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
if (Time.time >= nextShootTime && !isShootingBurst)
|
||||
{
|
||||
StartCoroutine(ShootBurstRoutine(Random.Range(1, isEnraged ? 6 : 4)));
|
||||
int burstCount = Random.Range(1, (int)personalBurstMax + (isEnraged ? 2 : 0));
|
||||
StartCoroutine(ShootBurstRoutine(burstCount));
|
||||
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
||||
}
|
||||
|
||||
@@ -496,13 +545,13 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
Vector3 dir = (player.position - transform.position).normalized;
|
||||
Vector3 perp = new Vector3(-dir.z, 0, dir.x) * (Random.value > 0.5f ? 1 : -1);
|
||||
rb.AddForce(perp * (isEnraged ? dodgeForce * 1.5f : dodgeForce), ForceMode.Impulse);
|
||||
rb.AddForce(perp * (isEnraged ? dodgeForce * 1.3f : dodgeForce), ForceMode.Impulse);
|
||||
|
||||
yield return new WaitForSeconds(dodgeDuration);
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.isKinematic = true;
|
||||
agent.enabled = true;
|
||||
nextDodgeTime = Time.time + (isEnraged ? dodgeCooldown * 0.5f : dodgeCooldown);
|
||||
nextDodgeTime = Time.time + (isEnraged ? dodgeCooldown * 0.6f : dodgeCooldown);
|
||||
isDodging = false;
|
||||
}
|
||||
|
||||
@@ -513,9 +562,25 @@ public class EnemyAI : MonoBehaviour
|
||||
DialogueResult result = JsonUtility.FromJson<DialogueResult>(json);
|
||||
if (chatBubble != null) chatBubble.Show(result.text);
|
||||
|
||||
moveSpeed += result.speedMod;
|
||||
// Áp dụng modifiers từ Gemini
|
||||
moveSpeed = Mathf.Clamp(moveSpeed + result.speedMod, 1f, 8f);
|
||||
suspicionLevel = Mathf.Clamp(suspicionLevel + result.suspicionMod, 0, 100);
|
||||
|
||||
// Modifier hung hãn: giảm delay bắn (max 50%)
|
||||
minShootDelay = Mathf.Clamp(minShootDelay * (1f - result.aggressionMod), 0.5f, 5f);
|
||||
maxShootDelay = Mathf.Clamp(maxShootDelay * (1f - result.aggressionMod), 1f, 10f);
|
||||
|
||||
// Modifier can đảm: thay đổi ngưỡng panic
|
||||
panicHealthThreshold = Mathf.Clamp(panicHealthThreshold - result.braveryMod, 0, 80);
|
||||
|
||||
// Modifier máu
|
||||
if (result.healthMod != 0)
|
||||
{
|
||||
health.AddHealth((int)result.healthMod);
|
||||
}
|
||||
|
||||
lastTalkTime = Time.time;
|
||||
Debug.Log($"[AI {npcName}] Gemini influence: Speed:{result.speedMod}, Aggro:{result.aggressionMod}, Bravery:{result.braveryMod}");
|
||||
}
|
||||
catch { if (chatBubble != null) chatBubble.Show(json); }
|
||||
}
|
||||
|
||||
@@ -31,10 +31,10 @@ namespace Hallucinate.AI
|
||||
private float nextRequestTime = 0f;
|
||||
|
||||
private string[] fallbackDialogues = {
|
||||
"{ \"text\": \"Nice weather, isn't it?\", \"speedMod\": 0.0, \"suspicionMod\": -5.0 }",
|
||||
"{ \"text\": \"Did you hear something? Probably just a rat.\", \"speedMod\": 0.0, \"suspicionMod\": 2.0 }",
|
||||
"{ \"text\": \"I'm so tired of this shift.\", \"speedMod\": -0.1, \"suspicionMod\": 0.0 }",
|
||||
"{ \"text\": \"Don't forget the coffee break later.\", \"speedMod\": 0.0, \"suspicionMod\": -2.0 }"
|
||||
"{ \"text\": \"Nice weather, isn't it?\", \"speedMod\": 0.0, \"suspicionMod\": -5.0, \"aggressionMod\": 0.0, \"braveryMod\": 0.0, \"healthMod\": 0.0 }",
|
||||
"{ \"text\": \"Did you hear something? Probably just a rat.\", \"speedMod\": 0.0, \"suspicionMod\": 2.0, \"aggressionMod\": 0.1, \"braveryMod\": -5.0, \"healthMod\": 0.0 }",
|
||||
"{ \"text\": \"I'm so tired of this shift.\", \"speedMod\": -0.2, \"suspicionMod\": 0.0, \"aggressionMod\": -0.1, \"braveryMod\": 5.0, \"healthMod\": 0.0 }",
|
||||
"{ \"text\": \"You looks strong, I should be careful.\", \"speedMod\": 0.1, \"suspicionMod\": 5.0, \"aggressionMod\": -0.2, \"braveryMod\": 10.0, \"healthMod\": 0.0 }"
|
||||
};
|
||||
|
||||
private void Awake()
|
||||
@@ -72,7 +72,15 @@ namespace Hallucinate.AI
|
||||
{
|
||||
activeRequests++;
|
||||
|
||||
string jsonInstruction = " Respond ONLY with a JSON object: { 'text': 'your dialogue', 'speedMod': 0.0, 'suspicionMod': 0.0 }.";
|
||||
string jsonInstruction = " Respond ONLY with a JSON object: { " +
|
||||
"'text': 'dialogue content', " +
|
||||
"'speedMod': 0.0 (change movement speed), " +
|
||||
"'suspicionMod': 0.0 (change suspicion level), " +
|
||||
"'aggressionMod': 0.0 (0.1 to 0.5 makes NPC shoot faster, negative makes them slower), " +
|
||||
"'braveryMod': 0.0 (positive makes them less likely to panic, negative makes them scared), " +
|
||||
"'healthMod': 0.0 (positive heals NPC, negative damages them) " +
|
||||
"}. Keep values realistic.";
|
||||
|
||||
string escapedPersona = persona.Replace("\"", "\\\"");
|
||||
string escapedPrompt = prompt.Replace("\"", "\\\"");
|
||||
|
||||
@@ -80,8 +88,8 @@ namespace Hallucinate.AI
|
||||
""systemInstruction"": {{""parts"": [{{ ""text"": ""{escapedPersona} {jsonInstruction}"" }}]}},
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""{escapedPrompt}"" }}]}}],
|
||||
""generationConfig"": {{
|
||||
""maxOutputTokens"": 100,
|
||||
""temperature"": 0.7,
|
||||
""maxOutputTokens"": 150,
|
||||
""temperature"": 0.8,
|
||||
""responseMimeType"": ""application/json""
|
||||
}}
|
||||
}}";
|
||||
@@ -110,8 +118,7 @@ namespace Hallucinate.AI
|
||||
Debug.LogError($"[Gemini] API Error: {request.error}");
|
||||
if (request.responseCode == 429)
|
||||
{
|
||||
nextRequestTime = Time.time + 60f; // Lock API for 1 minute
|
||||
Debug.LogWarning("Quota Exceeded. API locked for 60s. Using fallback.");
|
||||
nextRequestTime = Time.time + 60f;
|
||||
}
|
||||
onComplete?.Invoke(fallbackDialogues[UnityEngine.Random.Range(0, fallbackDialogues.Length)]);
|
||||
}
|
||||
|
||||
@@ -130,9 +130,11 @@ namespace Invector.vShooter
|
||||
}
|
||||
|
||||
var rigb = hitInfo.collider.gameObject.GetComponent<Rigidbody>();
|
||||
if (rigb)
|
||||
if (rigb && !rigb.isKinematic)
|
||||
{
|
||||
rigb.AddForce(transform.forward * damage.damageValue * forceMultiplier, ForceMode.Impulse);
|
||||
// GIẢM LỰC ĐẨY: Chỉ dùng 10% lực để NPC không bị bay quá xa
|
||||
float realisticForce = (damage.damageValue * forceMultiplier) * 0.1f;
|
||||
rigb.AddForce(transform.forward * realisticForce, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
startPosition = transform.position;
|
||||
@@ -292,8 +294,20 @@ namespace Invector.vShooter
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
// DIAGNOSTIC X-RAY: Kiểm tra xem có trúng cái gì mà bị HitLayer cấm không?
|
||||
RaycastHit diagHit;
|
||||
if (Physics.Linecast(previousPosition, transform.position + transform.forward * 0.5f, out diagHit, ~0)) // ~0 là tất cả layer
|
||||
{
|
||||
if (diagHit.collider.CompareTag("Enemy") || diagHit.collider.name.Contains("Guard") || diagHit.collider.GetComponentInParent<EnemyAI>() != null)
|
||||
{
|
||||
Debug.LogError($"<color=red>LAYER BLOCK DETECTED!</color> Đạn vừa bay xuyên qua {diagHit.collider.name}. " +
|
||||
$"Đối tượng này ở Layer: {LayerMask.LayerToName(diagHit.collider.gameObject.layer)} ({diagHit.collider.gameObject.layer}). " +
|
||||
$"NHƯNG súng của bạn đang dùng HitLayer Mask: {hitLayer.value}, không bao gồm layer này!");
|
||||
}
|
||||
}
|
||||
|
||||
if (debugTrajetory)
|
||||
{
|
||||
Debug.DrawLine(transform.position, previousPosition, debugColor, 10f);
|
||||
@@ -301,7 +315,8 @@ namespace Invector.vShooter
|
||||
}
|
||||
|
||||
previousPosition = transform.position;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -242,6 +242,21 @@ namespace Invector.vShooter
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
protected virtual void Start()
|
||||
{
|
||||
// TỰ ĐỘNG SỬA LỖI LAYER: Đảm bảo súng luôn có thể bắn trúng Enemy
|
||||
int enemyLayer = LayerMask.NameToLayer("Enemy");
|
||||
if (enemyLayer != -1)
|
||||
{
|
||||
// Cưỡng bức thêm Layer Enemy vào mask (Dùng toán tử bit OR)
|
||||
hitLayer.value |= (1 << enemyLayer);
|
||||
Debug.Log($"<color=green>[WEAPON AUTO-FIX]</color> {gameObject.name} hiện đã có thể bắn trúng Layer 'Enemy' (Mask mới: {hitLayer.value}).");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("<color=red>[WEAPON ERROR]</color> Bạn chưa tạo Layer tên là 'Enemy' trong Project Settings > Tags and Layers!");
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
@@ -256,6 +271,14 @@ namespace Invector.vShooter
|
||||
{
|
||||
ShootBullet(startPoint, endPoint);
|
||||
ShotEffect();
|
||||
|
||||
// Gửi tín hiệu tiếng súng cho toàn bộ AI lân cận
|
||||
var hitColliders = Physics.OverlapSphere(muzzle.position, 50f);
|
||||
foreach (var hit in hitColliders)
|
||||
{
|
||||
var ai = hit.GetComponentInParent<EnemyAI>();
|
||||
if (ai != null) ai.TriggerCombatAlert(muzzle.position);
|
||||
}
|
||||
}
|
||||
public virtual Vector3 Dispersion(Vector3 aim, float dispersion)
|
||||
{
|
||||
@@ -297,6 +320,18 @@ namespace Invector.vShooter
|
||||
|
||||
protected virtual void ShootBullet(Vector3 startPoint, Vector3 endPoint)
|
||||
{
|
||||
// TỰ ĐỘNG SỬA LỖI LAYER (Cưỡng bức mỗi khi bắn):
|
||||
int enemyLayer = LayerMask.NameToLayer("Enemy");
|
||||
if (enemyLayer != -1)
|
||||
{
|
||||
// Ép thêm Layer Enemy vào mask nếu nó bị thiếu
|
||||
if ((hitLayer.value & (1 << enemyLayer)) == 0)
|
||||
{
|
||||
hitLayer.value |= (1 << enemyLayer);
|
||||
Debug.Log($"<color=orange>[WEAPON FORCE-FIX]</color> Đã cưỡng bức thêm Layer 'Enemy' vào {gameObject.name} khi bắn.");
|
||||
}
|
||||
}
|
||||
|
||||
var dir = endPoint - startPoint;
|
||||
//StartCoroutine(DebugDispersion(startPoint, endPoint));
|
||||
var rotation = Quaternion.LookRotation(dir);
|
||||
|
||||
Reference in New Issue
Block a user