Search Unity

Question Looping a part of animation from event

Discussion in 'Animation' started by raulatreides, May 23, 2022.

  1. raulatreides

    raulatreides

    Joined:
    Jun 10, 2020
    Posts:
    4
    Hi, I'm new to Unity, learning in a course and we are using Mixamo animations for practice.
    I'm trying to create a "continued" attack from the "2 hand wide spell", but can't loop the animation, because it has all the states (begin movement, spellcast, return to normal).

    I would like to insert an event in the animation, calling a script to check if spell key still pressed and if so, loop to few frames back, to create the illusion of a mantained spell.
    I have tried to find a way, but no example seems to be accurate to my needs. And as is a very specific need, I don't know how to search better.

    Thanks a lot.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hello,

    You can set an animation's normalized time through code:
    GetComponent<Animator>().Play("Attack", 0, 0.5f);
    "Attack" is the name of the state, 0 is the layer in the animator, and 0.5f is the normalized time.

    If you're unfamiliar with normalized time, it's just at what point in the animation you're at; 0 being the beginning and 1 being the end. So 0.5 would be the middle.

    To loop the middle of the animation, you can create an event at the end of the looped area that sets the animation back to the beginning of the loop using normalized time. It's a pain to find what normalized value equates to what point in an animation, so I honestly wouldn't recommend doing this. It also won't loop seamlessly.
     
    raulatreides likes this.
  3. raulatreides

    raulatreides

    Joined:
    Jun 10, 2020
    Posts:
    4
    Thank you very much, Unrighteouss. I will try your way and see what happens. As I'm now working in UI in class, maybe I'll try later and wanted to thank you asap.
    If it works I'll post the results.
    Thks again.