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 serializedVersion: 2
m_Modification: m_Modification:
serializedVersion: 3 serializedVersion: 3
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 1408989034}
m_Modifications: m_Modifications:
- target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: notificationText propertyPath: notificationText
@@ -41397,7 +41397,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.x propertyPath: m_LocalRotation.x
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.y propertyPath: m_LocalRotation.y
@@ -41444,6 +41444,43 @@ PrefabInstance:
m_AddedGameObjects: [] m_AddedGameObjects: []
m_AddedComponents: [] m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} 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 --- !u!1 &282447033
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -42218,7 +42255,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 333057065} m_GameObject: {fileID: 333057065}
serializedVersion: 2 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_LocalPosition: {x: 3.62463, y: 13.27338, z: -19.50002}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
@@ -42268,7 +42305,7 @@ Transform:
- {fileID: 375187854} - {fileID: 375187854}
- {fileID: 1061793569} - {fileID: 1061793569}
- {fileID: 1398230979} - {fileID: 1398230979}
m_Father: {fileID: 0} m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &333831099 --- !u!1 &333831099
GameObject: GameObject:
@@ -42650,12 +42687,12 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 348410791} m_GameObject: {fileID: 348410791}
serializedVersion: 2 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_LocalPosition: {x: 5.7248, y: 1.5, z: -32.50002}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &365978100 --- !u!1 &365978100
GameObject: GameObject:
@@ -85097,7 +85134,7 @@ PrefabInstance:
serializedVersion: 2 serializedVersion: 2
m_Modification: m_Modification:
serializedVersion: 3 serializedVersion: 3
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 1408989034}
m_Modifications: m_Modifications:
- target: {fileID: 5687887011233860168, guid: 15df559ce497e104a81254e0adf3107e, type: 3} - target: {fileID: 5687887011233860168, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_Layer propertyPath: m_Layer
@@ -85121,15 +85158,15 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3} - target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalRotation.x propertyPath: m_LocalRotation.x
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3} - target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalRotation.y propertyPath: m_LocalRotation.y
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3} - target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalRotation.z propertyPath: m_LocalRotation.z
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3} - target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
propertyPath: m_LocalEulerAnglesHint.x propertyPath: m_LocalEulerAnglesHint.x
@@ -85159,6 +85196,11 @@ PrefabInstance:
insertIndex: -1 insertIndex: -1
addedObject: {fileID: 1665376262} addedObject: {fileID: 1665376262}
m_SourcePrefab: {fileID: 100100000, guid: 15df559ce497e104a81254e0adf3107e, type: 3} 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 --- !u!43 &593490377
Mesh: Mesh:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -85343,7 +85385,7 @@ PrefabInstance:
serializedVersion: 2 serializedVersion: 2
m_Modification: m_Modification:
serializedVersion: 3 serializedVersion: 3
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 1408989034}
m_Modifications: m_Modifications:
- target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: notificationText propertyPath: notificationText
@@ -85379,7 +85421,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.x propertyPath: m_LocalRotation.x
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.y propertyPath: m_LocalRotation.y
@@ -85426,6 +85468,11 @@ PrefabInstance:
m_AddedGameObjects: [] m_AddedGameObjects: []
m_AddedComponents: [] m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} 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 --- !u!43 &631793619
Mesh: Mesh:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -88453,7 +88500,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 90, y: -90, z: 0} m_LocalEulerAnglesHint: {x: 90, y: -90, z: 0}
--- !u!114 &678938551 --- !u!114 &678938551
MonoBehaviour: MonoBehaviour:
@@ -102350,7 +102397,7 @@ RectTransform:
m_Children: m_Children:
- {fileID: 1007024365} - {fileID: 1007024365}
- {fileID: 292895766} - {fileID: 292895766}
m_Father: {fileID: 0} m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0}
@@ -128666,12 +128713,12 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1098531124} m_GameObject: {fileID: 1098531124}
serializedVersion: 2 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_LocalPosition: {x: -16.49995, y: 10.56881, z: 19.27294}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1143870345 --- !u!1 &1143870345
GameObject: GameObject:
@@ -135446,6 +135493,47 @@ MeshFilter:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1398230978} m_GameObject: {fileID: 1398230978}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} 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 --- !u!1 &1453277286
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -141009,12 +141097,12 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1793734342} m_GameObject: {fileID: 1793734342}
serializedVersion: 2 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_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 1408989034}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1813306719 --- !u!1001 &1813306719
PrefabInstance: PrefabInstance:
@@ -141022,7 +141110,7 @@ PrefabInstance:
serializedVersion: 2 serializedVersion: 2
m_Modification: m_Modification:
serializedVersion: 3 serializedVersion: 3
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 1408989034}
m_Modifications: m_Modifications:
- target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: -8789529727683405701, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: notificationText propertyPath: notificationText
@@ -141058,7 +141146,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.x propertyPath: m_LocalRotation.x
value: 0 value: -0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} - target: {fileID: 600579176653881994, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3}
propertyPath: m_LocalRotation.y propertyPath: m_LocalRotation.y
@@ -141105,6 +141193,11 @@ PrefabInstance:
m_AddedGameObjects: [] m_AddedGameObjects: []
m_AddedComponents: [] m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: b96d0fcc541b0b947907dd08b9f5beab, type: 3} 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 --- !u!43 &1820026756
Mesh: Mesh:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -141780,6 +141873,70 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 333057066} m_Father: {fileID: 333057066}
m_LocalEulerAnglesHint: {x: 0, y: 45, z: 0} 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 --- !u!43 &1951987860
Mesh: Mesh:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -144775,13 +144932,5 @@ Transform:
SceneRoots: SceneRoots:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_Roots: m_Roots:
- {fileID: 678938550} - {fileID: 1408989034}
- {fileID: 333057066} - {fileID: 258369250}
- {fileID: 1098531126}
- {fileID: 348410792}
- {fileID: 963322862}
- {fileID: 1793734345}
- {fileID: 628283017}
- {fileID: 241771774}
- {fileID: 1813306719}
- {fileID: 572767392}

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 System.Collections.Generic;
using UnityEngine; 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 public class EnemyAI : MonoBehaviour
{ {
[Header("References")] [Header("References")]
@@ -11,6 +13,12 @@ public class EnemyAI : MonoBehaviour
public float moveSpeed = 3f; public float moveSpeed = 3f;
public float rotateSpeed = 50f; 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")] [Header("Artifact")]
public bool playerHasArtifact; public bool playerHasArtifact;
@@ -21,23 +29,44 @@ public class EnemyAI : MonoBehaviour
public float maxShootDelay = 3f; public float maxShootDelay = 3f;
private float nextShootTime; private float nextShootTime;
private NavMeshAgent agent;
public Node behaviorTreeRoot; public Node behaviorTreeRoot;
private void Start() 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); nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
InitBehaviorTree(); InitBehaviorTree();
FindPlayer();
} }
private void Update() 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(); behaviorTreeRoot?.Evaluate();
} }
private void FindPlayer()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null)
{
player = playerObj.transform;
}
}
private void InitBehaviorTree() private void InitBehaviorTree()
{ {
// Player có artifact -> focus + shoot // Player có artifact -> focus + shoot
@@ -54,14 +83,14 @@ public class EnemyAI : MonoBehaviour
new TaskNode(ActionMoveToPlayer) new TaskNode(ActionMoveToPlayer)
}); });
// Không thấy ai -> scan // Không thấy ai -> Tuần tra bằng NavMesh
var scanNode = new TaskNode(ActionRotationScan); var patrolNode = new TaskNode(ActionPatrol);
behaviorTreeRoot = new Selector(new List<Node> behaviorTreeRoot = new Selector(new List<Node>
{ {
laserSequence, laserSequence,
chaseSequence, chaseSequence,
scanNode patrolNode
}); });
} }
@@ -69,18 +98,14 @@ public class EnemyAI : MonoBehaviour
private NodeState CheckHasArtifact() private NodeState CheckHasArtifact()
{ {
return playerHasArtifact return playerHasArtifact ? NodeState.Success : NodeState.Failure;
? NodeState.Success
: NodeState.Failure;
} }
private NodeState CheckCanSeePlayer() private NodeState CheckCanSeePlayer()
{ {
if (player == null) if (player == null) return NodeState.Failure;
return NodeState.Failure;
float distance = float distance = Vector3.Distance(transform.position, player.position);
Vector3.Distance(transform.position, player.position);
if (distance <= detectRange) if (distance <= detectRange)
{ {
@@ -94,66 +119,74 @@ public class EnemyAI : MonoBehaviour
#region ACTIONS #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( // Kiểm tra xem NPC đã đến điểm đích chưa
Vector3.up, if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
rotateSpeed * Time.deltaTime); {
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; return NodeState.Running;
} }
private NodeState ActionMoveToPlayer() private NodeState ActionMoveToPlayer()
{ {
if (player == null) if (player == null) return NodeState.Failure;
return NodeState.Failure;
Debug.Log("Chasing Player"); Debug.Log("Chasing Player");
Vector3 dir = agent.isStopped = false;
(player.position - transform.position).normalized; agent.speed = moveSpeed; // Phục hồi tốc độ rượt đuổi
agent.SetDestination(player.position);
transform.position +=
dir *
moveSpeed *
Time.deltaTime;
return NodeState.Running; return NodeState.Running;
} }
private NodeState ActionFocusAndShoot() private NodeState ActionFocusAndShoot()
{ {
if (player == null) if (player == null) return NodeState.Failure;
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 // Focus player
Vector3 dir = Vector3 dir = player.position - transform.position;
player.position - transform.position;
dir.y = 0f; dir.y = 0f;
if (dir != Vector3.zero) if (dir != Vector3.zero)
{ {
Quaternion targetRotation = Quaternion targetRotation = Quaternion.LookRotation(dir);
Quaternion.LookRotation(dir); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
transform.rotation =
Quaternion.Slerp(
transform.rotation,
targetRotation,
5f * Time.deltaTime);
} }
// Shoot with random delay // Shoot with random delay
if (Time.time >= nextShootTime) if (Time.time >= nextShootTime)
{ {
ShootLaser(); ShootLaser();
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
nextShootTime =
Time.time +
Random.Range(minShootDelay, maxShootDelay);
} }
return NodeState.Running; return NodeState.Running;
@@ -161,14 +194,8 @@ public class EnemyAI : MonoBehaviour
private void ShootLaser() private void ShootLaser()
{ {
if (laserPrefab == null || firePoint == null) if (laserPrefab == null || firePoint == null) return;
return; Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
Instantiate(
laserPrefab,
firePoint.position,
firePoint.rotation);
Debug.Log("Laser Shot!"); Debug.Log("Laser Shot!");
} }

View File

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