Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by ToniGuillem · May 08, 2013 at 09:21 AM · mecanimanimator controllerresetrestart

Restart Mecanim's Animator controller

Is there any way to Reset the Animator controller of an Avatar? When a character dies, I would like to respawn it and restart its Animator controller (playing its default state and with all the parameters set with default values, just like when we Start a new game).

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by meat5000 · Oct 16, 2015 at 07:43 AM

Ok, to finally answer this after all this time:

You must check if the Animator is initalised and if it is not manually invoke a "Rebind" which will re-initialise the Animator.

 if(!anim.isInitialized)
 {
     anim.Rebind();
 }

This is stated in the docs.

For Unity versions pre 4.5 use TonyLi's answer.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TonyLi · Oct 16, 2015 at 02:08 PM 1
Share

Animator.Rebind was only added in 4.5.0. But this is a good answer!

avatar image meat5000 ♦ TonyLi · Oct 16, 2015 at 02:15 PM 0
Share

Ah ok :) Thats why everyone missed it.

avatar image siddharth3322 · Mar 12, 2016 at 12:27 PM 0
Share

Rebind() method works for me as well.

avatar image 5argon · Apr 08, 2017 at 05:02 AM 1
Share

It does not work on 5.6.0f3, by the way. (likely is a bug)

avatar image meat5000 ♦ 5argon · Apr 21, 2017 at 03:32 PM 0
Share

Thanks for the updated info

avatar image huulong 5argon · May 14, 2017 at 03:55 PM 1
Share

Indeed, but it's at the same time a bug and a fix. With the previous system, some users complained that it was unintuitive that the Animator got reset on object deactivation rather than simply pause (the behavior when you only disable the Animator component). The reason was that Unity needed to free memory at that time. In Unity5.6, Unity stores the animator parameters and state aside when you either disable the Animator component or deactivate the game object (if you check the Animator window, you'll see it even enters editor mode so you can change the defaults). Then it restores everything when you reactivate the object / reenable the Animator component.

One example of user being bothered by the original behavior is in http://answers.unity3d.com/questions/919995/parameters-and-states-of-animator-reset-on-objects.html where the answer is actually a store/restore script. Something similar seems to have been implemented directly inside Unity5.6.

Now that I know that, I'm going with one of @TonyLi 's methods (probably 2. and 3.).

EDITED: corrected remark on Animator behavior when disabled

avatar image impurekind huulong · Sep 23, 2018 at 11:59 PM 0
Share

So, if Unity is indeed storing all the parameters and states of the animator as default when you deactivate the object, why isn't there also an option for users to say don't store them? Sometimes you will want to store this stuff, and other times you will absolutely want to wipe it all. Surely this should just a simple bool or something that you turn on or off when you go to deactivate an object, no? Similar to how you can set an object to persistent or not in some programs (dunno about Unity) so that it still there after loading a new level, even though you only created it in the first level/screen or whatever.

avatar image Helical · Aug 15, 2017 at 07:09 AM 0
Share

I would add anim.Update(0f); after the Rebind(), Just to make sure that it playes the first frame of the default state, because it might be in the frame from the last state.

avatar image impurekind Helical · Sep 23, 2018 at 11:20 PM 0
Share

Nope, my agun is still stuck in the exact animation frame (position/rotation) it was on when deactivated. The animation controller says it's back playing the default Idle animation again but that's now what is actually being shown. What is shown is the gun in broken half fire or reload pose with the default Idle animation playing on top of the broken pose. And the only way I seem to be able to fix it is to reset the level entirely.

Any other ideas?

avatar image impurekind · Sep 23, 2018 at 11:20 PM 0
Share

Nope, my agun is still stuck in the exact animation frame (position/rotation) it was on when deactivated. The animation controller says it's back playing the default Idle animation again but that's now what is actually being shown. What is shown is the gun in broken half fire or reload pose with the default Idle animation playing on top of the broken pose. And the only way I seem to be able to fix it is to reset the level entirely.

Any other ideas?

Show more comments
avatar image
3

Answer by huulong · May 14, 2017 at 04:52 PM

As explained in the comments, Unity5.6 stores and restores the animator state on Animator disable/enable and game object deactivate/activate, so I suggest to apply @TonyLi's answers 2 and 3 at the same time. If you only apply 2, the old animator parameters will be preserved and with only 3, the initial state may or may not be reverted depending on how your transitions are built.

I give implementation details below.

Step 1. Reset all animator parameters

You can use this method I wrote as an extension (it is based on this answer which aimed at restoring animator parameters):

 /// Reset all animator parameters to their default
 public static void ResetParameters (this Animator animator) {
     AnimatorControllerParameter[] parameters = animator.parameters;

     for (int i = 0; i < parameters.Length; i++) {
         AnimatorControllerParameter parameter = parameters[i];
         switch (parameter.type)
         {
         case AnimatorControllerParameterType.Int:
             animator.SetInteger(parameter.name, parameter.defaultInt);
             break;
         case AnimatorControllerParameterType.Float:
             animator.SetFloat(parameter.name, parameter.defaultFloat);
             break;
         case AnimatorControllerParameterType.Bool:
             animator.SetBool(parameter.name, parameter.defaultBool);
             break;
         }
     }
 }

