Animator Random Animation (Parameter)

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:

anim = GetComponent<Animator>();
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?

.

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.

@Mecanim-Dev

Thats Perfect! Greatly Appreciated!

void Start () {
        anim = GetComponent<Animator>();

        int pickAnumber = Random.Range(1,3);//exclusive never prints the last only goes 1 to 2
        Debug.Log (pickAnumber);

//randJumpInt is the parameter in animator
//pickAnumber random number from 1 to 2 from above
        anim.SetInteger ("randJumpInt", pickAnumber);
    }
1 Like

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?

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

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.

That would be awesome. I’m using 4.6.1

Thanks!

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

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.

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


animator.SetInteger ("RandomAttack", Random.Range (0, RandomMax));
Debug.Log ("OnStateEnter:" + animator.GetInteger ("RandomAttack"));

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

@imtrobin Did you figure this out? I’m encountering the exact same issue.

No, I moved to Unreal.

4 Likes

ahahah interesting :slight_smile:

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

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.

2 Likes

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?
3333288--259955--upload_2017-12-25_19-15-11.png

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.