'unity animation trigger inspection
When setting a trigger in unity animator, and the state machine is not in a state where it can invoke the trigger, the trigger is "charged" and will set off the moment it can. how can I check if the trigger is set, and how can I uncheck it?
Solution 1:[1]
Checking if a certain Trigger is currently set is actually harder than I thought ^^
However, you can reset a trigger by using Animator.ResetTrigger
.
Exactly to avoid your issue I once created a script that simply resets all triggers whenever entering a new state.
using System.Linq;
using UnityEngine;
public class ResetAllTriggers : StateMachineBehaviour
{
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
foreach (var parameter in animator.parameters.Where(parameter => parameter.type == AnimatorControllerParameterType.Trigger))
{
animator.ResetTrigger(parameter.name);
}
}
}
This makes sure that in the beginning of every state all triggers are reset => Only triggers actually set during the current state are handled and never stored for later.
In order to not have to attach it to each and every state you can simply put this Behaviour to the according Layer of the state machine! It is then simply invoked whenever any state calls OnStateEnter
.
Click on the according Layer in your AnimatorController e.g. the BaseLayer
Then click on Add Behaviour and add the reset behaviour
As you can see the trigger is now reset every time a new state is entered
Solution 2:[2]
You can get the current value of a trigger by doing the following:
Animator animator = GetComponent<Animator>();
bool triggerValue = animator.GetBool("TriggerName");
A trigger is just a boolean that gets consumed (turned to false) after it is used, so you can access the value the same way as you do a boolean.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | β.εηοιτ.βε |