Animation gets shorter when the speed multiplier gets larger

I’m making a RPG with an “attack speed” stat which is added as a speed multiplier for the animation. When I then go in game the animation looks good when attack speed is 1.0 but when it I set it to 2.0 the animation gets faster but considerably shorter. I haven’t done a lot of animating so I suspect it can be in my animation handler code. Does anyone know where the problem might be?

Animation handler code:

void Update ()
    {
        //update variables
        UsingMagic = animator.GetBool("UseMagic");
        AttackDone = (!startedAnimation && animator.GetBool("Attack") && !animator.IsInTransition(0) &&
            animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= animator.GetCurrentAnimatorStateInfo(0).length);

        //manage animations
        if (AttackDone && !UsingMagic)
        {
            AttackReset();
        }
        //reset started animation variable
        if (animator.GetCurrentAnimatorClipInfo(0)[0].clip.name != "PlayerMelee" || animator.GetCurrentAnimatorClipInfo(0)[0].clip.name != "PlayerMagic")
        {
            startedAnimation = false;
        }

    }
/// <summary>
    /// Play attack animation using attack speed if melee
    /// </summary>
    /// <param name="attackSpeed">the speed of the melee animation</param>
    public void Attack (float attackSpeed)
    {
        if (!animator.IsInTransition(0) && !animator.GetBool("Attack") &&
            !animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerMelee") &&
            !animator.GetCurrentAnimatorStateInfo(0).IsName("PlayerMagic"))//don't interrupt
        {
            animator.SetBool("Attack", true);
            animator.SetFloat("AttackSpeed", attackSpeed);
            startedAnimation = true;
        }
    }

Attack code:

if (Input.GetButton("Fire1") && !UsingMagic && weaponEquiped != null)
        {
            animHandler.Attack(stats.AttackSpeed);
            //Debug.Log("MeleeAttack");
        }

I’m not sure what the problem is.

If you have an animation that lasts 2 seconds at normal speed, and you play it at twice the speed, then it will have to be half as long.

I mean the sword in this case does barely swings it looks like it just twitches, it doesn’t reach as far as it should. The time the animation takes is not what I meant, I mean the animation reach literaly gets shorter.

Makes more sense now.

It’s hard to tell from just looking at the code. Nothing in the code you’ve pasted jumps out as wrong.

Except maybe you should use GetButtonUp instead of GetButton, to avoid calling Attack multiple times per press.

I figured out the problem which was that I checked “>= animator.GetCurrentAnimatorStateInfo(0).length” instead of “>= animator.GetCurrentAnimatorClipInfo(0).Length”

1 Like