diff --git a/Assets/Scripts/AI NPC/EnemyAI.cs b/Assets/Scripts/AI NPC/EnemyAI.cs index 48dc6ab6..2b53ca7c 100644 --- a/Assets/Scripts/AI NPC/EnemyAI.cs +++ b/Assets/Scripts/AI NPC/EnemyAI.cs @@ -314,16 +314,48 @@ public class EnemyAI : MonoBehaviour private NodeState ActionFocusAndShoot() { - agent.isStopped = true; - FaceTarget(player.position); + if (player == null) return NodeState.Failure; + + agent.isStopped = true; // Đứng im bắn cố định khi có cổ vật + + // 1. XOAY THÂN THEO TRỤC NGANG (Giúp NPC luôn đứng thẳng lưng, không bị đổ người) + Vector3 bodyDir = player.position - transform.position; + bodyDir.y = 0f; // Khóa trục dọc của thân + if (bodyDir != Vector3.zero) + { + Quaternion targetRotation = Quaternion.LookRotation(bodyDir); + transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime); + } + + // 2. XOAY HỌNG SÚNG THEO TRỤC DỌC (Chúi xuống hoặc ngước lên thẳng vào Player) + if (firePoint != null) + { + // Cộng thêm Vector3.up * 1f để họng súng nhắm vào NGỰC/BỤNG player, không bị bắn chúi xuống đất (bắn vào chân) + Vector3 targetCenter = player.position + Vector3.up * 1f; + Vector3 aimDir = targetCenter - firePoint.position; + + if (aimDir != Vector3.zero) + { + // Họng súng nhìn thẳng hoàn toàn vào Player (bao gồm cả trục Y chéo xuống) + firePoint.rotation = Quaternion.LookRotation(aimDir); + } + } + + // 3. BẮN LASER (Laser sinh ra sẽ tự động lấy rotation chéo của firePoint) if (Time.time >= nextShootTime) { - if (laserPrefab && firePoint) Instantiate(laserPrefab, firePoint.position, firePoint.rotation); + ShootLaser(); nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay); } + return NodeState.Running; } - + private void ShootLaser() + { + if (laserPrefab == null || firePoint == null) return; + Instantiate(laserPrefab, firePoint.position, firePoint.rotation); + Debug.Log($"[AI {npcName}] Fired chéo Laser downwards/upwards at Player!"); + } private NodeState ActionDodge() { if (!isDodging) StartCoroutine(DodgeRollRoutine());