npc talk
This commit is contained in:
10
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
10
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
@@ -6,9 +6,15 @@
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Fonts/BitcountSingleInk-VariableFont_CRSV,ELSH,ELXP,SZP1,SZP2,XPN1,XPN2,YPN1,YPN2,slnt,wght SDF.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Fonts/BitcountSingleInk-VariableFont_CRSV,ELSH,ELXP,SZP1,SZP2,XPN1,XPN2,YPN1,YPN2,slnt,wght SDF.asset" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Audio/Basic Locomotion/Mixers/FOR AI GAME.mixer" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Audio/Basic Locomotion/Mixers/FOR AI GAME.mixer" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/NPC/xNPC.prefab" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/NPC/xNPC.prefab" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Presets/FOR AI GAME.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Presets/FOR AI GAME.asset" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Scenes/Cho môn AI/Only AI.unity" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scenes/Cho môn AI/Only AI.unity" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/AI NPC/ChatBubble.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/AI NPC/ChatBubble.cs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/AI NPC/EnemyAI.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/AI NPC/EnemyAI.cs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/AI NPC/GeminiService.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/AI NPC/GeminiService.cs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player/CharacterController/Editor/Resources/vEditorStartupPrefs.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player/CharacterController/Editor/Resources/vEditorStartupPrefs.asset" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Third Parties/TextMesh Pro/Resources/Fonts & Materials/LiberationSans SDF - Fallback.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Third Parties/TextMesh Pro/Resources/Fonts & Materials/LiberationSans SDF - Fallback.asset" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -178,7 +184,7 @@
|
||||
<workItem from="1780364354282" duration="4357000" />
|
||||
<workItem from="1780409218377" duration="9852000" />
|
||||
<workItem from="1780494322686" duration="643000" />
|
||||
<workItem from="1780633654231" duration="7070000" />
|
||||
<workItem from="1780633654231" duration="12926000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
||||
@@ -57,7 +57,7 @@ AudioMixerGroupController:
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: -5864968286415327506}
|
||||
m_UserColorIndex: 0
|
||||
m_UserColorIndex: 3
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
@@ -76,7 +76,15 @@ AudioMixerController:
|
||||
m_SuspendThreshold: -80
|
||||
m_EnableSuspend: 1
|
||||
m_UpdateMode: 0
|
||||
m_ExposedParameters: []
|
||||
m_ExposedParameters:
|
||||
- guid: 9d220dd29b931844f8bf0cabe811b681
|
||||
name: MasterVolume
|
||||
- guid: bf1d95a6c9abe924fa3d98795325cc1b
|
||||
name: MusicVolume
|
||||
- guid: 27be8a879c26ddd4585dbedff31a33e3
|
||||
name: SFXVolume
|
||||
- guid: 6b8a96f21df846741a0036e2a182ec15
|
||||
name: UIVolume
|
||||
m_AudioMixerGroupViews:
|
||||
- guids:
|
||||
- f31c09d88c9703d4aae5faeb43b19082
|
||||
@@ -104,7 +112,7 @@ AudioMixerGroupController:
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: 24400004}
|
||||
m_UserColorIndex: 0
|
||||
m_UserColorIndex: 1
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
@@ -148,7 +156,7 @@ AudioMixerGroupController:
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: -8884289898024163737}
|
||||
m_UserColorIndex: 0
|
||||
m_UserColorIndex: 2
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
@@ -167,7 +175,7 @@ AudioMixerGroupController:
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: -4762669937127778309}
|
||||
m_UserColorIndex: 0
|
||||
m_UserColorIndex: 4
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
|
||||
@@ -88,6 +88,7 @@ GameObject:
|
||||
- component: {fileID: 7721795325225879236}
|
||||
- component: {fileID: 1194338186308686302}
|
||||
- component: {fileID: 1620994605374968907}
|
||||
- component: {fileID: 3045503552244378433}
|
||||
m_Layer: 5
|
||||
m_Name: Canvas
|
||||
m_TagString: Untagged
|
||||
@@ -191,6 +192,21 @@ CanvasGroup:
|
||||
m_Interactable: 1
|
||||
m_BlocksRaycasts: 1
|
||||
m_IgnoreParentGroups: 0
|
||||
--- !u!114 &3045503552244378433
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 3381350595760429543}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: ea510cea4b9ed1547ae4725a2ded949a, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::Hallucinate.UI.ChatBubble
|
||||
textDisplay: {fileID: 4454520411962799200}
|
||||
canvasGroup: {fileID: 1620994605374968907}
|
||||
bubbleRect: {fileID: 6775114823217050358}
|
||||
--- !u!1 &5687887011233860168
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -209,7 +225,7 @@ GameObject:
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
m_IsActive: 0
|
||||
--- !u!4 &5863061020199015852
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -299,10 +315,10 @@ BoxCollider:
|
||||
m_LayerOverridePriority: 0
|
||||
m_IsTrigger: 0
|
||||
m_ProvidesContacts: 0
|
||||
m_Enabled: 1
|
||||
m_Enabled: 0
|
||||
serializedVersion: 3
|
||||
m_Size: {x: 1, y: 1, z: 1}
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
m_Size: {x: 1, y: 1, z: 1.2136346}
|
||||
m_Center: {x: 0, y: 0, z: -0.106817186}
|
||||
--- !u!1 &6192986059782212342
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -452,12 +468,12 @@ GameObject:
|
||||
- component: {fileID: 8272839718325411334}
|
||||
- component: {fileID: 5770331367975928816}
|
||||
- component: {fileID: 4112854993683970537}
|
||||
- component: {fileID: 3240800621832008763}
|
||||
- component: {fileID: 8239948856752686218}
|
||||
- component: {fileID: 3321677048699702084}
|
||||
- component: {fileID: 6469822191588635990}
|
||||
- component: {fileID: 9027690817715396964}
|
||||
m_Layer: 0
|
||||
m_Name: xNPC
|
||||
m_TagString: Untagged
|
||||
m_TagString: Enemy
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
@@ -495,7 +511,8 @@ MonoBehaviour:
|
||||
player: {fileID: 0}
|
||||
moveSpeed: 3
|
||||
rotateSpeed: 50
|
||||
patrolPoints: []
|
||||
patrolWaypoints: []
|
||||
currentWaypointIndex: 0
|
||||
patrolWaitTime: 2
|
||||
playerHasArtifact: 0
|
||||
laserPrefab: {fileID: 3965388737199864462, guid: fbec2b501d70daa4c9cb481ba53fc0b8, type: 3}
|
||||
@@ -505,6 +522,11 @@ MonoBehaviour:
|
||||
dodgeForce: 8
|
||||
dodgeDuration: 0.5
|
||||
dodgeCooldown: 3
|
||||
npcName: Guard
|
||||
persona: You are a grumpy guard protecting gold.
|
||||
talkRange: 20
|
||||
talkCooldown: 30
|
||||
isTalking: 0
|
||||
--- !u!195 &5770331367975928816
|
||||
NavMeshAgent:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -554,21 +576,6 @@ Rigidbody:
|
||||
m_Interpolate: 0
|
||||
m_Constraints: 112
|
||||
m_CollisionDetection: 0
|
||||
--- !u!114 &3240800621832008763
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 7522161431095319480}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: ea510cea4b9ed1547ae4725a2ded949a, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::Hallucinate.UI.ChatBubble
|
||||
textDisplay: {fileID: 4454520411962799200}
|
||||
canvasGroup: {fileID: 1620994605374968907}
|
||||
bubbleRect: {fileID: 6775114823217050358}
|
||||
--- !u!114 &8239948856752686218
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -591,7 +598,7 @@ MonoBehaviour:
|
||||
m_Bits: 256
|
||||
canSeePlayer: 0
|
||||
lastKnownPlayerPosition: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &3321677048699702084
|
||||
--- !u!114 &6469822191588635990
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
@@ -603,8 +610,39 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 35bba55c2a743d042ab1fff35e29db50, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::AnimatorAI
|
||||
debugMode: 1
|
||||
debugColor: {r: 1, g: 0, b: 0.21842146, a: 1}
|
||||
useSimulation: 0
|
||||
autoCycleSpeed: 0
|
||||
simVerticalVelocity: 0
|
||||
simIsSprinting: 0
|
||||
simIsAiming: 0
|
||||
simMoveSetID: 0
|
||||
sprintThreshold: 0.8
|
||||
dampTime: 0.1
|
||||
--- !u!136 &9027690817715396964
|
||||
CapsuleCollider:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 7522161431095319480}
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_LayerOverridePriority: 0
|
||||
m_IsTrigger: 1
|
||||
m_ProvidesContacts: 0
|
||||
m_Enabled: 1
|
||||
serializedVersion: 2
|
||||
m_Radius: 0.37279892
|
||||
m_Height: 1.8474874
|
||||
m_Direction: 1
|
||||
m_Center: {x: 0, y: 0.92911196, z: -0.24853802}
|
||||
--- !u!1001 &7561534673732472622
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -17,27 +17,27 @@ MonoBehaviour:
|
||||
Clip: {fileID: 8300000, guid: fecdee2673ce2f542a3db1a8b56d1571, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: UI_Warning
|
||||
Clip: {fileID: 8300000, guid: 32b49cf6b9e2e8e408663785554c3e75, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: UI_Click
|
||||
Clip: {fileID: 8300000, guid: 30f85fcee050492448db7f91217910b3, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: Item_Pickup
|
||||
Clip: {fileID: 8300000, guid: b1ae905972eed154497f5454b22ba711, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: Enemy_Alert
|
||||
Clip: {fileID: 8300000, guid: d5f9671eecb70364f8282999c81d8295, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: Enemy_Shoot
|
||||
Clip: {fileID: 8300000, guid: de0b7f47746d51f48b733b64b307540e, type: 3}
|
||||
DefaultVolume: 1
|
||||
@@ -47,14 +47,14 @@ MonoBehaviour:
|
||||
Clip: {fileID: 8300000, guid: e14cb014b8c41bf4a98768f2e4b4c1d7, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: NPC_Interact
|
||||
Clip: {fileID: 8300000, guid: 6d36adcf33e186c4cbe64a4c149e138f, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
- Name: NPC_Response
|
||||
Clip: {fileID: 8300000, guid: 7c266b12aa7ed1a49bf7ea0889d32302, type: 3}
|
||||
DefaultVolume: 1
|
||||
DefaultPitch: 1
|
||||
MixerGroup: {fileID: 0}
|
||||
MixerGroup: {fileID: 24300002, guid: a622ceda579315442bd0a82d6626974c, type: 2}
|
||||
|
||||
@@ -90229,6 +90229,10 @@ PrefabInstance:
|
||||
propertyPath: m_Layer
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 8272839718325411334, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
|
||||
propertyPath: npcName
|
||||
value: bob
|
||||
objectReference: {fileID: 0}
|
||||
m_RemovedComponents: []
|
||||
m_RemovedGameObjects: []
|
||||
m_AddedGameObjects: []
|
||||
@@ -93669,7 +93673,7 @@ PrefabInstance:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
value: -6.00416
|
||||
value: 1.26
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
|
||||
propertyPath: m_LocalPosition.y
|
||||
@@ -93677,7 +93681,7 @@ PrefabInstance:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: -28.878128
|
||||
value: -8.928129
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 6442306242859885696, guid: 15df559ce497e104a81254e0adf3107e, type: 3}
|
||||
propertyPath: m_LocalRotation.w
|
||||
@@ -134181,6 +134185,52 @@ Transform:
|
||||
m_Children: []
|
||||
m_Father: {fileID: 1617980815}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1130381420
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1130381421}
|
||||
- component: {fileID: 1130381422}
|
||||
m_Layer: 0
|
||||
m_Name: Gemini
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &1130381421
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1130381420}
|
||||
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: 1617980815}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &1130381422
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1130381420}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: a859fc8e9ec10a347a3704b6045ca7e8, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::Hallucinate.AI.GeminiService
|
||||
apiKey: AIzaSyC4DRm2dffDuDogYkY0Ag86p-EYLu67bDo
|
||||
geminiURL: https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent
|
||||
--- !u!1 &1143870345
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -145170,6 +145220,7 @@ Transform:
|
||||
- {fileID: 1158761166}
|
||||
- {fileID: 1922855029}
|
||||
- {fileID: 1098531126}
|
||||
- {fileID: 1130381421}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1620426061
|
||||
|
||||
@@ -14,13 +14,19 @@ namespace Hallucinate.UI
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
mainCameraTransform = Camera.main.transform;
|
||||
canvasGroup.alpha = 0;
|
||||
gameObject.SetActive(false);
|
||||
if (canvasGroup != null) canvasGroup.alpha = 0;
|
||||
// gameObject.SetActive(false); // Bỏ dòng này để tránh tắt nhầm NPC gốc
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
// Tìm Camera nếu chưa có (Tránh lỗi Null nếu Camera chưa spawn hoặc bị xóa)
|
||||
if (mainCameraTransform == null)
|
||||
{
|
||||
if (Camera.main != null) mainCameraTransform = Camera.main.transform;
|
||||
else return;
|
||||
}
|
||||
|
||||
// Billboard effect
|
||||
transform.LookAt(transform.position + mainCameraTransform.rotation * Vector3.forward, mainCameraTransform.rotation * Vector3.up);
|
||||
}
|
||||
|
||||
@@ -2,53 +2,51 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using System.Linq;
|
||||
|
||||
// Quy trình ưu tiên: Né đòn --> Bắn hạ (Artifact) --> Đuổi theo (Vector) --> Điều tra --> Đi tuần
|
||||
// Quy trình ưu tiên: Né đòn --> Bắn hạ (Artifact) --> Đuổi theo (Vector) --> Điều tra --> Nói chuyện --> Đi tuần
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(FieldOfView))]
|
||||
public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
[Header("Tham chieu he thong")]
|
||||
[Header("References")]
|
||||
public Transform player;
|
||||
private NavMeshAgent agent;
|
||||
private Rigidbody rb;
|
||||
private FieldOfView fov;
|
||||
|
||||
[Header("Toc do & Di chuyen")]
|
||||
[Header("Movement Settings")]
|
||||
public float moveSpeed = 3f;
|
||||
public float rotateSpeed = 10f;
|
||||
|
||||
[Header("He thong di tuan (Waypoints)")]
|
||||
[Header("Patrol Settings")]
|
||||
public Transform[] patrolWaypoints;
|
||||
public int currentWaypointIndex = 0;
|
||||
public float patrolWaitTime = 2f;
|
||||
private float currentWaitTime = 0f;
|
||||
|
||||
[Header("Trang thai Co vat")]
|
||||
[Header("Combat State")]
|
||||
public bool playerHasArtifact;
|
||||
|
||||
[Header("Vu khi Laser")]
|
||||
public GameObject laserPrefab;
|
||||
public Transform firePoint;
|
||||
public float minShootDelay = 1f;
|
||||
public float maxShootDelay = 3f;
|
||||
private float nextShootTime;
|
||||
|
||||
[Header("Co che Ne don (Vat ly)")]
|
||||
[Header("Dodge Settings")]
|
||||
public float dodgeForce = 10f;
|
||||
public float dodgeDuration = 0.2f;
|
||||
public float dodgeCooldown = 1.2f;
|
||||
private bool isDodging = false;
|
||||
private float nextDodgeTime = 0f;
|
||||
|
||||
[Header("Conversation")]
|
||||
[Header("Conversation Settings")]
|
||||
public string npcName = "Guard";
|
||||
public string persona = "You are a grumpy guard protecting gold.";
|
||||
public float talkRange = 4f;
|
||||
public float talkCooldown = 30f;
|
||||
[TextArea] public string persona = "You are a grumpy guard protecting gold.";
|
||||
public float talkRange = 10f;
|
||||
public float talkCooldown = 15f;
|
||||
private float lastTalkTime;
|
||||
private bool isTalking;
|
||||
public bool isTalking; // Public để debug
|
||||
private EnemyAI talkingPartner;
|
||||
private Hallucinate.UI.ChatBubble chatBubble;
|
||||
|
||||
@@ -59,8 +57,9 @@ public class EnemyAI : MonoBehaviour
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
fov = GetComponent<FieldOfView>();
|
||||
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
|
||||
|
||||
// Thiết lập Rigidbody chuẩn Unity 6 để sẵn sàng nhận lực né
|
||||
// Rigidbody setup cho Unity 6
|
||||
rb.isKinematic = true;
|
||||
rb.freezeRotation = true;
|
||||
|
||||
@@ -69,48 +68,30 @@ public class EnemyAI : MonoBehaviour
|
||||
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
|
||||
if (playerObj != null) player = playerObj.transform;
|
||||
}
|
||||
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
|
||||
|
||||
/*
|
||||
// Tạm thời comment đoạn này để tránh lỗi Tag chưa định nghĩa
|
||||
if (patrolWaypoints == null || patrolWaypoints.Length == 0)
|
||||
{
|
||||
patrolWaypoints = GameObject.FindGameObjectsWithTag("PatrolPoint")
|
||||
.Select(go => go.transform).ToArray();
|
||||
}
|
||||
*/
|
||||
|
||||
InitTree();
|
||||
|
||||
Debug.Log($"<color=white>[AI {npcName}] Init complete. Waypoints: {patrolWaypoints.Length}</color>");
|
||||
}
|
||||
|
||||
void InitTree()
|
||||
{
|
||||
// 1. Nhánh Né đòn (Khi player bấm chuột trái tấn công)
|
||||
var dodgeSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckDodgeConditions),
|
||||
new TaskNode(ActionDodge)
|
||||
});
|
||||
|
||||
// 2. Nhánh Tấn công Laser (Khi player lấy được cổ vật)
|
||||
var laserSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckHasArtifact),
|
||||
new TaskNode(ActionFocusAndShoot)
|
||||
});
|
||||
|
||||
// 3. Nhánh Đuổi theo (Sử dụng agent.Move di chuyển thẳng bằng Vector)
|
||||
var chaseSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanSeePlayer),
|
||||
new TaskNode(ActionChasePlayer)
|
||||
});
|
||||
|
||||
// 4. Nhánh Điều tra (Khi mất dấu, đi kiểm tra vị trí cuối cùng)
|
||||
var investigateSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckHasInvestigateTarget),
|
||||
new TaskNode(ActionInvestigate)
|
||||
});
|
||||
var talkSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanTalkToNPC),
|
||||
new TaskNode(ActionTalk)
|
||||
});
|
||||
// 5. Nhánh Đi tuần mặc định
|
||||
var dodgeSequence = new Sequence(new List<Node> { new TaskNode(CheckDodgeConditions), new TaskNode(ActionDodge) });
|
||||
var laserSequence = new Sequence(new List<Node> { new TaskNode(CheckHasArtifact), new TaskNode(ActionFocusAndShoot) });
|
||||
var chaseSequence = new Sequence(new List<Node> { new TaskNode(CheckCanSeePlayer), new TaskNode(ActionChasePlayer) });
|
||||
var investigateSequence = new Sequence(new List<Node> { new TaskNode(CheckHasInvestigateTarget), new TaskNode(ActionInvestigate) });
|
||||
var talkSequence = new Sequence(new List<Node> { new TaskNode(CheckCanTalkToNPC), new TaskNode(ActionTalk) });
|
||||
var patrolAction = new TaskNode(ActionPatrol);
|
||||
|
||||
// Xây dựng cây tổng hợp theo thứ tự ưu tiên từ trên xuống dưới
|
||||
rootNode = new Selector(new List<Node>
|
||||
{
|
||||
dodgeSequence,
|
||||
@@ -125,21 +106,27 @@ public class EnemyAI : MonoBehaviour
|
||||
void Update()
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
// An toàn cho NavMeshAgent
|
||||
if (!agent.isOnNavMesh)
|
||||
{
|
||||
Debug.LogWarning($"[AI {npcName}] NPC is NOT on NavMesh!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isTalking && !isDodging && agent.isStopped)
|
||||
agent.isStopped = false;
|
||||
|
||||
rootNode?.Evaluate();
|
||||
}
|
||||
|
||||
#region CONDITIONS (CAC HAM KIEM TRA)
|
||||
#region CONDITIONS
|
||||
|
||||
private NodeState CheckDodgeConditions()
|
||||
{
|
||||
// Nếu đang trong quá trình né thì luôn cho phép chạy tiếp hành động né
|
||||
if (isDodging) return NodeState.Success;
|
||||
|
||||
// Điều kiện kích hoạt né: Thấy player + Player nhấn chuột trái + Hết cooldown
|
||||
if (fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
|
||||
{
|
||||
if (fov != null && fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
|
||||
return NodeState.Success;
|
||||
}
|
||||
return NodeState.Failure;
|
||||
}
|
||||
|
||||
@@ -151,132 +138,65 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
private NodeState CheckCanSeePlayer()
|
||||
{
|
||||
|
||||
return fov.canSeePlayer ? NodeState.Success : NodeState.Failure;
|
||||
bool canSee = fov != null && fov.canSeePlayer;
|
||||
if (canSee) StopConversation();
|
||||
return canSee ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
private NodeState CheckHasInvestigateTarget()
|
||||
{
|
||||
return fov.lastKnownPlayerPosition != Vector3.zero ? NodeState.Success : NodeState.Failure;
|
||||
return (fov != null && fov.lastKnownPlayerPosition != Vector3.zero) ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ACTIONS (CAC HAM HANH DONG)
|
||||
|
||||
private NodeState ActionDodge()
|
||||
{
|
||||
if (!isDodging)
|
||||
{
|
||||
StartCoroutine(DodgeRollRoutine());
|
||||
nextDodgeTime = Time.time + dodgeCooldown;
|
||||
}
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private IEnumerator DodgeRollRoutine()
|
||||
{
|
||||
isDodging = true;
|
||||
agent.enabled = false; // Tắt định vị để nhường quyền cho Vật lý
|
||||
rb.isKinematic = false; // Bật chế độ vật lý động
|
||||
|
||||
// Tính hướng né vuông góc với Player
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
Vector3 perpendicularDir = new Vector3(-directionToPlayer.z, 0, directionToPlayer.x);
|
||||
Vector3 dodgeDirection = (Random.Range(0, 2) == 0 ? perpendicularDir : -perpendicularDir).normalized;
|
||||
|
||||
// Đẩy bằng lực Impulse vật lý thực tế
|
||||
rb.AddForce(dodgeDirection * dodgeForce, ForceMode.Impulse);
|
||||
|
||||
yield return new WaitForSeconds(dodgeDuration);
|
||||
|
||||
// Trả lại quyền cho NavMeshAgent sau khi né xong
|
||||
rb.linearVelocity = Vector3.zero; // Cú pháp chuẩn của Unity 6
|
||||
rb.isKinematic = true;
|
||||
agent.enabled = true;
|
||||
isDodging = false;
|
||||
}
|
||||
|
||||
private NodeState ActionChasePlayer()
|
||||
{
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed;
|
||||
|
||||
// 1. Tính toán hướng đi thẳng tắp (Bỏ trục Y)
|
||||
Vector3 dir = player.position - transform.position;
|
||||
dir.y = 0f;
|
||||
dir.Normalize();
|
||||
|
||||
// 2. Di chuyển tịnh tiến bằng Vector tích hợp qua Agent.Move
|
||||
Vector3 movement = dir * moveSpeed * Time.deltaTime;
|
||||
agent.Move(movement);
|
||||
|
||||
// 3. Xoay mượt mà theo hướng di chuyển của Vector đúng ý bạn
|
||||
if (dir != Vector3.zero)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(dir);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
private NodeState CheckCanTalkToNPC()
|
||||
{
|
||||
|
||||
if (playerHasArtifact || fov.canSeePlayer) return NodeState.Failure;
|
||||
if (playerHasArtifact || (fov != null && fov.canSeePlayer)) return NodeState.Failure;
|
||||
if (Time.time < lastTalkTime + talkCooldown) return NodeState.Failure;
|
||||
if (isTalking) return NodeState.Success;
|
||||
|
||||
// Tìm NPC gần nhất
|
||||
// Quét tìm NPC
|
||||
Collider[] hitColliders = Physics.OverlapSphere(transform.position, talkRange);
|
||||
foreach (var hit in hitColliders)
|
||||
{
|
||||
if (hit.gameObject != gameObject && hit.CompareTag("Enemy"))
|
||||
if (hit.gameObject == gameObject) continue;
|
||||
|
||||
EnemyAI other = hit.GetComponentInParent<EnemyAI>();
|
||||
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
|
||||
{
|
||||
EnemyAI other = hit.GetComponent<EnemyAI>();
|
||||
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
|
||||
// Chỉ ID nhỏ hơn gọi để tránh trùng
|
||||
if (gameObject.GetInstanceID() < other.gameObject.GetInstanceID())
|
||||
{
|
||||
talkingPartner = other;
|
||||
return NodeState.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NodeState.Failure;
|
||||
}
|
||||
private NodeState ActionTalk()
|
||||
|
||||
#endregion
|
||||
|
||||
#region ACTIONS
|
||||
|
||||
private NodeState ActionTalk()
|
||||
{
|
||||
if (talkingPartner == null) return NodeState.Failure;
|
||||
|
||||
if (!isTalking)
|
||||
{
|
||||
isTalking = true;
|
||||
agent.isStopped = true;
|
||||
|
||||
// Xoay về phía bạn
|
||||
FaceTarget(talkingPartner.transform.position);
|
||||
|
||||
// Bắt đầu hội thoại qua Gemini
|
||||
StartNPCConversation();
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private void StartNPCConversation()
|
||||
{
|
||||
string prompt = $"You are {npcName}. You are talking to your fellow guard {talkingPartner.npcName}. " +
|
||||
"Keep it short (1 sentence). Topic: gold security or complaining about work.";
|
||||
|
||||
Hallucinate.AI.GeminiService.Instance.GetResponse(persona, prompt, (response) => {
|
||||
if (chatBubble != null) chatBubble.Show(response);
|
||||
|
||||
// Hẹn giờ kết thúc hội thoại
|
||||
Invoke(nameof(EndConversation), 5f);
|
||||
});
|
||||
|
||||
// Thông báo cho bạn diễn cũng dừng lại để "nghe"
|
||||
talkingPartner.OnPartnerTalked(this);
|
||||
Debug.Log($"<color=yellow>[AI {npcName}] Talking to {talkingPartner.npcName}</color>");
|
||||
|
||||
string prompt = $"You are {npcName}. Speak 1 short sentence in English to your colleague {talkingPartner.npcName} about the shift.";
|
||||
Hallucinate.AI.GeminiService.Instance.GetResponse(persona, prompt, (response) => {
|
||||
if (chatBubble != null) chatBubble.Show(response);
|
||||
Invoke(nameof(EndConversation), 5f);
|
||||
});
|
||||
talkingPartner.OnPartnerTalked(this);
|
||||
}
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
public void OnPartnerTalked(EnemyAI partner)
|
||||
@@ -285,8 +205,6 @@ public class EnemyAI : MonoBehaviour
|
||||
talkingPartner = partner;
|
||||
agent.isStopped = true;
|
||||
FaceTarget(partner.transform.position);
|
||||
|
||||
// Chờ bạn nói xong mới phản hồi
|
||||
Invoke(nameof(EndConversation), 6f);
|
||||
}
|
||||
|
||||
@@ -294,7 +212,7 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
isTalking = false;
|
||||
lastTalkTime = Time.time;
|
||||
if (agent != null) agent.isStopped = false;
|
||||
if (agent != null && agent.isOnNavMesh) agent.isStopped = false;
|
||||
talkingPartner = null;
|
||||
}
|
||||
|
||||
@@ -303,47 +221,37 @@ public class EnemyAI : MonoBehaviour
|
||||
if (!isTalking) return;
|
||||
CancelInvoke(nameof(EndConversation));
|
||||
EndConversation();
|
||||
if (chatBubble != null) chatBubble.Show("Suỵt! Có gì đó không ổn...", 2f);
|
||||
if (chatBubble != null) chatBubble.Show("Wait, what's that?!", 2f);
|
||||
}
|
||||
|
||||
private void FaceTarget(Vector3 targetPos)
|
||||
private NodeState ActionPatrol()
|
||||
{
|
||||
Vector3 dir = targetPos - transform.position;
|
||||
dir.y = 0;
|
||||
if (dir != Vector3.zero)
|
||||
if (patrolWaypoints == null || patrolWaypoints.Length == 0) return NodeState.Failure;
|
||||
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed * 0.5f;
|
||||
|
||||
var target = patrolWaypoints[currentWaypointIndex];
|
||||
agent.SetDestination(target.position);
|
||||
|
||||
if (Vector3.Distance(transform.position, target.position) < 1.5f)
|
||||
{
|
||||
transform.rotation = Quaternion.LookRotation(dir);
|
||||
currentWaitTime += Time.deltaTime;
|
||||
if (currentWaitTime >= patrolWaitTime)
|
||||
{
|
||||
currentWaypointIndex = (currentWaypointIndex + 1) % patrolWaypoints.Length;
|
||||
currentWaitTime = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private NodeState ActionFocusAndShoot()
|
||||
{
|
||||
agent.isStopped = true; // Đứng im bắn cố định khi có cổ vật
|
||||
|
||||
// Tập trung xoay người nhìn về phía Player
|
||||
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, rotateSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Cơ chế đếm ngược bắn Laser ngẫu nhiên giống code cũ của bạn
|
||||
if (Time.time >= nextShootTime)
|
||||
{
|
||||
ShootLaser();
|
||||
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private void ShootLaser()
|
||||
private NodeState ActionChasePlayer()
|
||||
{
|
||||
if (laserPrefab == null || firePoint == null) return;
|
||||
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
|
||||
Debug.Log("Laser Shot!");
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed;
|
||||
agent.SetDestination(player.position);
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionInvestigate()
|
||||
@@ -351,44 +259,67 @@ public class EnemyAI : MonoBehaviour
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed * 0.7f;
|
||||
agent.SetDestination(fov.lastKnownPlayerPosition);
|
||||
|
||||
// Đi tới vị trí cuối cùng nhìn thấy Player
|
||||
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
|
||||
if (Vector3.Distance(transform.position, fov.lastKnownPlayerPosition) < 1.5f)
|
||||
{
|
||||
fov.lastKnownPlayerPosition = Vector3.zero; // Xóa vị trí nghi vấn để thoát trạng thái
|
||||
fov.lastKnownPlayerPosition = Vector3.zero;
|
||||
return NodeState.Success;
|
||||
}
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionPatrol()
|
||||
private NodeState ActionFocusAndShoot()
|
||||
{
|
||||
if (patrolWaypoints == null || patrolWaypoints.Length == 0)
|
||||
agent.isStopped = true;
|
||||
FaceTarget(player.position);
|
||||
if (Time.time >= nextShootTime)
|
||||
{
|
||||
return NodeState.Failure;
|
||||
if (laserPrefab && firePoint) Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
|
||||
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
||||
}
|
||||
|
||||
agent.isStopped = false;
|
||||
agent.speed = moveSpeed * 0.5f; // Đi tuần chậm rãi
|
||||
|
||||
var targetWaypoint = patrolWaypoints[currentWaypointIndex];
|
||||
var distanceToTarget = Vector3.Distance(transform.position, targetWaypoint.position);
|
||||
|
||||
// Nếu đã đến gần điểm tuần tra الحالي
|
||||
if (distanceToTarget < 1f)
|
||||
{
|
||||
currentWaitTime += Time.deltaTime;
|
||||
// Chờ một khoảng thời gian trước khi chuyển sang điểm tiếp theo
|
||||
if (currentWaitTime >= patrolWaitTime)
|
||||
{
|
||||
currentWaypointIndex = (currentWaypointIndex + 1) % patrolWaypoints.Length;
|
||||
currentWaitTime = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
agent.SetDestination(patrolWaypoints[currentWaypointIndex].position);
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionDodge()
|
||||
{
|
||||
if (!isDodging) StartCoroutine(DodgeRollRoutine());
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private IEnumerator DodgeRollRoutine()
|
||||
{
|
||||
isDodging = true;
|
||||
agent.enabled = false;
|
||||
rb.isKinematic = false;
|
||||
Vector3 dir = (player.position - transform.position).normalized;
|
||||
Vector3 perp = new Vector3(-dir.z, 0, dir.x);
|
||||
rb.AddForce((Random.value > 0.5f ? perp : -perp) * dodgeForce, ForceMode.Impulse);
|
||||
yield return new WaitForSeconds(dodgeDuration);
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.isKinematic = true;
|
||||
agent.enabled = true;
|
||||
isDodging = false;
|
||||
}
|
||||
|
||||
private void FaceTarget(Vector3 pos)
|
||||
{
|
||||
Vector3 dir = (pos - transform.position);
|
||||
dir.y = 0;
|
||||
if (dir != Vector3.zero) transform.rotation = Quaternion.LookRotation(dir);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
// Vẽ vùng nói chuyện (Xanh lá)
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawWireSphere(transform.position, talkRange);
|
||||
|
||||
// Vẽ đường nối tới bạn diễn nếu đang nói
|
||||
if (isTalking && talkingPartner != null)
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawLine(transform.position + Vector3.up, talkingPartner.transform.position + Vector3.up);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,11 +40,16 @@ namespace Hallucinate.AI
|
||||
{
|
||||
var jsonBody = $@"{{
|
||||
""systemInstruction"": {{""parts"": [{{ ""text"": ""{persona}"" }}]}},
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}]
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}],
|
||||
""generationConfig"": {{
|
||||
""maxOutputTokens"": 60,
|
||||
""temperature"": 0.7
|
||||
}}
|
||||
}}";
|
||||
|
||||
var requestURL = $"{geminiURL}?key={apiKey}";
|
||||
|
||||
|
||||
using (var request = new UnityWebRequest(requestURL, "POST"))
|
||||
{
|
||||
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
|
||||
@@ -56,15 +61,28 @@ namespace Hallucinate.AI
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
string rawResponse = request.downloadHandler.text;
|
||||
try
|
||||
{
|
||||
var response = JsonUtility.FromJson<GeminiResponse>(request.downloadHandler.text);
|
||||
if (response?.candidates != null && response.candidates.Length > 0)
|
||||
var response = JsonUtility.FromJson<GeminiResponse>(rawResponse);
|
||||
if (response != null &&
|
||||
response.candidates != null &&
|
||||
response.candidates.Length > 0 &&
|
||||
response.candidates[0].content != null &&
|
||||
response.candidates[0].content.parts != null &&
|
||||
response.candidates[0].content.parts.Length > 0)
|
||||
{
|
||||
onComplete?.Invoke(response.candidates[0].content.parts[0].text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[Gemini] Response structure invalid or blocked. Raw: {rawResponse}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}\nRaw Response: {rawResponse}");
|
||||
}
|
||||
catch (Exception e) { Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}"); }
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
23
Assets/Scripts/AI NPC/GeminiTest.cs
Normal file
23
Assets/Scripts/AI NPC/GeminiTest.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using UnityEngine;
|
||||
using Hallucinate.AI;
|
||||
|
||||
public class GeminiTest : MonoBehaviour
|
||||
{
|
||||
void Start()
|
||||
{
|
||||
Debug.Log("<color=cyan>[Gemini Test]</color> Bắt đầu kiểm tra kết nối API...");
|
||||
|
||||
if (GeminiService.Instance == null)
|
||||
{
|
||||
Debug.LogError("<color=red>[Gemini Test]</color> Không tìm thấy GeminiService Instance! Hãy đảm bảo bạn đã kéo script GeminiService vào một GameObject trong Scene.");
|
||||
return;
|
||||
}
|
||||
|
||||
string testPersona = "Bạn là một robot kiểm tra hệ thống.";
|
||||
string testPrompt = "Chào bạn, nếu bạn nhận được tin nhắn này, hãy trả lời: 'Kết nối Gemini thành công!'";
|
||||
|
||||
GeminiService.Instance.GetResponse(testPersona, testPrompt, (response) => {
|
||||
Debug.Log($"<color=green>[Gemini Test] Phản hồi từ API:</color> {response}");
|
||||
});
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/GeminiTest.cs.meta
Normal file
2
Assets/Scripts/AI NPC/GeminiTest.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e61a7aa4c1a936a43a97cf67a6e6a559
|
||||
@@ -12,4 +12,4 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 2f22f37f63bcec14080b11ce5e381ce6, type: 3}
|
||||
m_Name: vEditorStartupPrefs
|
||||
m_EditorClassIdentifier:
|
||||
displayWelcomeScreen: 1
|
||||
displayWelcomeScreen: 0
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user