Search Unity

Question Slowing animation to the correct speed.

Discussion in 'Scripting' started by vcinardo, Jan 26, 2023.

  1. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    31
    I am making the player hit faster as they level up. I can't seem to get the right animator speed to sync with the amount of time that the animation is supposed to play. Here is the roll animation. I am trying to get the roll to finish executing at 1.5 seconds. I am trying to adjust the animator speed to account for this.

    Code (CSharp):
    1. animator.Play("Roll");
    2. float length = animator.GetCurrentAnimatorStateInfo(0).length;
    3. rollTime =1.5;
    4. animator.speed = rollTime / length;
    Let's say the length is 2;

    My reasoning is the that when speed is one, length will play out in .416 seconds. So if length is 2...
    .416 / 1 = 2 / x -> x = 2 / 0.416
    Then I use x as the speed multiplier. What am I doing wrong?

    Here I just adjusted the time until it looks right.
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    If you double rollTime, you'll double the animator speed. I think you want to halve the speed!
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    You want
    length / speed = rollTime
    , so
    speed = length / rollTime
    .

    You mixed up the order of
    length
    and
    rollTime
    . I think your example has the order correct?
     
  4. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    31
    Yep, thank you both it's working now!