- Home /
Mecanim Animation Parameter Types: Boolean vs. Trigger
Hi everyone. I'm using Unity 4.3.1 and in the animation state machine window, I can add 4 types of parameters: int, float, Bool and Trigger. What is the difference between Bool and Trigger?
They seem to be the same (both have checkbox interface). There's nothing about the "Trigger" parameter type in Unity Docs. Instead, there's a vector type that's no longer in Unity. http://docs.unity3d.com/Documentation/Manual/AnimationParameters.html
Answer by tanoshimi · Dec 20, 2013 at 04:05 PM
Trigger is new added in 4.3. My understanding is that it is a "one-shot" Boolean, which can be triggered (i.e. set to "true") for a single frame only, after which it automatically reverts back to false.
Prior to this, it was somewhat difficult to implement state transitions that you wanted to take immediate effect but then return (i.e. such as firing a gun), because you had to wait for the transition to begin before resetting the Bool with logic such as:
// Set the Bool to transition to the firing state
animator.SetBool("Firing", true);
// Once transition has begun, reset the bool
if (animator.GetNextAnimatorStateInfo(0).IsName("Firing")) {
animator.SetBool("Firing", false);
}
It actually appears that its not just set for one frame. A trigger is set true until it is consumed by a state change. Only after that does it return to false.
What @ahorne said is the most important aspect of the trigger compared to bool (the idea is similar just better explained than the answer). I had a state machine that got 2 conflicting state change events in one frame. One was float change and the other was bool (from false to true). Bool was true for a very short time, but the float transition was getting priority and until its state finishes the bool was already false and did not trigger his transition. Trigger solves this by "remembering" the event until it is used by some transition.
Parameter values are only changed when code changes them. If you had a bool that was "true for a very short time", then one of your script's code was setting it to false before you wanted it to be. Triggers manage this for you, and some of the time that may be exactly what you want.
Answer by Sylker · Sep 09, 2014 at 01:28 PM
It works like this:
Let's say you have an animation that plays when a bool is set to true. After the animation is complete the bool should be set to false again in order to transit to another state.
This used to be achieved by code, but now you can use a Trigger instead. It's a bool that will be set to false automatically after the completion of animation.
Quite useful, huh?
Actually it is not set to false after animation is complete, neither is it set to false after 1 frame - it is directly set to false ("reset") when $$anonymous$$ecanim triggers it. If you have the same trigger as a condition for more state transitions (for example on another layer) that get triggered at the "same time" only the first from the top will activate and trigger will be already reset when $$anonymous$$ecanim will be considering triggering the second one.
Answer by ritesh_khokhani · Apr 17, 2015 at 07:21 AM
For kind of info,
In unity's parameter windows both Trigger and Bools seems same,
But from Transition's inspector window one can disguise it, for Bools it shows checkbox while for Trigger it won't..