multiple objects share Animation. I need to control Each Objects animation speed individualy.

Hi everyone, I cannot figure out how to control the animation speed of objects who share a animation controller and animations. I can only find code for changing the controller’s speed which affects all animations or
anim[“Walk”].speed = -1.0f; which absolutley does not change the aniomation speed.

OK, first and foremost, I don’t use Animator/Mecanim very much. I HATE Mecanimwith a searing passion…

So with that in mind, I’m operating on a limited knowledge base of how to do this a couple years ago when Mecanimfirst got added… combined with some of the features that were added early on.

So… first and foremost, the way Mecanimworks is that you have animation ‘states’ rather than animations them selves. An animation ‘state’ plays a specific animation, but has various information that is paird with it (including speed). When you’re in the AnimatorController you’ll see this as this:

Motion being the actual animation, but the other properties being the stateful properties for that given state.

So you’re not looking to change the speed of an animation, but of a specific animation state. The state sets the speed when it plays, ignoring anything about the speed of the animation itself (or rather augmenting the animation speed, I think)…

Now early on there literally was NO WAY to modify said speed at runtime. Yep… really annoying. You could in an editor script, but that was only useful for editor scripts… so yeah, not runtime. All you could do is change it in the editor before compiling.

They then added a feature which you can see in the image. That being linking a parameter to a property of the state. So for example you can create a ‘float’ property on the AnimatorController called ‘RunSpeed’, and then tick the ‘Parameter’ checkbox, select ‘RunSpeed’ as the parameter, and then if you change that value from code (at runtime), the value would effect the animation state.

This should solve your problem at hand.

If only in a ham fisted manner… meaning you have to literally set up a parameter for every animation you want to be able to control (uniquely that is… you may have a run left, run right, and run straight which can all use the same RunSpeed parameter). And you have to do this on every AnimatorController.

This is actually why I freaking DESPISE Mecanim. It’s so god damn adhoc.

Note - they may have added newer better ways to do this that I’m unaware of. I don’t know since I don’t use Mecanimon a regular basis. But looking through the documentation, there is no clear way to do it that I can find. And googling for it of course comes up with the usual bag of Legacy vs Mecanim, as well as the other “can’t be done” stuff.

If Unity ever drops support for legacy animation, I’m out of here. Like srsly… I loathe Mecanim.

1 Like

thank you, but what you have there changes the animations parameter speed, but for all objects and not individual objects which will still affect all objects that share that animation. I need to be able to adjust each objects individual animation speed as they all share a single animation and animator controller. Its seems absolutley freaking stupid that unity has no easy way to adjust the damn speed of a shared animation. Just that objects "instance’ of that shared animation.

lordofduct’s method does work for individual objects. You need an AnimationController on each object, and you need to set it from code:

animator.SetFloat("WhateverYouCalledIt", 0.5f);

If you just want to change the animation speed as a whole you can just do:

animator.speed = 0.5f;

But if you want to change the speed of a particular animation only, then I believe you have to do it with the parameter.

2 Likes

anim[“Walk”].speed = -1.0f This is working If I check the Speed parameter multiplier Tick box and select my Float parameter I named AnimationSpeed in teh controller. The controller animationSpeed float parameters set to 0.5 value and using anim[“Walk”].speed = -1.0f in my script seems to override the animation speed which is what I want since it wont work to set the initial speed. so I set the initial speed of the AnimationSpeed parameter value in the controller for the starting aniamtion speed then make any changes with “anim[“Walk”].speed = -1.0f” . If I uncheck the parameter tick box of Speed on the animation clip then “anim[“Walk”].speed = -1.0f” will not work at all for setting the animation speed, not even at start its like the code won’t fcking work without the parameter being set in the animation controller and parameter multiplier tick box on the clip.

I would really like to know why you cannot set the inital animation speed on an individual model with JUST the following code: anim[“Walk”].speed = -1.0f, WITHOUT also seting a float parameter in the controller and using a multiplier on the clip to set the inital speed of the animation.

Wait, are you using Mecanim or the old school Animation system? anim[“Walk”] sounds like you are using the Animation class (old school), whereas with Mecamin you should be using the Animator class.

2 Likes

Yeah, I’m really confused here on that matter.

1 Like

