được sơ sơ
This commit is contained in:
4
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
4
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
@@ -6,8 +6,10 @@
|
||||
<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/Prefabs/NPC/xNPC.prefab" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/NPC/xNPC.prefab" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Shooter/Player/vShooterMelee_Inventory.prefab" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Shooter/Player/vShooterMelee_Inventory.prefab" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/AI NPC/AnimatorAI.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/AI NPC/AnimatorAI.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/KamikazeAI.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/AI NPC/KamikazeAI.cs" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
||||
@@ -511,10 +511,11 @@ MonoBehaviour:
|
||||
player: {fileID: 0}
|
||||
moveSpeed: 3
|
||||
rotateSpeed: 50
|
||||
patrolWaypoints: []
|
||||
currentWaypointIndex: 0
|
||||
patrolWaitTime: 2
|
||||
patrolSpeed: 2.5
|
||||
patrolRadius: 12
|
||||
playerHasArtifact: 0
|
||||
isAggroedBySound: 0
|
||||
laserPrefab: {fileID: 3965388737199864462, guid: fbec2b501d70daa4c9cb481ba53fc0b8, type: 3}
|
||||
firePoint: {fileID: 5863061020199015852}
|
||||
minShootDelay: 1
|
||||
@@ -522,6 +523,12 @@ MonoBehaviour:
|
||||
dodgeForce: 8
|
||||
dodgeDuration: 0.5
|
||||
dodgeCooldown: 3
|
||||
minStrafeDuration: 0.5
|
||||
maxStrafeDuration: 2.2
|
||||
maxSpreadAngle: 6
|
||||
burstInterval: 0.12
|
||||
approachWeight: 0.35
|
||||
minCombatDistance: 5
|
||||
npcName: Guard
|
||||
persona: You are a grumpy guard protecting gold.
|
||||
talkRange: 5
|
||||
@@ -609,20 +616,13 @@ MonoBehaviour:
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 7522161431095319480}
|
||||
m_Enabled: 0
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
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
|
||||
forceGrounded: 1
|
||||
movementBoost: 1.5
|
||||
dampTime: 0.1
|
||||
--- !u!136 &9027690817715396964
|
||||
CapsuleCollider:
|
||||
@@ -702,18 +702,18 @@ PrefabInstance:
|
||||
- target: {fileID: 9500000, guid: f32cd9f795c1282478d3bc3fbd8b2831, type: 3}
|
||||
propertyPath: m_Avatar
|
||||
value:
|
||||
objectReference: {fileID: 9000000, guid: 340f99b8334a14c4a8a01dce6e1e5348, type: 3}
|
||||
objectReference: {fileID: 9000000, guid: f32cd9f795c1282478d3bc3fbd8b2831, type: 3}
|
||||
- target: {fileID: 9500000, guid: f32cd9f795c1282478d3bc3fbd8b2831, type: 3}
|
||||
propertyPath: m_Controller
|
||||
value:
|
||||
objectReference: {fileID: 9100000, guid: 87885946b43e2d1449e1d5aa2042f8a8, type: 2}
|
||||
- target: {fileID: 9500000, guid: f32cd9f795c1282478d3bc3fbd8b2831, type: 3}
|
||||
propertyPath: m_UpdateMode
|
||||
value: 1
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 9500000, guid: f32cd9f795c1282478d3bc3fbd8b2831, type: 3}
|
||||
propertyPath: m_AnimatePhysics
|
||||
value: 1
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
m_RemovedComponents: []
|
||||
m_RemovedGameObjects: []
|
||||
|
||||
@@ -4197,6 +4197,11 @@ MonoBehaviour:
|
||||
_spawnParticle: 1
|
||||
_spawnStepMark: 1
|
||||
_useTriggerEnter: 1
|
||||
emitAINoise: 1
|
||||
aiNoiseRange: 10
|
||||
npcLayer:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
leftFootTrigger: {fileID: 11408204}
|
||||
rightFootTrigger: {fileID: 11477308}
|
||||
currentStep: {fileID: 0}
|
||||
|
||||
@@ -3,97 +3,86 @@ using UnityEngine.AI;
|
||||
using Invector;
|
||||
using Invector.vEventSystems;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// AnimatorAI: Advanced Invector-style Animator synchronization for EnemyAI.
|
||||
/// Resolves T-pose by ensuring all core Invector parameters are correctly synced.
|
||||
/// </summary>
|
||||
public class AnimatorAI : MonoBehaviour, vIAnimatorStateInfoController
|
||||
{
|
||||
protected Animator animator;
|
||||
protected EnemyAI enemyAI;
|
||||
protected NavMeshAgent agent;
|
||||
protected Rigidbody rb;
|
||||
protected vHealthController healthController;
|
||||
protected EnemyAI enemyAI;
|
||||
protected KamikazeAI kamikazeAI;
|
||||
|
||||
[Header("Movement Settings")]
|
||||
public float sprintThreshold = 0.8f;
|
||||
public float dampTime = 0.1f;
|
||||
public float groundDistanceValue = 0.05f;
|
||||
[Header("Force Settings")]
|
||||
public bool forceGrounded = true;
|
||||
public float movementBoost = 1.2f;
|
||||
public float dampTime = 0.1f;
|
||||
|
||||
public vAnimatorStateInfos animatorStateInfos { get; protected set; }
|
||||
|
||||
#region Animator Parameters (Invector Style)
|
||||
protected vAnimatorParameter isDead;
|
||||
protected vAnimatorParameter isGrounded;
|
||||
protected vAnimatorParameter isStrafing;
|
||||
protected vAnimatorParameter isSprinting;
|
||||
protected vAnimatorParameter isAiming;
|
||||
protected vAnimatorParameter verticalVelocity;
|
||||
protected vAnimatorParameter horizontalVelocity;
|
||||
protected vAnimatorParameter groundDistance;
|
||||
protected vAnimatorParameter moveSet_ID;
|
||||
protected vAnimatorParameter attackID;
|
||||
protected vAnimatorParameter hitDirection;
|
||||
protected vAnimatorParameter reactionID;
|
||||
protected vAnimatorParameter triggerReaction;
|
||||
protected vAnimatorParameter resetState;
|
||||
#region Animator Parameters
|
||||
protected vAnimatorParameter isDead, isGrounded, isStrafing, isSprinting, isAiming;
|
||||
protected vAnimatorParameter verticalVelocity, horizontalVelocity, inputMagnitude;
|
||||
protected vAnimatorParameter groundDistance, moveSet_ID, attackID, triggerReaction, resetState;
|
||||
#endregion
|
||||
|
||||
protected Vector3 lastPosition;
|
||||
protected float currentV;
|
||||
protected float currentH;
|
||||
protected float currentV, currentH, currentMagnitude, calculatedSpeed;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
animator = GetComponentInChildren<Animator>();
|
||||
enemyAI = GetComponent<EnemyAI>();
|
||||
if (animator == null) animator = GetComponentInParent<Animator>();
|
||||
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
healthController = GetComponent<vHealthController>();
|
||||
if (agent == null) agent = GetComponentInParent<NavMeshAgent>();
|
||||
|
||||
healthController = GetComponentInChildren<vHealthController>();
|
||||
enemyAI = GetComponent<EnemyAI>();
|
||||
kamikazeAI = GetComponent<KamikazeAI>();
|
||||
|
||||
if (animator)
|
||||
{
|
||||
animator.applyRootMotion = false;
|
||||
animator.updateMode = AnimatorUpdateMode.Normal;
|
||||
|
||||
// Reset all layers initially to prevent T-Pose
|
||||
for (int i = 1; i < animator.layerCount; i++) animator.SetLayerWeight(i, 0f);
|
||||
|
||||
animatorStateInfos = new vAnimatorStateInfos(animator);
|
||||
InitializeParameters();
|
||||
Debug.Log($"<color=green>[AnimSystem]</color> Đã kích hoạt trên {gameObject.name}");
|
||||
}
|
||||
|
||||
lastPosition = transform.position;
|
||||
}
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
this.Register();
|
||||
if (healthController) healthController.onReceiveDamage.AddListener(OnReceiveDamage);
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
this.UnRegister();
|
||||
if (healthController) healthController.onReceiveDamage.RemoveListener(OnReceiveDamage);
|
||||
}
|
||||
protected virtual void OnEnable() { this.Register(); }
|
||||
protected virtual void OnDisable() { this.UnRegister(); }
|
||||
|
||||
protected virtual void InitializeParameters()
|
||||
{
|
||||
isDead = new vAnimatorParameter(animator, "isDead");
|
||||
isGrounded = new vAnimatorParameter(animator, "IsGrounded");
|
||||
isStrafing = new vAnimatorParameter(animator, "IsStrafing");
|
||||
isSprinting = new vAnimatorParameter(animator, "IsSprinting");
|
||||
isAiming = new vAnimatorParameter(animator, "IsAiming");
|
||||
verticalVelocity = new vAnimatorParameter(animator, "VerticalVelocity");
|
||||
horizontalVelocity = new vAnimatorParameter(animator, "HorizontalVelocity");
|
||||
groundDistance = new vAnimatorParameter(animator, "GroundDistance");
|
||||
moveSet_ID = new vAnimatorParameter(animator, "MoveSet_ID");
|
||||
attackID = new vAnimatorParameter(animator, "AttackID");
|
||||
hitDirection = new vAnimatorParameter(animator, "HitDirection");
|
||||
reactionID = new vAnimatorParameter(animator, "ReactionID");
|
||||
triggerReaction = new vAnimatorParameter(animator, "TriggerReaction");
|
||||
resetState = new vAnimatorParameter(animator, "ResetState");
|
||||
isDead = ValidateAndInit("isDead");
|
||||
isGrounded = ValidateAndInit("isGrounded");
|
||||
if (!isGrounded.isValid) isGrounded = ValidateAndInit("IsGrounded");
|
||||
isStrafing = ValidateAndInit("IsStrafing");
|
||||
isSprinting = ValidateAndInit("IsSprinting");
|
||||
isAiming = ValidateAndInit("IsAiming");
|
||||
verticalVelocity = ValidateAndInit("InputVertical");
|
||||
horizontalVelocity = ValidateAndInit("InputHorizontal");
|
||||
inputMagnitude = ValidateAndInit("InputMagnitude");
|
||||
groundDistance = ValidateAndInit("GroundDistance");
|
||||
moveSet_ID = ValidateAndInit("MoveSet_ID");
|
||||
attackID = ValidateAndInit("AttackID");
|
||||
triggerReaction = ValidateAndInit("TriggerReaction");
|
||||
resetState = ValidateAndInit("ResetState");
|
||||
}
|
||||
|
||||
private vAnimatorParameter ValidateAndInit(string pName) => new vAnimatorParameter(animator, pName);
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (animator == null || enemyAI == null || agent == null) return;
|
||||
if (animator == null || agent == null) return;
|
||||
|
||||
UpdateMovementParameters();
|
||||
UpdateCombatParameters();
|
||||
@@ -101,106 +90,74 @@ public class AnimatorAI : MonoBehaviour, vIAnimatorStateInfoController
|
||||
|
||||
protected virtual void UpdateMovementParameters()
|
||||
{
|
||||
// 1. Grounded & GroundDistance (Critical for T-pose prevention)
|
||||
bool grounded = agent.isOnNavMesh || agent.enabled;
|
||||
SetBool(isGrounded, grounded);
|
||||
SetFloat(groundDistance, grounded ? 0f : groundDistanceValue);
|
||||
SetBool(isGrounded, forceGrounded);
|
||||
SetFloat(groundDistance, 0f);
|
||||
|
||||
// 2. Responsive Velocity Calculation
|
||||
// Use a mix of agent velocity and position delta for better responsiveness
|
||||
Vector3 worldVelocity = (transform.position - lastPosition) / Time.deltaTime;
|
||||
Vector3 delta = transform.position - lastPosition;
|
||||
calculatedSpeed = delta.magnitude / Time.deltaTime;
|
||||
lastPosition = transform.position;
|
||||
|
||||
if (worldVelocity.magnitude < 0.01f) worldVelocity = Vector3.zero;
|
||||
|
||||
Vector3 localVelocity = transform.InverseTransformDirection(worldVelocity);
|
||||
Vector3 localVel = transform.InverseTransformDirection(delta / Time.deltaTime);
|
||||
|
||||
float targetV = localVelocity.z / enemyAI.moveSpeed;
|
||||
float targetH = localVelocity.x / enemyAI.moveSpeed;
|
||||
float maxS = (enemyAI) ? enemyAI.moveSpeed : (kamikazeAI ? agent.speed : 3f);
|
||||
if (maxS <= 0) maxS = 3f;
|
||||
|
||||
float targetV = (localVel.z / maxS) * movementBoost;
|
||||
float targetH = (localVel.x / maxS) * movementBoost;
|
||||
|
||||
// Smooth velocity values
|
||||
currentV = Mathf.Lerp(currentV, targetV, 10f * Time.deltaTime);
|
||||
currentH = Mathf.Lerp(currentH, targetH, 10f * Time.deltaTime);
|
||||
currentMagnitude = new Vector2(currentH, currentV).magnitude;
|
||||
|
||||
// ÉP GIÁ TRỊ VÀO ANIMATOR
|
||||
SetFloat(verticalVelocity, currentV);
|
||||
SetFloat(horizontalVelocity, currentH);
|
||||
|
||||
// 3. Sprinting
|
||||
bool sprinting = worldVelocity.magnitude > (enemyAI.moveSpeed * sprintThreshold);
|
||||
SetBool(isSprinting, sprinting);
|
||||
|
||||
// 4. Strafing
|
||||
SetBool(isStrafing, enemyAI.playerHasArtifact);
|
||||
SetFloat(inputMagnitude, currentMagnitude);
|
||||
}
|
||||
|
||||
protected virtual void UpdateCombatParameters()
|
||||
{
|
||||
SetBool(isAiming, enemyAI.playerHasArtifact);
|
||||
SetInt(moveSet_ID, enemyAI.playerHasArtifact ? 1 : 0);
|
||||
// 1. Kiểm tra trạng thái AI
|
||||
bool isShooting = (enemyAI && enemyAI.IsShootingBurst);
|
||||
bool hasArtifact = (enemyAI && enemyAI.playerHasArtifact);
|
||||
|
||||
// Shooting burst
|
||||
if (enemyAI.IsShootingBurst)
|
||||
SetInt(attackID, 1);
|
||||
// 2. Cập nhật MoveSet và Aiming
|
||||
SetInt(moveSet_ID, hasArtifact ? 1 : 0);
|
||||
SetBool(isAiming, hasArtifact);
|
||||
SetBool(isStrafing, hasArtifact);
|
||||
|
||||
// 3. Xử lý BẮN SÚNG (Layer 6 trong Animator của bạn)
|
||||
if (isShooting)
|
||||
{
|
||||
// Bật Layer bắn súng lên 1 (Smooth)
|
||||
animator.SetLayerWeight(6, Mathf.Lerp(animator.GetLayerWeight(6), 1f, 15f * Time.deltaTime));
|
||||
SetInt(attackID, 1); // Kích hoạt animation bắn trong Blend Tree của Layer 6
|
||||
}
|
||||
else
|
||||
{
|
||||
// Tắt Layer bắn súng về 0
|
||||
animator.SetLayerWeight(6, Mathf.Lerp(animator.GetLayerWeight(6), 0f, 10f * Time.deltaTime));
|
||||
SetInt(attackID, 0);
|
||||
|
||||
// Dodge logic
|
||||
if (enemyAI.IsDodging)
|
||||
{
|
||||
// In Invector, dodges are often handled via triggers or specific IDs
|
||||
SetAnimatorTrigger(triggerReaction);
|
||||
}
|
||||
|
||||
// Death state
|
||||
if (healthController) SetBool(isDead, healthController.isDead);
|
||||
}
|
||||
|
||||
protected virtual void OnReceiveDamage(vDamage damage)
|
||||
{
|
||||
if (animator == null || !animator.enabled) return;
|
||||
|
||||
// Sync damage parameters for hit reactions
|
||||
if (hitDirection.isValid && damage.sender)
|
||||
{
|
||||
float angle = transform.HitAngle(damage.sender.position);
|
||||
animator.SetInteger(hitDirection, (int)angle);
|
||||
}
|
||||
|
||||
if (reactionID.isValid) animator.SetInteger(reactionID, damage.reaction_id);
|
||||
|
||||
SetAnimatorTrigger(triggerReaction);
|
||||
SetAnimatorTrigger(resetState);
|
||||
if (enemyAI && enemyAI.IsDodging) SetAnimatorTrigger(triggerReaction);
|
||||
}
|
||||
|
||||
#region Helpers
|
||||
protected void SetBool(vAnimatorParameter param, bool value)
|
||||
{
|
||||
if (param.isValid && animator.GetBool(param) != value)
|
||||
animator.SetBool(param, value);
|
||||
protected void SetBool(vAnimatorParameter p, bool v) { if (p.isValid) animator.SetBool(p, v); }
|
||||
protected void SetFloat(vAnimatorParameter p, float v) { if (p.isValid) animator.SetFloat(p, v, dampTime, Time.deltaTime); }
|
||||
protected void SetInt(vAnimatorParameter p, int v) { if (p.isValid) animator.SetInteger(p, v); }
|
||||
|
||||
public void SetAnimatorTrigger(vAnimatorParameter trigger)
|
||||
{
|
||||
if (trigger.isValid) StartCoroutine(SetTriggerRoutine(trigger));
|
||||
}
|
||||
|
||||
protected void SetFloat(vAnimatorParameter param, float value)
|
||||
{
|
||||
if (param.isValid)
|
||||
animator.SetFloat(param, value, dampTime, Time.deltaTime);
|
||||
}
|
||||
|
||||
protected void SetInt(vAnimatorParameter param, int value)
|
||||
{
|
||||
if (param.isValid && animator.GetInteger(param) != value)
|
||||
animator.SetInteger(param, value);
|
||||
}
|
||||
|
||||
public void SetAnimatorTrigger(vAnimatorParameter trigger)
|
||||
{
|
||||
if (trigger.isValid) StartCoroutine(SetTriggerRoutine(trigger));
|
||||
}
|
||||
|
||||
private IEnumerator SetTriggerRoutine(int targetHash)
|
||||
{
|
||||
animator.SetTrigger(targetHash);
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
animator.ResetTrigger(targetHash);
|
||||
|
||||
private IEnumerator SetTriggerRoutine(int targetHash)
|
||||
{
|
||||
animator.SetTrigger(targetHash);
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
animator.ResetTrigger(targetHash);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ public class KamikazeAI : MonoBehaviour
|
||||
// HÀM TUẦN TRA NGẪU NHIÊN MỚI
|
||||
private NodeState ActionRandomPatrol()
|
||||
{
|
||||
Debug.Log("Wandering randomly...");
|
||||
// Debug.Log("Wandering randomly...");
|
||||
agent.isStopped = false;
|
||||
agent.speed = patrolSpeed;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user