Mecanim - Change animation speed of specific animation or layers

Hello,

So I’ve been trying to port my animation system over to Mecanim but I’m hitting a wall. In the old system, I could go directly to the animation component and set the animation speed as such

animation["animationName"].speed = somefloat;

However with Mecanim, the only such control I am afforded is through changing the animator component speed. The problem with this is that this variable controls the speed of all animations for that component, not just one.

I was trying to work around this by doing some logic checks in my animation class, and checking to see which animation was playing and then setting the speed as needed. This works fine until you start using multiple layers with the Animator component, at which point the following happens:

Let’s say I have a walk animation in my base layer. I then go into my script and check “If the walk animation is playing, set the animation speed = walk velocity”. That works fine with one layer. Now, I add another layer, for the upper body. This layer contains things like an attack and hurt animations, meant to be played while walking.

Now this code above doesn’t work. Because yes, the “walk” animation is still playing, but so is the hurt or attack animation. The walk animation is meant to have one speed, the other animations the normal speed. I can’t seem to figure out a way to fix this at the moment.

Does anyone know if this is even possible with Mecanim? This is a huge oversight if that’s the case, but I’m hoping I’m wrong and I just don’t know the right way to do it yet. Even a hacky workaround will do, so long as it still uses mecanim.

I’m trying really hard to port my system to Mecanim, and I want to make sure I exhaust all my options before I declare it a lost cause and stick to legacy animations.

1 Like

Hi Rico,

If you go into the animator window (with all your states (walk, run, punch etc.)) and click on the state/animation that you want - ‘walk’ in this case…in the Inspector window it gives you the option of setting the speed of that particular state/animation. Each state/animation can have its own individual speed setting.

Edit - Also, so far I’m fairly certain that there’s no way to alter the overall speed of a particular layer…just the animator component like you described.

I hope this is what you were trying to do! :smile:

2 Likes

yeah for mechanim inside the controller you just click on the animation and on the inspector you will see the speed like John mentioned, not the whole scripting like animation[“animname”].speed that is more for the legacy animations

Right, I’ve seen that . Sorry, I guess to clarify, I’m looking for a way to do this via script, at runtime.

The movement speed of my enemies varies. As they walk or run faster, their animation needs to be sped up accordingly. This was done in the past like I mentioned, though script, using animation[“name”].speed = value; However with mecanim I can’t find a way to change the speed through script without changing it for all other animations on the component.

I don’t know if you can set the speed like that, but what I do know is that you can use a blendtree to achieve this:

Maybe you only have 1 walk anim and want to use it at speeds 2-10 m/s. Lets say it’s animated at 2 m/s.
Make a blendtree with two motion fields and use the same anim in both, based on a speed float:
anim: walk Threshold: 2 Timescale: 1
anim: walk Threshold: 10 Timescale: 5

Drag you speed slider in the blendtree to preview the anim change speed.
Hope this gives you some ideas.

6 Likes

Hey Arnleif,

That’s actually an interesting workaround I didn’t think of before, thanks for sharing it!

I’ll give that a shot to see if it works. Best 1st post ever!

I would still like to see a way to do this through script. It really is almost a necessity when doing steering behaviors for AI.

1 Like

I agree with Rico, even a simple case of two characters walking together- if one is short, he needs a faster walk speed to keep up, but changing Animator.speed makes all of his clips speed up.
It seems silly to have to make a new animator controller just for one clip.

Arnleif’s workaround is a pretty good solution for now- just set a bool parameter to trigger the faster speed for my short character.

we need animator.state.speed

1 Like

Arnleif et Al,

Thanks for the Great work around Solution. It worked like Magic !

We are trying migrate our Scrubbing code from the old animation system to Mecanim, was wondering if anyone has some ideas.

Cheers

Thanx for this workaround. it has helped me too :slight_smile:

I was trying to figure out how to do this, and your method works well.

I really need to be able to set/modify the playback speed of the individual states from scripting as well.

The method mentioned here won’t work for my use, unfortunately. Does anyone have a clue when Mecanim will get ALL variables exposed in the API?
It is very much not in the spirit of “Unity” having so much machinery concealed from a runtime scripting intervention. I need this so(!) badly.

1 Like

i’m pretty bummed to hear that this isn’t supported currently. maybe there is a way to access the private variables through reflection? hopefully this is on the roadmap

1 Like

Mecanim needs an overhaul. Since I primarily use Playmaker (which is already a state machine), there isn’t a single advantage Mecanim gives me over the legacy system, and it introduces quite a few cons.

Originally I decided to dabble in Mecanim because of the potential of using body masks in override layers. This has proved useless for my purpose as there’s (currently) no way two concurrent animations can animate their respectively controlled bones with their respective root transformation/rotation parameters. One root transform to rule them all.

2 Likes

Hey all, I know this is old tread but I stumbled upon it having the same problem.

Here is the easy solution.

You can change the speed of the animation in mechanim via scripts.

Animator.speed = value;

By default the value is set to 1, but you can change it this way.

private Animator anim;

Void start()
(

anim = gameObject.GetComponent();
anim.speed =2; // this doubles the speed animation for all animations inside the animator of the object this script is attached to
)

1 Like

@Sericet1
Your solution changes the entire animators speed

I’m necroing this thread rather then creating my own because my needs are the same.
The solution of the blend tree is a clever one, but the preferred solution is to change the animation speed by changing the animation state information. Also the change should only applied to a single instance of an animators motor, that is, if my speed is changed only my speed is changed.

Come on Unity… Are you seriously going to make me flip through Sprites manually in the SpriteRenderer in order to set a desired animation speed through code? A high level engine like this shouldn’t force developers to go back to the basics…

3 Likes

Bump

Unity 4.5.0 is out and we can´t change speed of individual animations and/or layers… Please, add this feature on your ToDo list, I can´t move from legacy to mechanim without this feature.

Regards

1 Like

It´s seems that with it works perfectly with blend Tree and Automate Thresholds.

The last time I tried mechanim, blend trees did not work like now.

Regards

Please, it’s been a very very long time since Mecanim came out. The ability to change speed of individual animation is very important for us to be able to use Mecanim in a meaningful way from code.

1 Like

Wow I am very disappointed to find that there is no way to change the animation speed of individual states in mecanim through scripting.

That is why I too am bringing this thread back to life so that someone from Unity will hopefully see it.

Changing animator.speed is not good enough because it will change the speed of all states. Just because I need a run state to go faster based on a character’s velocity does not for example mean that I want their melee attack animation to go faster as well.

Please fix this. We can already click a state in the editor and alter its speed that way, but we need to be able to do it by scripting too.

THIS HAS NOTHING TO DO WITH BLEND TREES, just in case you still don’t get it