Update NPC: Nav, find Player

This commit is contained in:
manhduyhoang90
2026-06-04 15:41:01 +07:00
parent 6c8c372f9d
commit f970c78192
6 changed files with 280 additions and 86 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7f8aea511f659ac4dad9a6c8d60dfc59
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -41361,7 +41361,7 @@ PrefabInstance:
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_TransformParent: {fileID: 1408989034}
m_Modifications:
- target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: notificationText
@@ -41397,7 +41397,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.x
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.y
@@ -41444,6 +41444,43 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
--- !u!4 &241771775 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
m_PrefabInstance: {fileID: 241771774}
m_PrefabAsset: {fileID: 0}
--- !u!1 &258369249
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 258369250}
m_Layer: 0
m_Name: _MANAGER
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &258369250
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 258369249}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 10.00109, y: 1.17286, z: 15.33649}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1892936536}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &282447033
GameObject:
m_ObjectHideFlags: 0
@@ -42218,7 +42255,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 333057065}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 3.62463, y: 13.27338, z: -19.50002}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@@ -42268,7 +42305,7 @@ Transform:
- {fileID: 375187854}
- {fileID: 1061793569}
- {fileID: 1398230979}
m_Father: {fileID: 0}
m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &333831099
GameObject:
@@ -42650,12 +42687,12 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 348410791}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 5.7248, y: 1.5, z: -32.50002}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &365978100
GameObject:
@@ -85097,7 +85134,7 @@ PrefabInstance:
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_TransformParent: {fileID: 1408989034}
m_Modifications:
- target: {fileID: 5687887011233860168, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_Layer
@@ -85121,15 +85158,15 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalRotation.x
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalRotation.y
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalRotation.z
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
@@ -85159,6 +85196,11 @@ PrefabInstance:
insertIndex: -1
addedObject: {fileID: 1665376262}
m_SourcePrefab: {fileID: 100100000, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
--- !u!4 &572767393 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
m_PrefabInstance: {fileID: 572767392}
m_PrefabAsset: {fileID: 0}
--- !u!43 &593490377
Mesh:
m_ObjectHideFlags: 0
@@ -85343,7 +85385,7 @@ PrefabInstance:
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_TransformParent: {fileID: 1408989034}
m_Modifications:
- target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: notificationText
@@ -85379,7 +85421,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.x
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.y
@@ -85426,6 +85468,11 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
--- !u!4 &628283018 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
m_PrefabInstance: {fileID: 628283017}
m_PrefabAsset: {fileID: 0}
--- !u!43 &631793619
Mesh:
m_ObjectHideFlags: 0
@@ -88453,7 +88500,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 90, y: -90, z: 0}
--- !u!114 &678938551
MonoBehaviour:
@@ -102350,7 +102397,7 @@ RectTransform:
m_Children:
- {fileID: 1007024365}
- {fileID: 292895766}
m_Father: {fileID: 0}
m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
@@ -128666,12 +128713,12 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1098531124}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -16.49995, y: 10.56881, z: 19.27294}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1143870345
GameObject:
@@ -135446,6 +135493,47 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1398230978}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &1408989033
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1408989034}
m_Layer: 0
m_Name: _SCENE
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1408989034
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1408989033}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 678938550}
- {fileID: 333057066}
- {fileID: 1098531126}
- {fileID: 348410792}
- {fileID: 963322862}
- {fileID: 1793734345}
- {fileID: 628283018}
- {fileID: 241771775}
- {fileID: 1813306720}
- {fileID: 572767393}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1453277286
GameObject:
m_ObjectHideFlags: 0
@@ -141009,12 +141097,12 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1793734342}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1813306719
PrefabInstance:
@@ -141022,7 +141110,7 @@ PrefabInstance:
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_TransformParent: {fileID: 1408989034}
m_Modifications:
- target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: notificationText
@@ -141058,7 +141146,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.x
value: 0
value: -0
objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.y
@@ -141105,6 +141193,11 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
--- !u!4 &1813306720 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
m_PrefabInstance: {fileID: 1813306719}
m_PrefabAsset: {fileID: 0}
--- !u!43 &1820026756
Mesh:
m_ObjectHideFlags: 0
@@ -141780,6 +141873,70 @@ Transform:
m_Children: []
m_Father: {fileID: 333057066}
m_LocalEulerAnglesHint: {x: 0, y: 45, z: 0}
--- !u!1 &1892936535
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1892936536}
- component: {fileID: 1892936537}
m_Layer: 0
m_Name: NavManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1892936536
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1892936535}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 258369250}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1892936537
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1892936535}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a5ac11cc976e418e8d13136b07e1f52, type: 3}
m_Name:
m_EditorClassIdentifier: Unity.AI.Navigation::Unity.AI.Navigation.NavMeshSurface
m_SerializedVersion: 0
m_AgentTypeID: 0
m_CollectObjects: 0
m_Size: {x: 10, y: 10, z: 10}
m_Center: {x: 0, y: 2, z: 0}
m_LayerMask:
serializedVersion: 2
m_Bits: 4294967295
m_UseGeometry: 0
m_DefaultArea: 0
m_GenerateLinks: 0
m_IgnoreNavMeshAgent: 1
m_IgnoreNavMeshObstacle: 1
m_OverrideTileSize: 0
m_TileSize: 256
m_OverrideVoxelSize: 0
m_VoxelSize: 0.16666667
m_MinRegionArea: 2
m_NavMeshData: {fileID: 23800000, guid: 8204ba29e85cfee47bf2cdd081e429f4, type: 2}
m_BuildHeightMesh: 0
--- !u!43 &1951987860
Mesh:
m_ObjectHideFlags: 0
@@ -144775,13 +144932,5 @@ Transform:
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 678938550}
- {fileID: 333057066}
- {fileID: 1098531126}
- {fileID: 348410792}
- {fileID: 963322862}
- {fileID: 1793734345}
- {fileID: 628283017}
- {fileID: 241771774}
- {fileID: 1813306719}
- {fileID: 572767392}
- {fileID: 1408989034}
- {fileID: 258369250}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8204ba29e85cfee47bf2cdd081e429f4
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 23800000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; // Cần thiết để dùng NavMesh
[RequireComponent(typeof(NavMeshAgent))] // Tự động thêm component này nếu chưa có
public class EnemyAI : MonoBehaviour
{
[Header("References")]
@@ -11,6 +13,12 @@ public class EnemyAI : MonoBehaviour
public float moveSpeed = 3f;
public float rotateSpeed = 50f;
[Header("Patrol Area")]
public float patrolRadius = 15f; // Bán kính khu vực tuần tra
public float patrolWaitTime = 2f; // Thời gian đứng chờ trước khi đi điểm khác
private Vector3 startPosition;
private float currentWaitTime;
[Header("Artifact")]
public bool playerHasArtifact;
@@ -21,23 +29,44 @@ public class EnemyAI : MonoBehaviour
public float maxShootDelay = 3f;
private float nextShootTime;
private NavMeshAgent agent;
public Node behaviorTreeRoot;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
agent = GetComponent<NavMeshAgent>();
agent.speed = moveSpeed;
// Lưu lại vị trí ban đầu để làm tâm của khu vực tuần tra
startPosition = transform.position;
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
InitBehaviorTree();
FindPlayer();
}
private void Update()
{
// Nếu mất reference (Player chết hoặc chưa spawn), liên tục tìm lại
if (player == null)
{
FindPlayer();
}
// Chỉ chạy AI nếu đã tìm thấy player (hoặc bạn có thể cho tuần tra ngay cả khi chưa có player tùy logic game)
behaviorTreeRoot?.Evaluate();
}
private void FindPlayer()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null)
{
player = playerObj.transform;
}
}
private void InitBehaviorTree()
{
// Player có artifact -> focus + shoot
@@ -54,14 +83,14 @@ public class EnemyAI : MonoBehaviour
new TaskNode(ActionMoveToPlayer)
});
// Không thấy ai -> scan
var scanNode = new TaskNode(ActionRotationScan);
// Không thấy ai -> Tuần tra bằng NavMesh
var patrolNode = new TaskNode(ActionPatrol);
behaviorTreeRoot = new Selector(new List<Node>
{
laserSequence,
chaseSequence,
scanNode
patrolNode
});
}
@@ -69,18 +98,14 @@ public class EnemyAI : MonoBehaviour
private NodeState CheckHasArtifact()
{
return playerHasArtifact
? NodeState.Success
: NodeState.Failure;
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
}
private NodeState CheckCanSeePlayer()
{
if (player == null)
return NodeState.Failure;
if (player == null) return NodeState.Failure;
float distance =
Vector3.Distance(transform.position, player.position);
float distance = Vector3.Distance(transform.position, player.position);
if (distance <= detectRange)
{
@@ -94,66 +119,74 @@ public class EnemyAI : MonoBehaviour
#region ACTIONS
private NodeState ActionRotationScan()
private NodeState ActionPatrol()
{
Debug.Log("Scanning...");
Debug.Log("Patrolling...");
agent.isStopped = false; // Đảm bảo NPC được phép di chuyển
agent.speed = moveSpeed * 0.5f; // Đi dạo nên đi chậm lại một chút
transform.Rotate(
Vector3.up,
rotateSpeed * Time.deltaTime);
// Kiểm tra xem NPC đã đến điểm đích chưa
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
{
currentWaitTime += Time.deltaTime;
// Chờ một lúc rồi mới chọn điểm mới
if (currentWaitTime >= patrolWaitTime)
{
// Tìm một điểm ngẫu nhiên trong bán kính cho trước
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
randomDirection += startPosition;
NavMeshHit hit;
// Đảm bảo điểm ngẫu nhiên nằm trên bề mặt NavMesh hợp lệ
if (NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1))
{
agent.SetDestination(hit.position);
}
currentWaitTime = 0f;
}
}
return NodeState.Running;
}
private NodeState ActionMoveToPlayer()
{
if (player == null)
return NodeState.Failure;
if (player == null) return NodeState.Failure;
Debug.Log("Chasing Player");
Vector3 dir =
(player.position - transform.position).normalized;
transform.position +=
dir *
moveSpeed *
Time.deltaTime;
agent.isStopped = false;
agent.speed = moveSpeed; // Phục hồi tốc độ rượt đuổi
agent.SetDestination(player.position);
return NodeState.Running;
}
private NodeState ActionFocusAndShoot()
{
if (player == null)
return NodeState.Failure;
if (player == null) return NodeState.Failure;
Debug.Log("Focus and Shoot!");
// Dừng NavMeshAgent lại để đứng bắn, tránh bị trượt
agent.isStopped = true;
// Focus player
Vector3 dir =
player.position - transform.position;
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,
5f * Time.deltaTime);
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
}
// Shoot with random delay
if (Time.time >= nextShootTime)
{
ShootLaser();
nextShootTime =
Time.time +
Random.Range(minShootDelay, maxShootDelay);
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
return NodeState.Running;
@@ -161,14 +194,8 @@ public class EnemyAI : MonoBehaviour
private void ShootLaser()
{
if (laserPrefab == null || firePoint == null)
return;
Instantiate(
laserPrefab,
firePoint.position,
firePoint.rotation);
if (laserPrefab == null || firePoint == null) return;
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
Debug.Log("Laser Shot!");
}

View File

@@ -69,9 +69,9 @@ NavMeshProjectSettings:
cost: 1
- name:
cost: 1
m_LastAgentTypeID: -887442657
m_LastAgentTypeID: -334000983
m_Settings:
- serializedVersion: 2
- serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
@@ -84,7 +84,9 @@ NavMeshProjectSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_SettingNames: