Search Unity

Smooth transitions

Discussion in 'Animation' started by zedz, May 31, 2018.

  1. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    253
    I've looked online at tutorials and video's but still have no idea how this works
    Say I have 3 Idle animations
    idle1,idle2,idle3

    I can easily transition from 1->2 by doing a 'make transition' between the 2 animations and that looks nice but I'm wanting to mix it up so they dont repeat the same paterns eg I'm using the following code, when an idle animation has finished

    GetComponent<Animator>().Play( animation_idle[Random.Range(0,animation_idle.Count) ] );
    so it could 1->3->1->2->2>3->2
    now what happens when the animations are playing as soon as its finished it jumps straight to the next one,

    Ideally I want some code like
    GetComponent<Animator>().Play( animation_idle1 );
    GetComponent<Animator>().TransitionTo( animation_idle3 );

    but have no idea how to achieve this, any ideas,
    cheers zed
     
  2. Goatogrammetry

    Goatogrammetry

    Joined:
    Apr 27, 2017
    Posts:
    197
    This is a design weakness in mechanim IMO. It'd be easy for unity to make a randomizing blend-tree but for now we have to fake it. Here's what I did:

    The easy way is to make a 2D blend tree with, in your case, 3 animations. Create a float variable to control the blend tree. Lets call it "IdleRand". Put an event at or near the end of each idle animation that will trigger a little script that may or may not change IdleRand based on randomness. Depending on how well your idles match, you may instantly change IdleRand or you may use a mathf MoveToward to very quickly blend to a *neighboring* (thats important) idle. Works for me. Of course you could also use one of those movement blend trees, but they like to have a 0x0y in the middle that you'd need to pass through all the time. It all depends on your idle animations and how they work together.
     
    zedz likes this.
  3. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    253
    OK thanks mate, Thats does sound like a flaw in Unity.
    Its not even that hard for them to program, they don't even need a TransitionTo function but something like

    animation_idle1 is playing
    GetComponent<Animator>().transitionSpeed = 0.25f; // takes 1/4 second to transition fully from old animation to new animation
    GetComponent<Animator>().Play( animation_idle3 );

    and internally whilst unity is updating the bones just go
    foreach( bone in skeleton ) {
    lerp( bone, previous_animation_frame(idle1), idle3, amount );
    }

    change transitionSpeed to 0.0 if you want the new animation to start playing instantly.
    This seems like a no brainer, its shocking that Unity doesn't have something similar already