Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to reset non-looping animation in animator?

Discussion in 'Animation' started by spvn, Feb 21, 2019.

  1. spvn

    spvn

    Joined:
    Dec 10, 2013
    Posts:
    80
    This seems like an awfully simple question but I've googled extensively.

    I have a non-looping animation that gets played once and just pauses at the end of the animation. When the object disables and re-enables, I want to be able to replay this non-looping animation from scratch from a function call. But problem is when I re-enable this object, the animation is still stuck in the last frame even before I can call Play() again.

    So far googling has only brought up doing something like this when I re-enable the object

    Code (CSharp):
    1.  
    2. private void OnEnable()
    3.     {
    4.         spinningAnimator.Play("spinning", -1, 0);
    5.         spinningAnimator.speed = 0;
    6.     }
    7.  
    and then set animator speed to 1 when I want the animation to start playing.

    Is this the only way?! Surely there's something more elegant to simply reset the "playhead" for a specific animation clip?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,575
    I suspect that's the best way you're going to find when using Mecanim. Another option would be to play an idle animation instead of using zero speed, but that would take a bunch more work to set up than a single line of code.

    What is it you don't like about that approach?