Search Unity

AnimatorStateInfo.normalizedTime

Discussion in 'Documentation' started by rex_francorum, Apr 21, 2020.

  1. rex_francorum

    rex_francorum

    Joined:
    Jun 25, 2019
    Posts:
    17
    Hi, I'm a beginning C#/Unity student and I'm attempting an assignment for my class. For the assignment we have to Destroy an object once the animation for it has ended. The teacher has provided the code and his solution is as follows:

    Code (CSharp):
    1.  // destroy the game object if the explosion has finished its animation
    2.         if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.5)
    3.         {
    4.             Destroy(gameObject);
    5.         }
    I don't know where the 0.5 comes from (the animation has 9 frames) and the documentation for the GetCurrentAnimatorStateInfo method and normalizedTime property are hard for me to parse. Could some explain the layerIndex parameter to me in GetCurrentAnimatorStateInfo and/or how normalizedTime works. As far as I can tell, the 0.5 for normalizedTime would be that it looped 0 times and is 50% of the way through the animation loop. Is that correct? Any help much appreciated.

    https://docs.unity3d.com/ScriptReference/AnimatorStateInfo-normalizedTime.html
    https://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Yup, the normalized time is between 0 and 1, where 0 is the start of the animation and 1 is the end. That's what "normalized" means, for time, vectors, whatever.

    But, yeah, wow the docs for normalizedTime looks like it was written by a drunk monkey. @jo-unity, it should probably get an entry in the mysterious docs backlog!

    GetCurrentAnimatorStateInfo is pretty well-documented, though the example uses the deprecated OnGUI to show some info.
     
    EWimsett likes this.
  3. Deleted User

    Deleted User

    Guest

    Hi :)
    Same here ;) I guess we are in the same class xD

    I still have problems grasping the 0.5 parameter.

    Well, if it is the first loop and 50% of it's way through the sprite sheet the whole explosion does not show, right?
    The strange thing is, I can start seing the whole explsion animation with a parameter of 2.0. (not below, like 1.0)

    All the bst
    Moody
     
  4. rex_francorum

    rex_francorum

    Joined:
    Jun 25, 2019
    Posts:
    17
    Oh nice! Dr. Chamillard? I tried to email him a while ago, but couldn't find an email address. Played around with it and it does play a longer animation if you do 1.0. 2.0 to me looks like it plays the whole animation twice. You can see a little clipping hiccup as it restarts. If you need someone to bounce ideas off of for the class I can give you my Discord: luc#4583
     
  5. Danas-Glodas

    Danas-Glodas

    Joined:
    Mar 10, 2020
    Posts:
    1
    You can reduce speed of explosion animation itself in Animator window in Unity editor. Click on orange rectangle with "Exploding" on it, and then, in Inspector change Speed parameter to 0.5 or 0.75.
     
  6. Naheedmir

    Naheedmir

    Joined:
    Sep 8, 2020
    Posts:
    12
    Hi Rex,
    Just opt for the following steps for animator stateInfo.
    1. Lower down the speed of explosion animation
    2. Pinch the Orange rectangle with explosion on
    3. Modify the speed parameter to .0.5 or 0.75
     
  7. EWimsett

    EWimsett

    Joined:
    Feb 16, 2017
    Posts:
    11
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    It just means that if the normalizedTime is 2.3, it's looped two times and is .3 of the way through the third loop.

    I think they updated the documentation since 2020. The docs are good now, and does not anymore look like they were written by a drunk monkey. So that's good!
     
    EWimsett likes this.
  9. EWimsett

    EWimsett

    Joined:
    Feb 16, 2017
    Posts:
    11
    Ahh gotcha :) thanks mate!

    I was referencing the current 2022 LTS docs unfortunately, perhaps a previous doc was updated and this one wasn't?