autotargetting fix

This commit is contained in:
2025-09-14 08:08:41 +02:00
parent fe6f768072
commit 7dab87f5ea
3 changed files with 27 additions and 28 deletions

View File

@@ -169,8 +169,7 @@ namespace Beyond
yield return new WaitForSeconds(targetingInterval);
}
}
// REFACTORED: This method now has a clearer, more robust flow.
private void UpdateTarget()
{
if (_playerTransform == null || _gameStateManager == null || _manualSwitchCooldownActive) return;
@@ -205,8 +204,7 @@ namespace Beyond
// Step 3: Every update, evaluate and apply the correct lock-on state for the current target.
UpdateLockOnState();
}
// NEW: This method exclusively handles the logic for locking on and off.
private void UpdateLockOnState()
{
if (targetLockSystem == null || _playerTransform == null) return;
@@ -216,8 +214,7 @@ namespace Beyond
if (CurrentTarget != null && (autoLockSelectedTarget || alwaysLockOnInCombat))
{
float distanceToTarget = Vector3.Distance(_playerTransform.position, CurrentTarget.transform.position);
// This is a hysteresis check: use different distances for locking and unlocking to prevent flickering.
if (targetLockSystem.isLockingOn)
{
// If already locked, stay locked unless we are beyond the unlock threshold.
@@ -233,17 +230,16 @@ namespace Beyond
// Synchronize the desired state with the lock-on system.
Transform desiredLockTarget = shouldBeLocked ? CurrentTarget.transform : null;
// The lock-on system should be smart enough to not do anything if the target hasn't changed.
// We send the desired target (or null) every update.
targetLockSystem.ManuallySetLockOnTarget(desiredLockTarget, true);
// --- THIS IS THE FIX ---
// We now pass the 'shouldBeLocked' boolean to tell the system whether to lock or unlock.
targetLockSystem.ManuallySetLockOnTarget(desiredLockTarget, shouldBeLocked);
if (alwaysLockOnInCombat && desiredLockTarget != null && !targetLockSystem.isLockingOn)
{
targetLockSystem.SetLockOn(true);
}
}
// SIMPLIFIED: This method now only handles changing the CurrentTarget reference and its visual highlight.
private void SetNewTarget(vFSMBehaviourController newTarget, bool forceLockSystemUpdate = false)
{
if (CurrentTarget == newTarget) return;
@@ -309,9 +305,6 @@ namespace Beyond
if (targetLockSystem == null) return;
_manualSwitchCooldownActive = true;
_manualSwitchCooldownTimer = manualSwitchCooldownDuration;
// After manually unlocking, we let the main UpdateTarget loop find the next best candidate.
// If the previously locked target is still the closest, it will be re-highlighted automatically.
}
#endregion
@@ -347,23 +340,15 @@ namespace Beyond
public void ClearTarget(bool findNewOneImmediately)
{
// --- FIX APPLIED HERE ---
// 1. Explicitly tell the lock-on system to unlock immediately.
// This is the crucial step that resets the 'isLockingOn' flag to false.
if (targetLockSystem != null)
{
targetLockSystem.SetLockOn(false);
}
// 2. Deselect the current target and remove its highlight.
SetNewTarget(null);
// 3. If requested, immediately find a new target.
if (findNewOneImmediately && _gameStateManager != null && _gameStateManager.CurrentState == GameStateManager.State.COMBAT)
{
// Now, when UpdateTarget() runs, 'isLockingOn' will be correctly set to false,
// forcing the system to use the stricter 'autoLockOnDistance' for the new candidate.
if (!_manualSwitchCooldownActive)
{
UpdateTarget();