Search Unity

Change speed of animation for a particular animator?

Discussion in 'Scripting' started by TheCelt, Aug 28, 2016.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    Hello

    I have a unit with an animator and i want to adjust the movement speed relative to his velocity. So i have something like this:


    Code (CSharp):
    1. animator.SetFloat("MoveState",unit.actualVelocity.magnitude);
    2. animator.speed = Mathf.Clamp(unit.actualVelocity.magnitude,0.5f, 1f);
    The problem is, it is changing the speed of all the animations not a specific one. I want to only adjust the speed of the walk animation for the character in question. How can i reference just that specific animation to adjust it's speed without adjusting the other animations like turning etc?
     
    EugeneArpa likes this.
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Its a obscure since it was a feature tacked on in 5.1 Follow these steps:
    1. Bring up the Animator window -- Under Window Menu its right below all the windows that have Ctrl Shortcuts. Its dockable so just dock it next to your scene
    2. Click on your animator (Prefab or the one in your scene). it should show the States and how they are linked to the individual animations.
    3. On the far left is 2 tabs that say Layer and Parameter. Click on Paramter. Below it click the + button and choose float. This will create a New Float you can rename that is currently set to 0.0. Change its name to whatever you like: AnimMultiplier for example. and set its value to 1.0
    4. Now Click on your Animation you want to have its speed changed. You will see its speed is set to 1 and below it is a greyed out Multiplier. Check the Parameter box, then Choose AnimMultiplier from the drop down
    5. We are done with the editor
    Now in your code you can adjust the speed like this:
    Code (CSharp):
    1. public Animator animator;
    2. void ChangeStateSpeed()
    3. {
    4.     animator.SetFloat("AnimMultiplier", 2.0f);
    5. }
     
    AKComp, TheCelt, TheGreenGiga and 7 others like this.
  3. Branxord

    Branxord

    Joined:
    May 13, 2018
    Posts:
    15
    How did this guy not thank you, this was a life saviour for sure! Thanks!
     
    nico_st_29, Dudey and rahulk1991 like this.
  4. nico_st_29

    nico_st_29

    Joined:
    Mar 14, 2020
    Posts:
    69
    Agreed, very helpful.
     
  5. falconfetus8

    falconfetus8

    Joined:
    Feb 4, 2013
    Posts:
    17
    That doesn't explain why setting "animator.speed" didn't work, though. Why doesn't it work?
     
  6. Matzerath

    Matzerath

    Joined:
    Jul 18, 2013
    Posts:
    1
    Incredibly helpful, thank you!
     
  7. samuelchanceholden

    samuelchanceholden

    Joined:
    Mar 29, 2023
    Posts:
    2
    YOU ARE THE ABSOLUTE BEST,
    TIP: make sure it is of type float not int