Update AI

This commit is contained in:
manhduyhoang90
2026-06-05 21:43:41 +07:00
parent 98806b862d
commit c03f78b557

View File

@@ -314,16 +314,48 @@ public class EnemyAI : MonoBehaviour
private NodeState ActionFocusAndShoot()
{
agent.isStopped = true;
FaceTarget(player.position);
if (Time.time >= nextShootTime)
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)
{
if (laserPrefab && firePoint) Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
return NodeState.Running;
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)
{
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($"<color=red>[AI {npcName}]</color> Fired chéo Laser downwards/upwards at Player!");
}
private NodeState ActionDodge()
{
if (!isDodging) StartCoroutine(DodgeRollRoutine());