Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Back to the idle even with animator disabled

Discussion in 'Animation' started by bisewski, Oct 17, 2023.

  1. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    191
    Hi!

    I have an animation clip where I dissamble an object. Like an explosion view.

    I start this animation clicking in a buttton where I can play this clip using a SetTriguer.
    When finish this animation or when I want just to exit I am using this code:
    Code (CSharp):
    1.  animator.Play("play", -1, 0);
    2. animator.SetTrigger("reset");
    This code is inside a function that I call clicking in a button. But the problem is that in the same code I need to use animator.enable = false because I need to disable it. This because for some reason I cant hide parts of this object when animator component is active.

    In tests that I did I saw that always that one object have a key frame in a animation, setActive to false it in realtime simple doesn't work...

    Ok, but if I use it, animator.enable = false I have another problem: The animation clip just dont back to the start position and my object keep not in the original position but with the animated positions...

    See the image:
    In the idle state I dont have any clip. In the Play, my explode view clip.

    What can I do to disable the animator but still back to the idle state where I have the original position of my objects...

    Code (CSharp):
    1.     if (!playEnabled)
    2.     {    
    3.         startAnimationBtn.sprite = stopImage;
    4.         animator.enabled = true;
    5.      
    6.         animator.Play("play", -1, 0);
    7.         animator.SetFloat("speed", 1);
    8.         animator.SetTrigger("anim");            
    9.         playEnabled = true;
    10.     }
    11.     else
    12.     {
    13.      
    14.         animator.Play("play", -1, 0);
    15.         animator.SetTrigger("reset");
    16.      
    17.         //animator.keepAnimatorStateOnDisable = true;
    18.                
    19.         animator.enabled = false;
    20.         startAnimationBtn.sprite = playImage;
    21.         playEnabled = false;
    22.     }
    23. }

    EDIT1: I find one answer that the state machine of any animator reset to the initial state when turn off...This not happen in my case...
     

    Attached Files:

    Last edited: Oct 17, 2023
  2. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    191
    Ops, using these line help me with my problem:
    animator.Rebind();
    animator.Update(0);
    animator.enabled = false;
     
  3. calllumtendo

    calllumtendo

    Joined:
    Sep 4, 2022
    Posts:
    9
    ok so I'm not necroing this but you can invert animations without playables! I made th suloution myself and its as simple
    as making the end keyframes on the start and end keyframes at the end (you will need 2 animations for it to work!)
     
    bisewski likes this.