Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

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

Discussion in 'Scripting' started by sunseeker1988, Sep 13, 2017.

  1. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    279
    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.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    4,834
    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:

    [​IMG]

    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.
     
    sunseeker1988 likes this.
  3. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    279
    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.
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,214
    lordofduct's method does work for individual objects. You need an AnimationController on each object, and you need to set it from code:

    Code (csharp):
    1. animator.SetFloat("WhateverYouCalledIt", 0.5f);
    If you just want to change the animation speed as a whole you can just do:
    Code (csharp):
    1. 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.
     
    sunseeker1988 and lordofduct like this.
  5. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    279
    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.
     
    Last edited: Sep 14, 2017
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,214
    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.
     
    sunseeker1988 and lordofduct like this.
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    4,834
    Yeah, I'm really confused here on that matter.
     
    sunseeker1988 likes this.
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,214
    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.
     
    sunseeker1988 likes this.
  9. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    279
    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.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    4,834
    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?
     
    Last edited: Sep 15, 2017
    sunseeker1988 likes this.
  11. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    959
  12. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    279
    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.
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    4,834
    Create a float parameter for the animatot, name it whatever you want to name it.

    Then here:
    [​IMG]

    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:
    Code (csharp):
    1. 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:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     //where rb is a RigidBody and animator is the Animator
    5.     animator.SetFloat("WhateverYouCalledIt", rb.velocity.magnitude);
    6. }
    7.  
    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.
     
    sunseeker1988 likes this.
  14. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,214
    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.
     
    sunseeker1988 and lordofduct like this.
  15. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    279
    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!
     
    makeshiftwings likes this.