Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animator control, reset current animation being played.

Discussion in '2D' started by BindingForceDev, Dec 17, 2014.

  1. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    Hi all! Quick Animator component question...

    I am having trouble finding out how I can reset the animation that was playing to its first frame after stopping an animation by executing (roughly, after getting a reference to the Animator component) animator.speed = 0; (which just freezes the current animation state at its current time position). I cant seem to find a way to directly reveal the animation being played by the controller's current state so that I can rewind it or do something else comparable. Any thoughts? I suppose I could just use animations attached to the gameObject, but I did already try that and had a LOT of trouble even getting them to play in the first place (even by trying to make them all legacy) so I decided to use the an Animator component and animation states. It's a real bummer having my character stop animating mid step...
     
    hopetolive likes this.
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    ive kind of had the same issue or trying to reset an animation to the first frame after a set event, ive got a sort of hacked up workaround that worked in my simple game for a falling enemy that hits the ground while the animation is paused at frame 0, plays animation when it hits, resets position and resets animation and then rinse and repeat. this is how I managed to get it to reset the animation to start again. I only have one animation state/clip in my animator controller.


    from the scripting reference
    http://docs.unity3d.com/ScriptReference/Animator.Play.html

    it states one of the implementations of
    public void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity);

    in my case enemyAnim is a reference to the Animator component.
    Code (CSharp):
    1. enemyAnim = GetComponent<Animator>();
    so what i did was go into the Animator window and take the name of the state that i want to reset, in my case "enemy_satellite_anim", its on the base layer in the animator window so i use '0' for the second parameter and then the timeframe that i want it to start on, and i wanted it to be the start so i used '0.0f'. directly after that i set the animation speed to 0;

    Code (CSharp):
    1. enemyAnim.Play ("enemy_satellite_anim",0, 0.0f);
    2. enemyAnim.speed = 0;
    seems to work for me in this case, highly likely there is a better way of doing it, but as im only learning myself, its the only one i can figure out :(
    hope it helps.
     
    Last edited: Dec 18, 2014
    awsapps and BindingForceDev like this.
  3. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    Wow, thanks for the great idea; this should help a lot. I haven't taken the time yet to try your solution in my game, but I think this will work just fine. In my setup I'll have to get the current state name as they are changing on the fly as the player moves but I'm pretty sure I can get access to the nameHash string by creating an AnimatorStateInfo object (which contains this field) with the Animator class's GetCurrentAnimatorStateInfo() function. I feel like this should be far easier than all this tho, but whatever works right? :)
     
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    no worries, glad to put up some sort of workaround for what i've encountered as i learn.
    but hey, that's what its about and what makes this community fantastic for me as a learner as everyones got the same view and chips in where they can :)
     
    Last edited: Dec 21, 2014
    BindingForceDev likes this.
  5. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    It looks like this does work, just needed to...
    Code (JavaScript):
    1. //Get Animator state after storing the Animator reference in my var 'animate'.
    2. var currentState : AnimatorStateInfo = animate.GetCurrentAnimatorStateInfo(0);
    3. var stateName : int = currentState.nameHash;
    4.  
    5. //Then later I can call...
    6.  
    7. //Reset Animation on first frame.
    8. animate.Play(stateName,0, 0.0f);
    9. animate.speed = 0;
    It does appear one cannot directly access the string value for the name of the currently playing animation state, however, the Animator.Play(..,..,..) function has an overload that allows the nameHash (an int value) to be used instead of that string for the first parameter.

    Anyway, I hope this helps others as well!!!