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

Randomized Animations in Unity 5 w/ State Machine Behaviors

Discussion in 'Animation' started by jchapman723, Mar 9, 2015.

  1. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    I have been having issues getting random animations to work with the new state machine behaviors.

    I did my best to follow the instructions in this article:
    http://blogs.unity3d.com/2015/02/11/having-fun-with-the-new-mecanim-features/

    My understanding is that I should be able to create a new behavior script, paste in the code snippet from the article (changed the random range to suit my needs), with the final script looking like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomIdle : StateMachineBehaviour {
    5.  
    6.     override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)</pre>
    7.     {
    8.      
    9.         animator.SetInt(“IdleIndex”, Random.Range(0, 2));
    10.      
    11.     }
    12. }
    13.  
    Making sure I have an "int' parameter named "IdleIndex", and a transition going in/out of the state machine so it repeats, it SHOULD choose a random animation each time it enters the state machine.

    However, I'm getting the following errors:


    This is my Animator - each transition from Entry has an "IdleIndex = " 0, 1 or 2 value:



    The article, and the "To Mecanim and Beyond" lecture on Youtube make it seem like it should be incredibly easy (although the code in the lecture is much different than the code presented in the article). I'm wondering if maybe some of the syntax changed or since public launch or something? Or am I just doing something wrong? Any help to get this working would be great.

    Thanks
     
    Last edited: Mar 9, 2015
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    It just looks like a cut-and-paste issue. Get rid of "</pre>" at the end of line 6, and change the open/close quote characters on line 9 to plain quote characters:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomIdle : StateMachineBehaviour {
    5.  
    6.   override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
    7.   {
    8.  
    9.       var index = Random.Range(0, 3);
    10.       animator.SetInteger("IdleIndex", index);
    11.       Debug.Log("Entering state machine. Setting IdleIndex to: " + index);
    12.  
    13.   }
    14.  
    15. }
     
    Last edited: Mar 12, 2015
    DMRhodes likes this.
  3. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    Thanks TonyLi. In addition to what you mentioned, I had to change SetInt to SetInteger and I get no errors (yay!), but the state machine doesn't really do anything now. It will play whatever the default state within the statemachine is, but the editor doesn't update with the time bar on the active state or anything. This could be a 5.0 editor issue perhaps, but it's entirely possibly that there's still something else going on. Seems like all the pieces are in place and it should just work now but no luck.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Make sure your transitions are based on the value of IdleIndex.

    While the scene is playing, click on the character and watch the state machine window (Animator view). Make sure the IdleIndex parameter is changing.
     
  5. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    The IdleIndex parameter is most definitely not changing with this current setup.

    Also, the transitions you're referring to are the ones from the Entry node into each animation node, correct? Those are the only transitions on which I have specified transition events, which are "IdleIndex = 0, 1, or 2".

    Do I need more code somewhere to make the IdleIndex change? Or some transitions on the way to the Exit node or something? It just doesn't seem like it's running through the state machine the way it should be, which is probably why the "onStateMachineEnter" function isn't doing anything.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Did you add the RandomIdle script to your state machine?

    I just updated the code snippet in my earlier reply. It will now write a line of information to the console. If you use this version, does it log anything to the console?
     
  7. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    Yes the RandomIdle script is attached to the StateMachine:



    I applied your new code with the debug line to the script and nothing printed to the console. I also seem to have run into a Unity 5 editor bug with Animator not updating when I start play mode, but after refreshing the window, I can now see what it's doing:



    So at least now it looks like it's running through the state machine properly, just that it's ignoring the behavior script on the state.

    Here is another video showing all the transitions and such:

     
    Last edited: Mar 11, 2015
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Two things:

    1. With Random.Range(min,max), max is exclusive, so Random.Range(0,2) will return either 0 or 1. Use Random.Range(0,2) to return 0, 1, or 2.

    2. It appears that if one of the states is the default state, it'll take precedence. I managed to get the state machine's behavior working by introducing a different default state outside of that state machine. Of course, it runs this state first, so you'll need to adjust it to your needs.

    The top level:



    The state machine ("New StateMachine"):

     
  9. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    I really appreciate the replies, TonyLi. For some reason on my end the IdleIndex just doesn't want to change with this behavior script. For the time being, I think I'm going to just control that parameter with playmaker since that's the only thing that seems to be having an effect.

    Thanks again for looking into it for me.
     
    theANMATOR2b likes this.
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Sorry it didn't work out. I'm glad PlayMaker's there as a fallback. Unity may still be working bugs out of their new state machine features.