lot's of fixes, fixed animation retargetting, fixed FSM

This commit is contained in:
2025-07-24 17:19:42 +02:00
parent 802eb5ac1d
commit d4d01dfb69
27 changed files with 4381 additions and 2990 deletions

View File

@@ -1,4 +1,6 @@
using System;
// Paste this code into your existing bMeleeAttackControl.cs file, replacing its content.
using System;
using System.Collections;
using System.Collections.Generic;
using Beyond; // For Player, GameStateManager, TimeController
@@ -39,42 +41,64 @@ namespace Invector.vMelee
public bool resetAttackTrigger;
[Tooltip("Normalized time point to reset attack triggers if resetAttackTrigger is true.")]
public float resetTriggerBeforeTime = 0.5f;
// --- NEW ---: Combo Rotation and Position Lerp Settings
[Header("Combo & Movement")]
[Tooltip("Normalized time to unlock rotation, allowing the player to aim the next attack in a combo. Set to 1 to disable.")]
[Range(0,1)]
public float unlockRotationTime = 0.7f;
[Tooltip("Enable to make the character move towards the target during the attack.")]
public bool lerpPositionTowardsTarget = false;
[vHideInInspector("lerpPositionTowardsTarget")]
[Tooltip("Max distance from the target to start moving towards it.")]
public float maxLerpDistance = 3.5f;
[vHideInInspector("lerpPositionTowardsTarget")]
[Tooltip("How fast the character moves towards the target.")]
public float positionLerpSpeed = 2.0f;
[vHideInInspector("lerpPositionTowardsTarget")]
[Tooltip("How close the character should get to the target.")]
public float stoppingDistance = 1.2f;
// --- END NEW ---
[Header("Slow Motion Settings")]
[Tooltip("Enable slow motion effect during this attack based on conditions below.")]
public bool useAttackTimeScale = false;
[Tooltip("Distance within which the current auto-target must be for slow motion to consider activating. Analogous to slowMoActivationDistance in vMeleeAttackControl.")]
public float maxTargetDistance = 3f; // This will be used as slowMoActivationDistance
[Tooltip("Target health threshold below which slow motion might activate (if near).")]
public float maxTargetDistance = 3f;
public float lowHealthTh = 10f;
[Tooltip("Time scale to apply during slow motion.")]
public float attackTimeScale = 0.2f;
[Tooltip("Normalized time to start the slow motion window. If < 0, uses 'startDamage'.")]
public float attackTimeScaleStart = -1f;
[Tooltip("Normalized time to end the slow motion window. If < 0, uses 'endDamage'.")]
public float attackTimeScaleEnd = -1f;
[Header("Rotation Settings")]
[Tooltip("If true, the character will attempt to rotate towards the auto-target during this attack state.")]
public bool rotatePlayerTowardsTarget;
[Tooltip("The angle (in degrees from player's forward) within which the auto-target must be for rotation to engage. Analogous to rotationActivationAngle in vMeleeAttackControl.")]
public float degreeThreshold = 20f; // This will be used as rotationActivationAngle
// rotationSpeed field is now unused, AutoTargetting.playerRotationSpeed will be used.
// maxTurnTowardDistance field is now unused.
public float degreeThreshold = 20f;
[Header("Debug")]
public bool debug;
// Private state variables
private bool isActive; // Is damage window active
private bool isActive;
private vIAttackListener mFighter;
private bool isAttacking; // Is this attack state logic considered "attacking"
private bool m_hasScaledTime; // Has slow motion been activated in this instance of the state
private bool isAttacking;
private bool m_hasScaledTime;
private AutoTargetting _autoTargettingInstance;
// --- NEW ---: Private variables for new features
private bThirdPersonController _characterController;
private bool _isRotationLockedByThis; // Tracks if this specific state has locked rotation
// --- END NEW ---
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
mFighter = animator.GetComponent<vIAttackListener>();
// --- NEW ---: Get reference to the character controller
_characterController = animator.GetComponent<bThirdPersonController>();
// --- END NEW ---
if (Player.Instance != null)
{
@@ -83,12 +107,21 @@ namespace Invector.vMelee
if (_autoTargettingInstance == null && debug)
{
Debug.LogWarning($"({damageType}) AutoTargetting instance not found via Player.Instance.AutoTarget on {animator.name}. Rotation and target-dependent slow-mo will be limited.");
Debug.LogWarning($"({damageType}) AutoTargetting instance not found on {animator.name}. Rotation and target-dependent features will be limited.");
}
isAttacking = true;
isActive = false;
m_hasScaledTime = false;
// --- NEW ---: Lock character rotation at the beginning of the attack
if (_characterController != null)
{
_characterController.lockRotation = true;
_isRotationLockedByThis = true;
if (debug) Debug.Log($"({damageType}) Rotation locked by state.");
}
// --- END NEW ---
if (mFighter != null)
mFighter.OnEnableAttack();
@@ -102,24 +135,33 @@ namespace Invector.vMelee
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (Player.Instance.ActiveWeaponTrail) // Existing weapon trail logic
if (Player.Instance.ActiveWeaponTrail)
{
Player.Instance.ActiveWeaponTrail.m_colorMultiplier = Color.white + Color.red * damageMultiplier;
}
float currentNormalizedTime = stateInfo.normalizedTime % 1;
if (currentNormalizedTime == 0 && stateInfo.normalizedTime > 0.5f) currentNormalizedTime = 1f;
// --- MODIFIED ---: Rotation and Movement logic is now more sophisticated
// Only execute script-based rotation if the controller's rotation is locked
if (_characterController != null && _characterController.lockRotation)
{
AttemptRotationTowardsAutoTarget(animator);
}
// Handle position lerping
AttemptPositionLerp(animator);
// --- ROTATION LOGIC ---
AttemptRotationTowardsAutoTarget(animator);
// Handle unlocking rotation for combo aiming
UpdateRotationLock(currentNormalizedTime);
// --- END MODIFIED ---
// --- SLOW MOTION LOGIC ---
if (useAttackTimeScale)
{
UpdateSlowMotion(animator, stateInfo, currentNormalizedTime);
}
// --- DAMAGE WINDOW LOGIC ---
if (!isActive && currentNormalizedTime >= startDamage && currentNormalizedTime <= endDamage)
{
if (debug) Debug.Log($"({damageType}) Enable Damage: normTime={currentNormalizedTime:F2} (Start:{startDamage:F2}, End:{endDamage:F2})");
@@ -131,13 +173,8 @@ namespace Invector.vMelee
if (debug) Debug.Log($"({damageType}) Disable Damage: normTime={currentNormalizedTime:F2} > {endDamage:F2}");
isActive = false;
ActiveDamage(animator, false);
// Note: Original bMeleeAttackControl reset m_hasScaledTime here.
// vMeleeAttackControl resets it if currentNormalizedTime > attackTimeScaleEnd.
// TimeController handles restoring time, so m_hasScaledTime is more about preventing re-triggering within one state.
// It's reset in UpdateSlowMotion or OnStateExit.
}
// --- ATTACK STATE AND TRIGGER RESET LOGIC ---
if (isAttacking)
{
if (currentNormalizedTime > endDamage)
@@ -146,15 +183,10 @@ namespace Invector.vMelee
isAttacking = false;
if (debug) Debug.Log($"({damageType}) OnDisableAttack called: normTime={currentNormalizedTime:F2}");
}
// Original bMelee had: "if (nTime > .1f && nTime < resetTriggerBeforeTime && isAttacking)"
// vMelee has: "else if (resetAttackTrigger && currentNormalizedTime >= resetTriggerBeforeTime)"
// Adopting vMelee's more standard approach for early reset:
else if (resetAttackTrigger && currentNormalizedTime >= resetTriggerBeforeTime)
{
if (mFighter != null) mFighter.ResetAttackTriggers();
if (debug) Debug.Log($"({damageType}) ResetAttackTriggers called: normTime={currentNormalizedTime:F2}");
// To prevent multiple calls, ideally ResetAttackTriggers is idempotent or use a flag.
// For now, matching vMelee's potential for multiple calls if time hovers.
}
}
}
@@ -177,13 +209,22 @@ namespace Invector.vMelee
}
isAttacking = false;
m_hasScaledTime = false; // Reset slow motion flag
m_hasScaledTime = false;
if (mFighter != null && resetAttackTrigger) // Final reset if configured
if (mFighter != null && resetAttackTrigger)
{
mFighter.ResetAttackTriggers();
if (debug) Debug.Log($"({damageType}) ResetAttackTriggers called on StateExit due to resetAttackTrigger flag.");
}
// --- NEW ---: Ensure rotation is unlocked upon exiting the state
if (_characterController != null && _isRotationLockedByThis)
{
_characterController.lockRotation = false;
_isRotationLockedByThis = false;
if (debug) Debug.Log($"({damageType}) Rotation unlocked by state on exit.");
}
// --- END NEW ---
}
private void AttemptRotationTowardsAutoTarget(Animator animator)
@@ -193,19 +234,54 @@ namespace Invector.vMelee
return;
}
// Using bMeleeAttackControl's degreeThreshold as the activation angle
if (_autoTargettingInstance.IsTargetInAngle(animator.transform, _autoTargettingInstance.CurrentTarget, degreeThreshold))
{
// AutoTargetting.playerRotationSpeed will be used internally by this call
_autoTargettingInstance.ExecuteRotationTowardsCurrentTarget(Time.deltaTime);
}
}
// --- NEW ---
private void UpdateRotationLock(float normalizedTime)
{
if (_characterController != null && _isRotationLockedByThis && normalizedTime >= unlockRotationTime)
{
_characterController.lockRotation = false;
_isRotationLockedByThis = false; // Stop this state from managing the lock
if(debug) Debug.Log($"({damageType}) Rotation unlocked for combo aiming at normTime={normalizedTime:F2}");
}
}
private void AttemptPositionLerp(Animator animator)
{
if (!lerpPositionTowardsTarget || _characterController == null || _autoTargettingInstance == null || _autoTargettingInstance.CurrentTarget == null)
{
return;
}
Transform playerTransform = _characterController.transform;
Transform targetTransform = _autoTargettingInstance.CurrentTarget.transform;
float distance = Vector3.Distance(playerTransform.position, targetTransform.position);
// Only lerp if within max distance and further than stopping distance
if (distance <= maxLerpDistance && distance > stoppingDistance)
{
Vector3 directionToTarget = (targetTransform.position - playerTransform.position).normalized;
directionToTarget.y = 0; // Keep movement on the horizontal plane
// The target position is a point in front of the enemy at the stopping distance
Vector3 targetPosition = targetTransform.position - directionToTarget * stoppingDistance;
// Use MoveTowards for consistent speed. This adds to root motion rather than fighting it.
playerTransform.position = Vector3.MoveTowards(playerTransform.position, targetPosition, positionLerpSpeed * Time.deltaTime);
}
}
// --- END NEW ---
private void UpdateSlowMotion(Animator animator, AnimatorStateInfo stateInfo, float currentNormalizedTime)
{
// This method is called only if useAttackTimeScale (field) is true.
// ... (this method remains unchanged)
if (_autoTargettingInstance == null || TimeController.Instance == null) return;
if (!m_hasScaledTime)
{
if (currentNormalizedTime >= attackTimeScaleStart && currentNormalizedTime <= attackTimeScaleEnd)
@@ -213,65 +289,45 @@ namespace Invector.vMelee
bool triggerSlowMo = false;
if (_autoTargettingInstance.CurrentTarget != null)
{
// Use bMeleeAttackControl's maxTargetDistance as the activation distance
float distSqr = (_autoTargettingInstance.CurrentTarget.transform.position - animator.transform.position).sqrMagnitude;
bool targetNear = distSqr <= maxTargetDistance * maxTargetDistance;
if (targetNear)
{
// Mimicking vMeleeAttackControl's effective logic:
// If useAttackTimeScale (field) is true (which it is to get here) and target is near, then trigger.
// The lowHealthTh can act as an additional, prioritized condition if desired,
// but with current structure, `this.useAttackTimeScale` being true makes the second part of OR true.
float currentTargetHealth = _autoTargettingInstance.GetCurrentTargetHealth();
bool targetHealthLow = currentTargetHealth > 0f && currentTargetHealth < lowHealthTh;
if (targetHealthLow) // Prioritize if health is low and near
if (targetHealthLow)
{
triggerSlowMo = true;
}
// else if (this.useAttackTimeScale) // This refers to the field, which is true if we are in this function.
// So if target is Near, this path will be taken if not already low health.
// Simplified: if targetNear, triggerSlowMo = true because this.useAttackTimeScale is already true.
// The following `else if` is essentially `else if (true)`
else
{
triggerSlowMo = true; // General case: near and useAttackTimeScale is on
triggerSlowMo = true;
}
}
}
// else: No current target, so no target-dependent slow motion.
if (triggerSlowMo)
{
// Use vMeleeAttackControl's duration calculation for SetTimeScaleForSec
float slowMoEffectDuration = (attackTimeScaleEnd - currentNormalizedTime) * stateInfo.length;
if (slowMoEffectDuration > 0.01f) // Ensure a meaningful duration
if (slowMoEffectDuration > 0.01f)
{
// The 'true' for forceUnique in bMelee's original TimeController call.
// Assuming TimeController.Instance.SetTimeScaleForSec now handles this or has an overload.
// If SetTimeScaleForSec(scale, duration, bool forceUnique) exists:
// TimeController.Instance.SetTimeScaleForSec(attackTimeScale, slowMoEffectDuration, true);
// If not, use the existing TimeController method. For now, assuming vMelee's version:
TimeController.Instance.SetTimeScaleForSec(attackTimeScale, slowMoEffectDuration);
if (debug) Debug.Log($"({damageType}) Slow-mo ACTIVATED. Target: {_autoTargettingInstance.CurrentTarget?.name ?? "N/A"}. Duration: {slowMoEffectDuration:F2}s. NormTime: {currentNormalizedTime:F2}");
}
else if (debug) Debug.Log($"({damageType}) Slow-mo trigger met, but calculated duration too short ({slowMoEffectDuration:F2}s). NormTime: {currentNormalizedTime:F2}");
m_hasScaledTime = true;
}
}
}
else if (currentNormalizedTime > attackTimeScaleEnd && m_hasScaledTime) // If slow-mo was active and window has passed
else if (currentNormalizedTime > attackTimeScaleEnd && m_hasScaledTime)
{
m_hasScaledTime = false;
if (debug) Debug.Log($"({damageType}) Slow-mo window ended (normTime={currentNormalizedTime:F2}). m_hasScaledTime reset.");
// TimeController.Instance is responsible for restoring time scale.
}
}
void ActiveDamage(Animator animator, bool value)
{
// ... (this method remains unchanged)
var meleeManager = animator.GetComponent<vMeleeManager>();
if (meleeManager)
{