Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Perform Segment of Animation

Discussion in 'Animation' started by unity_FD9A1B9D4DFDDFC16A81, Mar 26, 2023.

  1. unity_FD9A1B9D4DFDDFC16A81

    unity_FD9A1B9D4DFDDFC16A81

    Joined:
    Sep 15, 2022
    Posts:
    1
    Hello everyone.

    I created an animation of a finger closing, but I wanted for the finger to only close according to a given value.
    Example:
    • Perform full closing animation if parameter is 100%.
    • Perform half of the closing animation if parameter is 50%,
    • etc.
    Do you guys think this can be done through animations or should I just control the extent to which the finger closes through a script?

    Thanks for everyone's help
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I'm not aware of any way to control something like this in the Animator, I'm pretty sure you'd have to use code or create additional animations. I'll show one way of doing this for anyone interested.

    Here is the code:
    Code (CSharp):
    1.     public bool halfAnimation = false;
    2.  
    3.     Animator anim;
    4.  
    5.     void Awake()
    6.     {
    7.         anim = GetComponent<Animator>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         float normalizedTime = anim.GetCurrentAnimatorStateInfo(0).normalizedTime;
    13.         float correctNormalizedTime = normalizedTime - Mathf.FloorToInt(normalizedTime);
    14.  
    15.         if (correctNormalizedTime >= 0.5f && halfAnimation == true)
    16.         {
    17.             anim.Play("Test", 0, 0.5f);
    18.         }
    19.     }
    What this code does is that when the "halfAnimation" bool is set to true, it freezes the animation exactly halfway through its playtime (this is when it reaches at least halfway, it'll play normally until then). If the bool is false, then the animation plays normally all the way.

    How does this work? To explain that, I need to explain how normalized time works.

    Normalized time is a value between 0 and 1 that corresponds to an exact point in the animation. 0 is the beginning, 1 is the end, 0.5 is halfway, etc...

    We can get this value to see when the animation is halfway done, and we can set an animation to start at any point using this value. The line
    anim.Play("Test", 0, 0.5f);
    is constantly playing the "Test" animation starting at normalized time 0.5 (halfway) every frame, so it's frozen there.

    The reason for this calculation...
    float correctNormalizedTime = normalizedTime - Mathf.FloorToInt(normalizedTime);

    ...is because we need to get a normalized time between 0 and 1. The way unity works is that whenever an animation loops, it doesn't reset its normalized time to 0, it continues adding on past 1. So when the animation is halfway through on its second loop, the normalized time will be 1.5, not 0.5. Because of that, we need to subtract the whole number from the normalized time unity gives us, so that 6.5 would become 0.5, etc...

    This is just one way of doing it.
     
    Last edited: Mar 27, 2023