I'm unsure how to Debug and fix this. I have a MeleeState that allows the player to do a combo. After any of the attack animations _stateComplete = true which allows exiting out of MeleeState. So I can do a full combo of 3 attacks and the Debug will fire _stateComplete = true once. This is correct.
If I button mash the attack button to enter into MeleeState then after between 25-50 button presses eventually _stateComplete = true fires twice. What this looks like is the player begins to do the first attack animation but because _stateComplete = true it exits out of MeleeState immediately which goes to IdleState - looks glitchy.
public class MeleeState : PlayerState {
private int _attackIndex;
private bool _stateComplete;
public MeleeState(PlayerSM sm) : base(sm) {}
public override void Enter() {
_attackIndex = -1;
_attackIndex++;
_anim = _animancer.Play(_melee.Attacks[_attackIndex]);
_anim.Events.OnEnd += OnAnimationEnd;
}
public override void Logic() {
TryCombo();
Debug.Log(_stateComplete);
}
public override void Exit() {
_stateComplete = false;
_attackIndex = -1;
}
private void TryCombo() {
float t = _anim.NormalizedTime;
if (_input.MeleeInput.WasPerformedThisFrame()) {
if ((t >= _melee.ComboStart && t <= _melee.ComboEnd)) {
_attackIndex = (_attackIndex + 1) % _melee.Attacks.Count;
_anim = _animancer.Play(_melee.Attacks[_attackIndex]);
_anim.Events.OnEnd += OnAnimationEnd;
}
}
}
private void OnAnimationEnd() => _stateComplete = true;
public override bool CanExitState() => _stateComplete;
public override bool CanEnterState() =>
_input.MeleeInput.WasPerformedThisFrame();
}