Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animator State Machines in Unity 5 when disabled

Discussion in 'Scripting' started by coshea, Oct 6, 2015.

  1. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    I have a simple sm as shown in the attachment.

    When you trigger an action, it does from the idle loop to unwrap and then holds on unwrap end of animation until another action is called to move to the next animation. This worked fine in Unity 4. I have a custom culling solution (because using MeshBaker need to have Animators set to always animate), so I enabled or disable the Animator component depending on if its within camera range (2D).

    Previously you could disable the Animator and enable it and everything would be fine, now when you re-enable the component it jumps to the entry point.

    How should I be using Entry in this situation?

    Thanks
     

    Attached Files:

  2. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    I would suggest using the AnyState rather than Entry if you are going to enable and disable the component. (ie. remove the transtition from Entry to Idle. Set up Transition AnyState to Idle). The Animator docs are a little meaty but can be useful. The one fore AnyState is here --> http://docs.unity3d.com/Manual/class-State.html (scroll to bottom)

    Hope that helps!!
     
  3. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Thanks for your reply.

    You can't delete the connection between Entry and the default state.

    I've tried adding AnyState to the idle, also Entry to every state and also AnyState connected to every state (see attachment) but its always the same thing, if you disable then re-enable the Animator component, everything gets reset (didn't used to happen this way).

    Just to clarify, I disable then re-enable the component when it is in the unfold state, and it returns to idle.

    Thanks
     

    Attached Files:

  4. v21

    v21

    Joined:
    Jun 13, 2009
    Posts:
    9
    Hiya,

    So I'm tackling the same thing. It seems as if disabling and re-enabling the Animator component (even if within the same frame) resets the current animation state back to Entry, as well as clearing all the parameters back to their default values. I'm currently getting around this by adding an extra script to this component, storing the state there, and setting it every Update. It would be a lot nicer if I didn't have to do this, though.
     
  5. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Agreed, maybe they will add a Animator.keepCurrentState like @Mecanim.Dev suggests on this other thread (they should)

    thanks for the link to here
    http://forum.unity3d.com/threads/bo...-except-if-set-in-editor.287955/#post-2138382

    So this works
    Code (csharp):
    1.  
    2. void disableAnimator(){
    3.   animatorStateInfo = myAnimator.GetCurrentAnimatorStateInfo(0);
    4.   myAnimator.enabled = false;
    5.  }
    6. void enableAnimator(){
    7.   myAnimator.enabled = true;
    8.   myAnimator.Play(animatorStateInfo.shortNameHash);
    9.  }
    10.  
     
  6. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Spoke a bit too soon.

    When you enable the animator and do play as above, it plays a little bit of idle and plays through the unfold animation. Before disable, it had played through the unfold animation (non looping), so was static on this state. But when you enable it, it plays through it again :(

    Any ideas? @Mecanim.Dev

    thanks
     

    Attached Files: