Search Unity

Is it possible to skip an Animation in Mecanim (without speeding up the animation)?

Discussion in 'Animation' started by Purpleshine84, Dec 3, 2013.

  1. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Well, thats my question :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm not sure exactly what you mean by "skip an Animation." But you can jump directly to any state in your Animator Controller from code, by just calling CrossFade(newStateName, 0.3f) on the Controller (substitute whatever cross-fade time you prefer).

    If you want a method that works with both Mecanim and legacy animations, then try this:

    Code (csharp):
    1.     private Animator animator;
    2.     private Animation legacyAnimation;
    3.     private string curStateName = "";
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.         animator = GetComponent<Animator>();
    8.         legacyAnimation = GetComponent<Animation>();
    9.     }
    10.    
    11.     void PlayAnimation(string name) {
    12.         if (animator != null) {
    13.             animator.CrossFade(name, 0.3f);
    14.         } else if (legacyAnimation != null) {
    15.             if (curStateName != name) {
    16.                 legacyAnimation.CrossFade(name, 0.3f);
    17.             }
    18.         }
    19.         curStateName = name;
    20.     }
    HTH,
    - Joe
     
    WoodsmanThePeasant and eab6863 like this.
  3. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Thanks a lot! Thats what I meant, I will report my results asap :)
     
  4. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Except for that I am only using Mecanim, not sure if Crossfade is an option there..
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, it is. The code I posted works for both Mecanim and legacy animation.
     
  6. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    I did it by selecting the transition (between the 2 states) and making the selected range as close to zero as possible (drag both brackets until they on top of each other).
    When working with 2D hand-drawn animations this is the way I do it.
     
  7. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    I cannot seem to code it from Mecanim only... to begin with: all my animations are humanoid, Secondly: its not animator.Crossfade but "animation.Crossfade", this mehod requires you to use a string but my animations are int's, so I tried ToString() but that doesnt work, but I dont know if its possible to make strings if I use Humanoids and Mecanim.. well, I will try then.
     
  8. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Nah.. i get a missing component reference which means this would only work with Mecanim AND Legacy.. pfff
     
  9. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    I also dont understand how you could use Mecanim and Legacy at the same time, wouldnt that be confusing for the compiler if you for example needed one animation both placed on the Inspector animator aswell as in the Mecanim State Machine. Because I need to skip the animations that are already in the State Machine, not making new ones that are the same(!) otherwise this would get confusing I geuss... some tips?
     
  10. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    I will try that as well, thnx ;)
     
  11. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    If I look at your code (sorry to bother again, I write too much), and look at this line: animator.Crossfade, this will not work since Crossfade is derived from "animation". However, I use Unity 4.2, might that be the problem? For the rest your script looks pretty clear, nicely organised.
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The script works fine here; I copied it directly out of working code. The Animator.CrossFade method is (poorly) documented here. The script is not intended to work with legacy and Mecanim animation at the same time; it is designed to work with either one. Just snip out all references to legacyAnimation if they are confusing you. (And be sure you do NOT change animator to animation; these are very different beasts.)

    And yes, you do have strings for your animation states; they're the names of the states as entered in the animation controller editor. It's certainly possible to use an animation state hash instead; the CrossFade method is overloaded to accept either a string or an int. However, if you have an int (the hash), you can't just call .ToString() on it to get back the string — a hash is a one-way operation. Use the original name instead, or update the code to work with the integer hash.
     
  13. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    You can also just un-tick "Atomic" on your transition lines in Mecanim. This will cause the animation to automatically go to the next if the condition is met, without needing to finish the animation.
     
  14. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Thank Joe! So the Animator.Crossfade is only available in Unity 4.3?
     
  15. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Yes, but it has to skip the animationClip itself, not the transition. I do thank you for the reply because it will help a lot of people looking for answers ;)
     
  16. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Hi Joe, I upgraded to Unity 4.3, and indeed: it works! So for everybody still using 4.2: Animator.Crossfade is not available there! And Joe, sorry for bothering, I would have solved immediately this issue if I would have upgraded before :/
     
  17. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    It does skip the clip itself. Transitions are only what tell the clips what to do and when. If you have Atomic turned off, and say you press left to queue your run, it will automatically skip to the run animation, it won't do anymore frames of whatever animation you're currently on. It's exactly as you say, skipping without speeding up. If the condition to move on is already met before the animation even gets a frame off, then it will go to that next animation.