Search Unity

Best method to resume an animation from where it was interrupted

Discussion in 'Animation' started by Frencich, Mar 8, 2022.

  1. Frencich

    Frencich

    Joined:
    Mar 20, 2018
    Posts:
    1
    Hi all!

    I hope you can help me with this. I have 2 animations: Idle and Attack. When the Attack Animation starts i want save the Idle animation's state so i can resume it after the Attack animation ends.
    To do this, i wrote this code (and it actually works):

        {
    Animator anim;
    AnimatorStateInfo stateInfo;

    float tempNormTime;
    float prevTime;


    private void Awake()
    {
    anim = gameObject.GetComponent<Animator>();
    }

    private void Update()
    {
    if (anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
    {
    stateInfo = anim.GetCurrentAnimatorStateInfo(0);

    tempNormTime = stateInfo.normalizedTime;
    prevTime = tempNormTime - (float)System.Math.Truncate(tempNormTime);
    }

    }

    public void changeAnimation()
    {
    if (anim.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
    {
    anim.Play("Attack");
    }
    else
    {
    anim.Play("Idle", 0, prevTime);
    }
    }
    }




    Using this method, however, i can't use the Transitions in the Animator. So I ask you if there is a more effective or elegant way to do what I need. In particular, i'm looking for a way to resume the animation without using the "Play()" function but instead using an animation's Behavior.

    Thank you for your time and any help!