- Home /
Mecanim Trigger getting stuck in 'TRUE' state
So, I have some triggers in the Animation Controller - these work fine most of the time... I call SetTrigger() from the code and they trigger once and then are immediately false - waiting to be triggered again. But occasionally i notice that the anim is auto-happening when it should be waiting for the trigger. When i check in the Animation Controller I can see a little tick next to the trigger indicating it's constantly turned on... behaving like a 'bool' - ... this seems to be a Unity bug... anybody got any ideas ? Thanks
I'm seeing this as well for my melee attack cycle, definitely seems like a Unity bug, nothing that I'm doing seems like it should cause this.
Yeah, it seems to be a bug. I am stuck with that too and using bool ins$$anonymous$$d. Gotta set it to false all the time =/
I've also encountered this bug in 5.1.1f. I was not able to find an open ticket in the issue tracker on it.
It isnt a bug,
The trigger is an auto managed bool, if you set a trigger it will stay true until a transition is triggered.
not just any transition either...it must be one that uses the trigger as a condition
This probably comes down to just a few things.
Ti$$anonymous$$g! Polling states can often result in a mismatch of ti$$anonymous$$g meaning the animation is looped or fired as the off condition is not set in time or has been totally missed.
Not Clearing a Set item. http://docs.unity3d.com/ScriptReference/Animator.ResetTrigger.html
Exit Time parameter - This is notorious for messing up even the simplest of Anim chain ti$$anonymous$$gs.
I've also had a similar experience although the ResetTrigger did not help unless I put it into a check for the next state.
if (Animator.GetCurrentStateInfo(0).nameHash = _defaultState)
{
Animator.ResetTrigger("AnimationYouWantToReset");
}
Answer by georgeq · May 31, 2015 at 06:02 PM
Ultroman's answer is not 100% exact. The problem does not (always) occur when you trigger an animation that is currently being played. It does occur when you trigger an amination while a transition is still running.
Let's say you have 2 animations: A and B and 2 triggers: tA and tB, animation A plays when tA gets triggered and animation B plays when tB gets triggered. If you trigger tA animation A will start playing, but if you trigger tB before animation A has finished, tA won't be reset, it will remain true until animation A is played from begining to end. The same also happens if you trigger tA for a second time while animation A is still playing.
So, if for some reason you need to trigger tB and you are not sure if animation A has finished, you should use ResetTrigger, I didn't understand why the guys at Unity decided to include this funtion until I stumbled upon this problem.
To prevent this unwanted behaviour you should do something like this:
animator.ResetTrigger("tA");
animator.SetTigger("tB);
Answer by Ultroman · Nov 19, 2014 at 12:44 PM
2021 update: Check out the answer above instead or follow this link: http://answers.unity.com/answers/977718/view.html
ORIGINAL ANSWER: I found out that there is a problem with Mechanim, if you try to trigger the same animation that is already running. I don't think it is built for that. It is a state-machine after all, so it makes sense that it should never trigger an animation that is already running.
You "simply" have to check if the animation you're starting is already running.
I've done these steps to ease my life:
Made one trigger for each of my animations, with the same name as the animation I want to trigger! (this is important, if you want to use the simple functions below)
Made a transition from "Any State" to each of my animations, and the only condition for each transition, is the matching trigger.
Then I made a PlayAnimation function, which checks if the animation is already running:
.
public void PlayAnimation(string animationName){
if (!_animator.GetCurrentAnimatorStateInfo(0).IsName(animationName))
_animator.SetTrigger(animationName);
}
...and here's one where you can control the layer you're checking:
public void PlayAnimation(string animationName, int layer){
if (!_animator.GetCurrentAnimatorStateInfo(layer).IsName(animationName))
_animator.SetTrigger(animationName);
}
The IsName() function checks if the string you pass it, is the name of the currently playing animation. If it isn't, I use the trigger with the same name. Simple.
This works for me.
The animations trigger instantly, though.
I don't know if this interrupts non-atomic animations.
EDIT: Since doing this, I somehow got my Animator working normally, so now I can just do:
_animator.Play(animationName);
It doesn't screw up anymore, and I don't need to use triggers at all. And also, I can do this to restart the running animation from scratch, even if it is the same animation:
_animator.Play(animationName, layer, 0.0f);
I don't know why, but this wasn't working yesterday. _animator.Play() just didn't do anything.
Thanks to meat5000 for pointing out a glaring error in my last edit.
Thank you for the good answer. You can make it more flexible by using static classes to hold variable information so you could end up with code something like:
if(_currAnimatorBaseState.nameHash != $$anonymous$$yAnimatorState.Idle){ _animObj3D.SetTrigger(AnimatorConditionNames.$$anonymous$$akeIdle);}
where you have, for example, $$anonymous$$yAnimatorState being
public static class $$anonymous$$yAnimatorState { public static int Idle= Animator.StringToHash("Base Layer.Idle"); }
Just my two cents.
Answer by tjPark · Dec 15, 2015 at 09:02 AM
This is how I did in my game after reading the articles.
string currentAnimation = null;
void setAnimation(string stringInput){
Animator anim =transform.GetComponent<Animator>();
Debug.Log("Setting Anim to : " + stringInput);
if(currentAnimation==stringInput){
}else{
if(currentAnimation!=null){
anim.ResetTrigger(currentAnimation);
}
anim.SetTrigger(stringInput);
currentAnimation=stringInput;
}
}
This is a tidy snippet that solved the issue I was having, thanks!
Answer by Aily · Jun 05, 2016 at 09:04 AM
you need to turn on flag "can transition to self" in transition settings rollout.
I wish I could give you more than 1 vote. I discovered your comment after it took me 2 hours to figure out that "can transition to self" was not checked!
Answer by guy.moreillon · Dec 02, 2015 at 03:41 PM
For what it's worth, I've had this problem when setting the trigger while in a state that had no exit condition based on that trigger. The trigger was simply not consumed.
Your answer
Follow this Question
Related Questions
How to check if trigger from mecanim is true or false. 1 Answer
Mechanim Trigger Issue 1 Answer
NetworkAnimator: SetBool vs. SetTrigget? 1 Answer
Basic animation key press 0 Answers
Mecanim Trigger event on specific frame of an animation 4 Answers