All 3 attack combos play even if I only attack once

Hi!

I don’t know what I did wrong, I thought I followed everything properly. At the end of this lesson, when trying out what we did, I click only once on the attack button but all three animation (attack combos) are played one after the other.

Anyone has any idea what I could have done wrong?

Thanks.

Paste in your AttackingState.cs and we’ll take a look.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAttackingState : PlayerBaseState
{
float _previousFrameTime;

Attack _attack;
public PlayerAttackingState(PlayerStateMachine stateMachine, int attackIndex) : base(stateMachine)
{
    _attack = _stateMachine.Attacks[attackIndex];
}

public override void Enter()
{
    _stateMachine.Animator.CrossFadeInFixedTime(_attack.AnimationName, _attack.TransitionDuration);
}

public override void Tick(float deltaTime)
{
    Move(deltaTime);
    
    FaceTarget();
    
    float normalizedTime = GetNormalizedTime();
    
    if (normalizedTime > _previousFrameTime && normalizedTime < 1f)
    {
        TryComboAttack(normalizedTime);
    }
    else
    {
        //TODO: Go back to locomotion
    }
    
    _previousFrameTime = normalizedTime;
}



public override void Exit()
{

}

void TryComboAttack(float normalizedTime)
{
    if (_attack.ComboStateIndex == -1) { return; }
    
    if (normalizedTime < _attack.ComboAttackTime) { return; }
    
    _stateMachine.SwitchState(new PlayerAttackingState(_stateMachine, _attack.ComboStateIndex));
}

float GetNormalizedTime()
{
    AnimatorStateInfo currentInfo = _stateMachine.Animator.GetCurrentAnimatorStateInfo(0);
    AnimatorStateInfo nextInfo = _stateMachine.Animator.GetNextAnimatorStateInfo(0);

    if (_stateMachine.Animator.IsInTransition(0) && nextInfo.IsTag("Attack"))
    {
        return nextInfo.normalizedTime;;
    }
    else if (!_stateMachine.Animator.IsInTransition(0) && currentInfo.IsTag("Attack"))
    {
        return currentInfo.normalizedTime;
    }
    else
    {
        return 0;
    }
}

}

I fixed it! I forgot an if statement to surround the TryComboAttack(normalizedTime):

It needed to be like this:

if (normalizedTime > _previousFrameTime && normalizedTime < 1f)
{
if (_stateMachine.InputReader.IsAttacking)
{
TryComboAttack(normalizedTime);
}
}
else
{
//TODO: Go back to locomotion
}

Thank you! :grinning:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms