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

Well, thats my question :)

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:

    private Animator animator;
    private Animation legacyAnimation;
    private string curStateName = "";

    // Use this for initialization
    void Start () {
        animator = GetComponent<Animator>();
        legacyAnimation = GetComponent<Animation>();
    }

    void PlayAnimation(string name) {
        if (animator != null) {
            animator.CrossFade(name, 0.3f);
        } else if (legacyAnimation != null) {
            if (curStateName != name) {
                legacyAnimation.CrossFade(name, 0.3f);
            }
        }
        curStateName = name;
    }

HTH,
- Joe

2 Likes

Thanks a lot! Thats what I meant, I will report my results asap :)

Except for that I am only using Mecanim, not sure if Crossfade is an option there..

Yes, it is. The code I posted works for both Mecanim and legacy animation.

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.

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.

Nah.. i get a missing component reference which means this would only work with Mecanim AND Legacy.. pfff

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?

I will try that as well, thnx ;)

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.

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.

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.

Thank Joe! So the Animator.Crossfade is only available in Unity 4.3?

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 ;)

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 :/

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.