Forcing an animation transition to happen on runtime

Guys, I need help. I coded this method for animation, and it works perfectly, except in one point: I have an Animator controller with an empty state and a spell casting animation, and it transitions between the spell casting animation and itself. now, when I press 1, the animation starts, (here the input normalized is just a vector2 that stores the front-back, left-right inputs), if I stand still it instantiates, if I move it does not, but, althou the animation starts, it does not end when I move. I tried using only the isCasting bool, and then I tried using the bool and a trigger, but it does not. I want the animation to transition to the empty state as soon as isCasting = false. Please, help, I am desperate

void CastSpell(){
        if(Input.GetKeyDown("1"))
        {
            if(inputNormalized != Vector2.zero)
            {
            return;
            }
            castTimer.Duration = spellcast.length -2;
            castTimer.Run();
           
            isCasting = true;
        }
            if(inputNormalized != Vector2.zero){
                isCasting = false;
                anim.SetTrigger("StopCast");
            }
        if(castTimer.Finished && isCasting){
            isCasting = false;
            Projectile firebal = Instantiate(spell.spellbolt, transform.position+transform.forward*2 +Vector3.up*1.5f, transform.rotation);
           
            firebal.target = target;
        }
        anim.SetBool("Spell", isCasting);
    }

You need to set up the transition in your Animator state machine to not have an exit time if you want the state to change immediately.

2 Likes

THANK YOU! Sometimes small details can go unoticed