Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mecanim - Play Random Animations in Idle State

Discussion in 'Animation' started by fjhamming_CleVR, Feb 26, 2014.

  1. fjhamming_CleVR

    fjhamming_CleVR

    Joined:
    Feb 6, 2014
    Posts:
    10
    I have found the following source on how to choose between multiple idle animations:
    http://forum.unity3d.com/threads/152489-Mecanim-Play-Random-Animations-in-Idle-State

    I like the approach because the blend tree allows me to have only one idle state, which is transperant. I have an animation event on each of the idle animations which triggers a function that changes the blend parameter of the tree. The approach works like a charm when you have idle animations of the same length, but not when they vary in length. (when you choose a shorter animation, the character will just freeze in the last pose of the animation until the next animation is chosen.)

    A better approach would probably be a subtree rather than a blend tree, with a central idle chooser state that should fire an event so that the parameter on which idle to choose can be changed. See example: $IdleStateMachine.png

    The only problem with that is that the state does not supoort to fire events, and i do not want to put a single frame animation with an event there.

    So, considering all new developments since the original forum thread linked above, what would be the best approach on playing a random idle animation?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,510
    Although it kind of defeats the purpose of transitions, you could start and stop idle states using Animator.Play() [or CrossFade()].

    To start a random idle, use something like:
    Code (csharp):
    1.  
    2. string idleState = string.Format("Idle{0}_ME", (int) Random.Range(0, NumIdleStates) + 1);
    3. animator.Play(idleState);
    4.  
    And whenever an event occurs that should stop the idle animation, maybe something like:
    Code (csharp):
    1.  
    2. animator.Play("Default State");
    3.  
    Where Default State has transitions to handle whatever the event is.