Search Unity

Change the running AnimationState of an Animation

Discussion in 'Animation' started by otnax, Jan 10, 2020.

  1. otnax

    otnax

    Joined:
    Jan 4, 2020
    Posts:
    3
    Hello everyone !

    I'm new using animations in unity. I created in blender 3 animations for a fish mesh and I now have 3 animations clips. For now, I have attached to my mesh's animation; one main clip as "animation" and in my "animations" list my 3 clips like this:

    screen.jpg

    My main animation "mouvement" is running, but i would like to be able to change to another one.

    I tried this in code:
    1. Animation anim = fish.GetComponent<Animation>();
    2. anim["mouvement"].enabled = false;
    3. anim["left"].enabled = true;
    It stops the "mouvement" animation but doesn't run the "left" animation.
    Is there a way to do it ?

    PS: I also thried with the an Animator, but when I drag a clip into the animator windows, it creates a state, but the clip inside it is set to None I can't put one of my clip (even if I can change it to other clips already present in Unity).

    PS2: all my clips are working, I tested it bu changing the main clip in the mesh animation
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    If you want to play an animation using the legacy Animation component, just call anim.Play("left");

    If you want to play an animation using the newer Animator component, you need to set its Rig Type to Generic or Humanoid (not Legacy).

    You might also be interested in Animancer (link in my signature) which has lots of detailed Examples that explain how to use it.
     
    otnax likes this.
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,792
    For some, pretty silly I'm sure, reason, Animation clips are not compatible with Animator and there is no built-in way to convert them (you can edit a flag in debug mode, but there may be issues).

    Other than that as @Kybernetik said : anim.Play("left");

    For simple animations legacy animation is way faster than the animator, while the animator is presumably faster with complex ones, although I haven't actually found a use case where that is true (but maybe my use cases are not complex enough).
     
    otnax likes this.
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    You can make a simple context menu function to convert animations that aren't part of a model between Generic and Legacy:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         [UnityEditor.MenuItem("CONTEXT/AnimationClip/Toggle Legacy")]
    3.         private static void ToggleLegacy(UnityEditor.MenuCommand command)
    4.         {
    5.             var clip = (AnimationClip)command.context;
    6.             clip.legacy = !clip.legacy;
    7.         }
    8. #endif
    Just put that in any script and it will add a function to the cog icon in the top right of the Inspector of any Animation Clip. I'm guessing that the runtime data format is too different for it to be worth supporting the old format in the Animator.

    I've found that Animator Controllers are far worse for complex cases because you end up with some bits of logic in the Animator Controller and other bits that you need to use hacky workarounds for in your scripts so trying to actually decipher how things work involved a convoluted back and forth between the controller and scripts.
    If you're interested in finding a better system, I highly recommend you check out Animancer (link in my signature).
     
    otnax likes this.
  5. otnax

    otnax

    Joined:
    Jan 4, 2020
    Posts:
    3
    Thank you for your answers guys, helped me a lot !