I will say that the more “correct” way to do what I think you’re trying to do (control how fast characters are walking) in Mecanim is to have a “Velocity” parameter, or a “VelocityX” and “VelocityZ” parameter if you’ve got strafing animations, and use a BlendTree that blends between idle, walking, and running animations based on that parameter. Look at how Unity does it in their Mecanim examples. You shouldn’t usually be setting clip speeds within states directly.

1 Like

I am using mecanim with legacy animation system but also using mecanim for the blend tree. the mecanim way has no way to set varied speeds on MULTIPLE objects ALL SHARING THE SAME ANIMATION and using the SAME controller. any adjustments made in macanim or to a parameter made in mecanim changes the entire controller’s animation properties which mean every model sharing it is affected, which is not what i want. If you can inform me of how to use mecanim to set individual animation speed on MULTIPLE objects that ALL SHARE the SAME CONTROLLER AND ANIMATION I would love to know since I cannot find ANYTHING online on how to do this. im dumb founded as to why there is no simple peice of code that can change the speed of a single objects animation speed that shares the same animation and controller with other objects.

What?

Are you trying to change the animation speed on an individual object, so that all the others are not impacted?

Or are you trying to change the speed so that all objects that use the same controller all get updated?

Also, please show us the code you’re using that attempts to do this.

Also…

HUH?

You’re using both legacy animation AND mecanim on the same object? Yeah… don’t do that.

And lastly, why do you write in giant paragraphs covering multiple things? Break up your statements into well formed sentences. And group them into shorter paragraphs that deal with each topic/thing you’re attempting to discuss.

These long run-on paragraphs are very difficult to parse.

Something like this:

In which case…

Please clarify what you mean… no idea. Using both? Don’t use both.

Are you wanting to change it so one object is uniquely speed controlled from the other? Or that all (multiple) are controlled by changing one single controller?

The way you word it implies the ladder, BUT the next portion contradicts:

Contradiction… one or multiple? What is going on here?

We’ve shown you pieces of code to change the speed of an animation, by setting a parameter that controls it.

But if it doesn’t work, it may be because we think you’re asking for the opposite of what you actually are asking? So please… clarify, what exactly are you attempting to do?

1 Like

This looks interesting.

1 Like

I want to set each individual separate objects animation speed to act independent of one another. all the code you guys have showed is for changing the overall mecanim controller’s speed property which changes every objects speed. is there a way in mecanim or in code that will allow me to do this?

according to firesides link above as of 2015 the option does not exist and the only way is to set animator.speedd of the clip to change an individuals animation speed.

Create a float parameter for the animatot, name it whatever you want to name it.

Then here:

Tick the ‘parameter’ box next to speed, and for the parameter select the one you defined.

Then, in a script attached alongside the Animator you can do this:

animator.SetFloat("WhateverYouCalledIt", 0.5f);

Replacing “WhateverYouCalledIt” with the name of the parameter, and 0.5 with whatever the speed is.

Depending on how you code it you can make it conditional.

For instance you may change it in Update so that it matches the actual speed of the object. Something maybe like this:

void Update()
{
    //where rb is a RigidBody and animator is the Animator
    animator.SetFloat("WhateverYouCalledIt", rb.velocity.magnitude);
}

Note, that’s just an example… your condition by which you define your speed (and the scale of it) may vary. It might not have Rigidbody for instance, so therefore you get the speed in some different manner.

This will NOT change every Animator out there. It will effect the Animator that it’s attached to.

1 Like

This is not true. You must be doing something weird. Are you maybe going in manually to the controller in the project view and changing things and then assuming it won’t work from code? To do animation with Mecanim:

  1. You make a Controller, this sits in a project folder as a prefab basically. You do not write code to change the Controller sitting in the project folder during runtime; I don’t think that’s even possible.
  2. You put a character into your scene, and add an Animator to it.
  3. You assign the Controller to the Animator. That makes it so that this character has its own instance of the Controller running in the Animator.

When you want to change animation values from a script, you change it through the Animator, on the character that is in a scene. You don’t directly modify the original Controller that’s sitting in a project folder. Every Animator is running its own copy of the Controller, with all its own values for the parameters.

2 Likes

Thanks guys. yea you 2 were right. Now I see the animatorr is running its own instance with code “animator.SetFloat(“Speed”, 3.0f);” working now. Thanks a ton!

1 Like