struct in UnityEditor.Animations
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseCondition that determines if a transition is taken.
Animator conditions represent a comparison between an animator parameter and a threshold value.
When a condition is assigned to a transition, the animator evaluates the condition to determine if the transition is taken.
When the condition is true, the transition is taken.
A condition is made of three parts: a comparison mode, a parameter name, and a threshold. The parameter is compared to the threshold using the comparison. The parameter is always to the left of the comparison.
For example, a condition with a comparison of Greater
evaluates to true
if the parameter is greater than the threshold.
Not all parameter types are compatible with every comparison modes. If you attempt to use a parameter type with an incompatible comparison mode, an error occurs. Parameters types and their compatible comparison modes are as follows:
Greater
and Less
modes are compatible.Greater
, Less
, Equals
, and NotEquals
modes are compatible.If
and IfNot
modes are compatible.If
mode is compatible.Note that when the comparison mode is If
or IfNot
, the threshold value is ignored.
The following example adds a menu that creates a state machine in the Editor. It uses animator conditions to control when a transition is taken.
using UnityEditor; using UnityEditor.Animations; using UnityEngine; public static class AnimatorConditionExample { [MenuItem("Example/CreateFancyController")] static void CreateController() { AnimatorController controller = new AnimatorController(); controller.AddLayer("Locomotion"); AnimatorState stateWalk = controller.layers[0].stateMachine.AddState("Walk"); AnimatorState stateJump = controller.layers[0].stateMachine.AddState("Jump"); AnimatorState stateDead = controller.layers[0].stateMachine.AddState("Dead"); controller.AddParameter("StartJump", AnimatorControllerParameterType.Trigger); controller.AddParameter("Health", AnimatorControllerParameterType.Int); controller.layers[0].stateMachine.AddEntryTransition(stateWalk); // Use the conditions property to get the current conditions or set new ones. // Here, the state machine transitions from walk to jump if the StartJump trigger is set. // Because this is an If condition mode, you don't have to set a threshold value. AnimatorStateTransition transitionWalkToJump = stateWalk.AddTransition(stateJump); transitionWalkToJump.conditions = new[] { new AnimatorCondition { mode = AnimatorConditionMode.If, parameter = "StartJump", } }; // Transition to dead if the healh parameter is below 1 AnimatorStateTransition transitionWalkToDead = stateWalk.AddTransition(stateDead); transitionWalkToDead.conditions = new[] { new AnimatorCondition { mode = AnimatorConditionMode.Less, parameter = "Health", threshold = 1, } }; // Consider using AddCondition as a shorthand to add a new AnimatorCondition to the conditions list AnimatorStateTransition transitionJumpToDead = stateJump.AddTransition(stateDead); transitionJumpToDead.AddCondition(AnimatorConditionMode.Less, 1, "Health"); // If no conditions are specified, the transition must have an exit time to be valid AnimatorStateTransition transitionJumpToWalk = stateJump.AddTransition(stateWalk); transitionJumpToWalk.hasExitTime = true; AssetDatabase.CreateAsset(controller, AssetDatabase.GenerateUniqueAssetPath("Assets/FancyController.controller")); } }
Additional resources: AnimatorStateMachine, AnimatorTransition.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.