Update AI
This commit is contained in:
@@ -314,16 +314,48 @@ public class EnemyAI : MonoBehaviour
|
|||||||
|
|
||||||
private NodeState ActionFocusAndShoot()
|
private NodeState ActionFocusAndShoot()
|
||||||
{
|
{
|
||||||
agent.isStopped = true;
|
if (player == null) return NodeState.Failure;
|
||||||
FaceTarget(player.position);
|
|
||||||
|
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 (Time.time >= nextShootTime)
|
||||||
{
|
{
|
||||||
if (laserPrefab && firePoint) Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
|
ShootLaser();
|
||||||
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NodeState.Running;
|
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()
|
private NodeState ActionDodge()
|
||||||
{
|
{
|
||||||
if (!isDodging) StartCoroutine(DodgeRollRoutine());
|
if (!isDodging) StartCoroutine(DodgeRollRoutine());
|
||||||
|
|||||||
Reference in New Issue
Block a user