IndexOutOfRangeException: Index was outside the bounds of the array

Hi, When I try to do all 3 combo’s I get this error in the console

and my attacks is setup as follows:
attacks

my code in PlayerAttackingState.cs is as follows

public class PlayerAttackingState : PlayerBaseState
{

    private float previousFrameTime;

    private bool alreadyAppliedForce;

    private Attack attack;

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

    public override void Enter()
    {
        stateMachine.Animator.CrossFadeInFixedTime(attack.AnimationName, 0.1f);
        
    }

    public override void Tick(float deltaTime)
    {
        Move(deltaTime);

        FaceTarget();

        float normalizedTime = GetNormalizedTime();

        if(normalizedTime >= previousFrameTime && normalizedTime < 1f)
        {
            if(normalizedTime >= attack.ForceTime)
            {
                TryApplyForce();
            }

            if(stateMachine.InputReader.isAttacking)
            {
                TryComboAttack(normalizedTime);
            }
        }
        else
        {
            if(stateMachine.Targeter.CurrentTarget != null)
            {
                stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
            }
            else
            {
                stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
            }
        }


        

        previousFrameTime = normalizedTime;
        
    }

   

    public override void Exit()
    {
        
    }

    private void TryComboAttack(float normalizedTime)
    {
        if (attack.ComboAttackTime == -1 ) { return; }

        if (normalizedTime < attack.ComboAttackTime) { return; }

        stateMachine.SwitchState
        (
           new PlayerAttackingState
           (
               stateMachine,
               attack.ComboStateIndex
           )
        );
    }

    private void TryApplyForce()
    {
        if (alreadyAppliedForce) { return; }
        stateMachine.ForceReceiver.AddForce(stateMachine.transform.forward * attack.Force);
        alreadyAppliedForce = true;
    }

    private 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 0f; 
        }
    }   

}

can anyone see what is causing this, and what I’ve overlooked, cause as I can see I’ve followed how the lecture code is

This line

        if (attack.ComboAttackTime == -1 ) { return; }

Should be

        if (attack.ComboStateIndex == -1 ) { return; }
1 Like

I think your issue is in this line in TryComboAttack
You should be testing to see if ComboStateIndex is -1

if (attack.ComboStateIndex == -1) {return;}
1 Like

lol, your right, did not see it…haha, freaking intelisence :sweat_smile:

thanks for the answers, I was looking for ages and feel I was blinded :joy:

It works now after changing

also not sure if I should post a new thread, but when we get the new environment in the Improving The Environment lecture, there is no player in the scene, but when I put my PlayerRig into the scene everything works except the follow camera. the camera just stays static where it’s placed in the scene. everything is as its on my other PlayerRig in the first scene, and I can move around, attack etc. but the Cinemachine camera just don’t seems to work as it should

Never mind the last post there. I forgot to add the CinemachineBrain component to the main camera. After i did that it works as it should.

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

Privacy & Terms