Search Unity

Animator Random Animation (Parameter)

Discussion in 'Animation' started by kodagames, Aug 21, 2014.

  1. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Inside of a Blend Tree when you add Parameters the only type that is in the inspector is float. How come Integers are not available?

    I realize its a Blend Tree and makes sense to have floats but I was thinking if there was the ability to have integers then creating random animations would be a little bit easier. I could add the Int parameter in the inspector which would then let me add my animation clips (through motion fields) then write something like:
    Code (CSharp):
    1. anim = GetComponent<Animator>();
    2. anim.SetInteger("randJump", Random.Range(0,1));
    but that doesn't seem to be an option (unless Im missing something?)

    Ive seen this post:
    Post

    and tried taking care of it randomly but I get random values which starts the animation at the wrong point in the animation. If that makes any sense? Does anyone have another way to create random animations with animator?

    .
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Integer in blend tree are not supported because the purpose of a blend tree is to do continuous blending while changing the parameter.

    In 5.0 we introduced StateMachine Transition which can be used to generate random animation from a list
    see this blog for an example
    http://blogs.unity3d.com/2014/06/26/shiny-new-animation-features-in-unity-5-0/

    in 4.x you can emulate this by creating a sub statemachine, with a first state that act like a selector; put a small clip(a few frame only) in this state that blend corrrectly to all your other random state, for each transition make sure that you don't have an exit time, you want to change as fast as possible from this selector state to your random state. tool20.png
     
  3. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    @Mecanim.Dev

    Thats Perfect! Greatly Appreciated!

    Code (CSharp):
    1. void Start () {
    2.         anim = GetComponent<Animator>();
    3.  
    4.         int pickAnumber = Random.Range(1,3);//exclusive never prints the last only goes 1 to 2
    5.         Debug.Log (pickAnumber);
    6.  
    7. //randJumpInt is the parameter in animator
    8. //pickAnumber random number from 1 to 2 from above
    9.         anim.SetInteger ("randJumpInt", pickAnumber);
    10.     }
     
    Last edited: Aug 22, 2014
    Yaolegol likes this.
  4. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    Mecanim.Dev, is there any chance you could expound on this process for Unity 4.x users? I cannot get any of this to work for me. My problem is the same as OP's, I just need mecanim to play random animations, one after another. How do I go about setting up from scratch with the sub-state machine, and are there parameters and scripts to needed to drive them?

    Also, when I select the transition I made in the sub-state machine, my inspector does not look like yours, and with part of the graph cut off, I'm not sure what the whole system is supposed to look like. Any advice?
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    The cut off part was only showing one state walk, and all idle state were transitionning to this state.
    Using the visual editor to setup the whole thing is one way to do it.

    An simpler method would be to play your clip manually from the script with Animator.Play
    http://docs.unity3d.com/ScriptReference/Animator.Play.html
    Simply drop all your idle clip in your controller
    and then from a script you choose which clip to play.

    If you want tell me your unity version and I will build a small controller that will show you how to do it.
     
  6. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    That would be awesome. I'm using 4.6.1

    Thanks!
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Alright I did it very quick so don't look at the bad blending.
    Package is too big for the forum so I did upload it to our dropbox

    Open up random scene and press play, the character should randomly play a kick attack animation.
    https://www.dropbox.com/s/bfc65v0ha4bigq0/random.unitypackage?dl=0

    This is one way to achieve this, another way could be with any state transition or by script with Animator.Play.

    Hope it will help you
    Best regards
     
  8. jchapman723

    jchapman723

    Joined:
    Jan 27, 2014
    Posts:
    27
    That's fantastic, thank you so much for taking the time to do that!

    I will study it and try to apply it to my character. Thanks again.
     
  9. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    I'm trying to do a random animation in U5 but I'm getting confused. I'm not sure if it's buggy or my inexperience with it. I setup a substatemachine AttackRandom.



    In there, there is an default transition that is empty. I also can't change the default transition to say, attack1.



    So I added a OnStateEnter to randomize the parameter and condition like this




    Code (csharp):
    1.  
    2. animator.SetInteger ("RandomAttack", Random.Range (0, RandomMax));
    3. Debug.Log ("OnStateEnter:" + animator.GetInteger ("RandomAttack"));
    4.  
    Then I look at the debug log, it randomizes between both attacks but it does not follow the RandomAttack

    eg. the debug log

    OnStateEnter:0
    OnStateEnter:0
    OnStateEnter:1
    OnStateEnter:0 <- This can play attack2
    OnStateEnter:1 <- This can play attack1
     
  10. FloBeber

    FloBeber

    Joined:
    Jun 9, 2015
    Posts:
    166
    @imtrobin Did you figure this out? I'm encountering the exact same issue.
     
  11. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    No, I moved to Unreal.
     
  12. FloBeber

    FloBeber

    Joined:
    Jun 9, 2015
    Posts:
    166
    ahahah interesting :)

    And so? 2 years later, do you like it better?
     
  13. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Haven't reach two years yet, the graphics and tools works great together, unlike unity where I have to "fight" to get third party integration working together.

    The C++ portion has some quirks, which makes quite painful to get into, if you stick to blueprint, you are fine but limited in what you can do.

    My main issue with Unreal is parts of the PhysX is wrapped in their own logic, which makes it harder for me to do certain setup properly. I integrate PhysX in custom engine before so I would have done so differently. Unitiy physx 4 also had a lot of issues but they resolve most of it in U5. Unity side is still not quite correct but it gives more control than UE4. That's a summary.
     
    TextusGames and HeadClot88 like this.
  14. hottabych

    hottabych

    Joined:
    Apr 18, 2015
    Posts:
    107
    It doesn't work if game starts from random motion... because you must set a default state.
    And he always starts from Walk_0.
    Any ideas to avoid it?
    upload_2017-12-25_19-15-11.png
     
  15. Goatogrammetry

    Goatogrammetry

    Joined:
    Apr 27, 2017
    Posts:
    197
    Guys. I'm not a great programmer-- Just an animator but I figured out how to do a decent random animation. Here's how:


    // This switches between 2 idle animations by using a 1D animation blender
    // The idle animations have a trigger on the last frame called IdleEnded that triggers the randomizer
    // One animation happens more frequently than the other based on the idleRange in this case
    private void IdleEnded ()
    {
    int idleRange = Random.Range(0, 3);
    if (idleRange == 0)
    {
    m_animator.SetFloat("IdleRandom", 1.0f);
    }
    else
    {
    m_animator.SetFloat("IdleRandom", 0.0f);
    }

    IdleRandom is the range from 0 to 1 (the slider parameter in the blend node) that blends the anims, and is always either 0 or 1 in this case. Its obvious how you could expand this beyond just 2 animations by making a .5 for the third etc. It solves the problem of substates having grayed-out input links and works on the very first time the animation plays if you were to set up IdleRandom in the start. Tested and works.

    Not sure how this would handle animations of different lengths. Seriously we sure could use a node that did exactly this: Instead of blend-lists, a random play list with % odds per animation.