được sơ sơ
This commit is contained in:
@@ -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