- Home /
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).
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.
Animator.Rebind was only added in 4.5.0. But this is a good answer!
It does not work on 5.6.0f3, by the way. (likely is a bug)
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
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.
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.
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?
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?
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");
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:
Destroy and re-instantiate the character from a prefab.
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.
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
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.
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)
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?
You can disregard the old answer above. When you disable and re-enable an Animator, it resets the entire state.
just to mention, it doesn't reset the Animator by disable/re-enable on Unity5.
@TonyLi in 5.6 it's not resetting the animator anymore, what the hell.
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
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.