Step 2. Play from the initial state

I suggest using a trigger with a standardized name (e.g. "Reset") that you'll use on all your animators that need to be reset. For each layer:

  • add a transition from Any State to your initial state on the condition of the Reset trigger

  • give that transition the highest priority among transitions from Any State

  • make it instantaneous (duration 0, no interruption)

  • check Can Transition To Self in case you want the animation to restart when the game object is already in its initial state.

If you expect to reset your object during certain transitions, make sure those transitions allow Interruption from the Next State.

You may also use Animator.Play (experimental) to play directly a state by name, but don't forget to do this on all the layers.

Final code

Using the extension above:

 myAnimator.ResetParameters();
 myAnimator.SetTrigger("Reset");


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by TonyLi · May 08, 2013 at 02:50 PM

I haven't come across any way to do this easily. Here are some ideas:

  1. Destroy and re-instantiate the character from a prefab.

  2. Manually reset everything in your controller. For transitions, you could have a Boolean parameter named Reset that transitions from Any State to the starting state.

  3. Write a method to reset everything to the default values recorded in the AnimatorController. I describe how to get this information in this answer: http://answers.unity3d.com/questions/418854/getting-a-list-of-mecanim-states-in-animator.html

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToniGuillem · May 09, 2013 at 01:18 PM 0
Share

Thanks for your answer. Anyway, I think it would be really useful to have a Reset method for the Animator already implemented in the engine.

avatar image meat5000 ♦ ToniGuillem · Oct 16, 2015 at 08:12 AM 0
Share

useful to have a Reset method for the Animator already

Its Rebind(). Its been there as long as $$anonymous$$ecanim ;)

Ok apparently added in 4.5.0 (Thanks TonyLi)

avatar image impurekind meat5000 ♦ · Sep 23, 2018 at 11:21 PM 0
Share

Nope, my agun is still stuck in the exact animation frame (position/rotation) it was on when deactivated. The animation controller says it's back playing the default Idle animation again but that's now what is actually being shown. What is shown is the gun in broken half fire or reload pose with the default Idle animation playing on top of the broken pose. And the only way I seem to be able to fix it is to reset the level entirely.

Any other ideas?

Show more comments
avatar image TonyLi · May 09, 2013 at 01:31 PM 0
Share

http://feedback.unity3d.com :-)

avatar image TonyLi · Sep 04, 2015 at 12:13 AM 0
Share

You can disregard the old answer above. When you disable and re-enable an Animator, it resets the entire state.

avatar image TMPxyz TonyLi · Oct 16, 2015 at 07:28 AM 0
Share

just to mention, it doesn't reset the Animator by disable/re-enable on Unity5.

avatar image Rodolfo-Rubens TonyLi · Apr 27, 2017 at 11:09 PM 0
Share

@TonyLi in 5.6 it's not resetting the animator anymore, what the hell.

avatar image TonyLi Rodolfo-Rubens · Apr 28, 2017 at 01:30 AM 1
Share

In earlier versions of Unity 5, as T$$anonymous$$Pxyz wrote, disabling and re-enabling the Animator doesn't reset it, but deactivating and reactivating the GameObject does. In Unity 5.6, this is no longer the case. Nor does Rebind() reset the animator.

This may be a bug that's fixed in 5.6.0p1 (https://issuetracker.unity3d.com/issues/animator-keeps-statemachine-state-when-disabling-gameobject).

@Jix posted a possible workaround here: http://answers.unity3d.com/questions/1334690/unity-56-animatorrebind-doesnt-work-animator-doesn.html

Show more comments
avatar image impurekind · Sep 24, 2018 at 12:11 AM 0
Share

The problem I have is that the controller itself is saying the animations have reset or returned to the default Idle animation state, and the little line below the animation block in the controller indeed shows it playing, but the actual gun in the game is still visually stuck in the position of the animation it was on when the object was deactivated, and the other animations are playing over the top of this. So, it doesn't seem to me that the issue is with the controller, or that is can even be fixed with the controller, because the controller itself appears to be doing exactly what it should. The object, however, isn't resetting back to whatever pose it should be in by default, before any other animations were played or whatever state the object was in when deactivated. It doesn't seem to matter what solution I try if it relates to the actual animation controller, and I've tried sooo many, as the issue seems to happening kinda irrespective of what the animation controller is doing (even though it's a pose, position and rotation, from an animation that is getting stuck on the object in my case when it's deactivated and then reactive). And, note, I'm doing all my animations directly in Unity. $$anonymous$$y friend, who did his animations in $$anonymous$$ax and then just loaded them into Unity, seems to have no problems, and all the solutions people are mentioning seem to actually work for him.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

unity freezes when running sprinting animation 0 Answers

C# Help with setting up jump animation 0 Answers

How do I check, and change, the current frame/time of a sprite animation in Unity 4.3? 0 Answers

Mecanim - Transitioning to the same animation in reverse 0 Answers

How do I use an animated prop with a Humanoid rig. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges