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

Scripts cant keep up with Animator

Discussion in 'Animation' started by UzuStark98, Jan 5, 2015.

  1. UzuStark98

    UzuStark98

    Joined:
    Sep 6, 2014
    Posts:
    5
    Hello,

    Basically what I am trying to do is keep checking where the current animation's normalized time is and if its greater than 0.9f then do the code that is supposed to happen when the animation is done.

    Code (CSharp):
    1. void Update()
    2. {
    3.     Animator anim = GetComponent<Animator>();
    4.     AnimatorStateInfo stateinfo = anim.GetCurrentAnimatorStateInfo(0);
    5.  
    6.     if (stateinfo.IsName("Attack1"))
    7.     {
    8.          if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.9)
    9.          {
    10.             print("It's done!");
    11.          }
    12.     }
    13. }
    now with longer/slower animations the script can keep up with the pace and do the code but with shorter/faster animations it moves too fast for the script to keep up with it....

    How do you suppose i can fix this?
     
    Last edited: Jan 5, 2015
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I would suggest you to use AnimationEvents for this kind of setup. Create an animation event at the end of your clip and do you code in this function, because if you plan to have a lot of state that does the same kind of thing you will end up with a lot of code in your update function which is called at each frame. At the end your code will run faster and your Update function will be more clean.

    If your clip is set to loop, the integer part of normalizeTime should increase by 1 each time the clip loop
    http://docs.unity3d.com/ScriptReference/AnimatorStateInfo-normalizedTime.html

    If your clip is not set to loop, when the animation finish to play normalizeTime should stay at 1.0.

    So i'm not sure why the value is never bigger than 0.9, maybe another script play another clip?
     
  3. UzuStark98

    UzuStark98

    Joined:
    Sep 6, 2014
    Posts:
    5
    First off, Thank you for replying!

    Oh I will test the animation events thing now and tell you the results :D.

    "So i'm not sure why the value is never bigger than 0.9, maybe another script play another clip?"

    If what i am thinking is correct then it never detects the greater than 0.9 because the animation state changes before the script can check whether its greater than 0.9 and when the animation state changes the state.IsName("Attack1") becomes false.

    Edit: Used the Animation Events and it works like a charm :), thnx alot for ur help!
     
    Last edited: Jan 6, 2015
  4. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Do you have a transition that goes from state 'attack' to another state with an exit time greater than 0.9 with a shot duration? if yes that could be the case.
     
  5. UzuStark98

    UzuStark98

    Joined:
    Sep 6, 2014
    Posts:
    5
    No, there is only 1 transition from "Attack1" and that is with a boolean variable.

    But that is ok, the Animation Events did the trick! thanks alot for you help!