- Home /
How to reset animator controller ?
Hello. I hay a question regarding animations,animation controllers,animators Is there a way how to "restart" animatorcontroller ?
For example :
If i have 2 guns with their own animations and animator controllers and switch betwean guns. One gun gets disabled and the other gun gets activated the animations gets stuck in weird position/rotation.
So is there a way how to restart the animator so animations would play normally without weird po sitions/rotations.
I have tried - creating a boolean and forcing the animator to play the 1st animaton ( taking gun out )
- disabling the animator and reactivating it - destroying the animator component and creating it again ( didn't work probably wrong setup) - I have also tried the Animator.Rebound() -don't know how but it didn't work Anybody knows how to fix this ? - I have also heard about deleting animation defaults or something like that ( didn't tried - don't know how )
I am using Unity 2017.3
Answer by Feelnside · Jun 20, 2018 at 06:04 PM
Probably the best way it's just start the idle animation after the switching a gun. For example:
anim_gun.Play("idle", -1, 0f);
'idle' is your animation which should play
'-1' is the layer, -1 means default (first)
'0f' is start time of the animation
This simply does not work for me.
The actual animation controller says the Idle animation is playing but the gun in still showing whatever animation position/frame it was on when the object was deactivated, and the Idle animation is simply playing over the top of that pose. So the animation is still essentially broken.
It's pretty much a Class A bug from what I can see, as there is apparently no way to properly wipe the stuck animation pose other than to reset the level entirely.
Unless you have any other suggestions?
I was having the same issue earlier. Specifying the normalizedTime as zero is supposed to force the animation to start from there, but it doesn't. Oddly enough, it used to in older versions of unity. I think it's like you said, a class A bug. Animator component has been very buggy for as long as I can remember.
Answer by sarahnorthway · Jan 21, 2021 at 05:22 AM
This works for me, at least for a simple case. It returns the AnimatorController to the Entry state:
Animator anim = gameObject.GetComponentInChildren<Animator>();
anim.Rebind();
anim.Update(0f);
Answer by impurekind · Sep 23, 2018 at 11:39 PM
This isn't the solution but there REALLY should be something as simple as Unity having some in-built function that when you use gameObject.SetActive(false); you can also select to save the object's states or not. And then when you reactivate the object it will either be reset completely or be in exactly the same states it was when you deactivated it. It really should be THAT simple, something like gameObject.SetActive(false, true); with the first argument saying if it's going to be activated or deactivated and the second saying whether you want to save all its states when deactivated or not. So if I set it to false, false then when I reactivate my gun every single thing about it will be back to the default state, on all its components and children and their components too. I mean, maybe I'm simplifying it, but you get the point.
Answer by canyon_gyh · Apr 22 at 09:14 AM
I think need SetActive(false) for Animator When want to Play ,First SetActive(true) , Second anim.Update(0f); anim.Play("idle", -1, 0f);