# 2. States, Transitions, and Parameters Part of [[Animation]]. Prev: [[1.Clips Avatar and Animator]]. Next: [[3.Blend Trees]]. **Docs:** Manual 6000.5 — Animation Parameters; Animation transitions (Exit Time, Fixed Duration, Interruption Source, Ordered Interruption); Animation State Machine / Sub-State Machines. ## 2.1 Parameters — the script API surface In the Animator window → **Parameters** (+): | Type | Script API | Notes | |---|---|---| | Float | `SetFloat` / `GetFloat` | Blend trees, speed, aim angles. Optional damp time overload. | | Int | `SetInteger` / `GetInteger` | Weapons, stance enums. | | Bool | `SetBool` / `GetBool` | Grounded, aiming, crouching. | | Trigger | `SetTrigger` / `ResetTrigger` | One-shot: consumed when a transition uses it. | ```csharp using UnityEngine; [RequireComponent(typeof(Animator))] public sealed class LocomotionAnimDriver : MonoBehaviour { static readonly int SpeedHash = Animator.StringToHash("Speed"); static readonly int GroundedHash = Animator.StringToHash("Grounded"); static readonly int JumpHash = Animator.StringToHash("Jump"); [SerializeField] float speedDampTime = 0.1f; Animator _anim; float _speed; // set by your movement code each frame void Awake() => _anim = GetComponent<Animator>(); public void SetPlanarSpeed(float metersPerSecond) => _speed = metersPerSecond; void Update() { // Damp time smooths blend-tree parameters (verified overload: SetFloat(id, value, dampTime, deltaTime)). _anim.SetFloat(SpeedHash, _speed, speedDampTime, Time.deltaTime); } public void SetGrounded(bool grounded) => _anim.SetBool(GroundedHash, grounded); public void Jump() => _anim.SetTrigger(JumpHash); } ``` Hash parameters once. String overloads allocate and are slower in hot paths. **Gameplay vs Animator state:** keep AI / player *logic* in a code state machine ([[Enemy AI State Machine]], State in [[Architecture and Code Patterns]]). The Animator mirrors *presentation*. The brain sets parameters; it does not scatter `if (anim.GetCurrentAnimatorStateInfo…)` through gameplay. ## 2.2 Transitions Select a state → make a transition arrow to another (right-click state → Make Transition). Inspector: | Property | Meaning | |---|---| | **Has Exit Time** | Transition may fire only after normalized time ≥ **Exit Time** (e.g. `0.75` = 75% into the clip). With conditions, Unity still waits for Exit Time first when this is checked. | | **Exit Time** | Normalized time; `< 1` re-evaluates each loop; `> 1` evaluates once after N loops (e.g. `3.5`). | | **Fixed Duration** | If checked, **Transition Duration** is seconds; if unchecked, fraction of source state length. | | **Transition Duration** | Blend length. | | **Transition Offset** | Where in the destination clip to start (0.5 = halfway). | | **Interruption Source** | None / Current State / Next State / combinations — who may interrupt this blend. | | **Ordered Interruption** | How the interruption queue is parsed. | | **Conditions** | All must be true (AND). Parameter + predicate + value. | Solo Exit Time (no conditions) = "play this attack until 90%, then return." Conditions without Exit Time = reactive (flinch as soon as `Hit` is true). Mixing both = "don't cancel the startup frames." ### Interruption (fundamentals) Any State transitions are conceptually queued first. Then, based on **Interruption Source**, transitions from the current and/or next state are considered. This is how a flinch can cancel a wind-up, or how you *prevent* a flinch from canceling recovery. Deep interruption matrices → later Animation advanced topic; here: start with **None** on attack recovery transitions, and **Current State** when you want grounded→airborne to cancel a walk blend. ## 2.3 Any State **Any State** → `HitReact` with condition `Trigger Hit` is the classic pattern. Caveats: - It can re-enter `HitReact` from itself unless you exclude or use a gate bool. - Prefer a short list of Any State transitions; otherwise debugging "why did I leave Idle?" becomes archaeology. ## 2.4 Sub-State Machines Right-click graph → **Create Sub-State Machine** (e.g. `Combat`). Inside: Idle/Attack/Block/Hit. Outside: a single transition into `Combat` and an **Exit** node back to locomotion. - **Enter** / **Exit** nodes connect the nested graph to the parent. - Use for stance groups (OnFoot vs InVehicle) without a spaghetti base layer. - Parameters are shared with the parent controller — no separate parameter tables. This mirrors hierarchical FSMs in code ([[Enemy AI State Machine]] Alert super-state) but stays presentational. ## 2.5 StateMachineBehaviour (optional hook) Attach scripts to a *state* (Inspector → Add Behaviour) for enter/exit callbacks without polling: ```csharp using UnityEngine; /// Fires a UnityEvent-style hook when this state is entered. /// Not compiled in Editor for this note set. public sealed class AnimStateEnterHook : StateMachineBehaviour { public string message = "OnAnimStateEnter"; public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { animator.SendMessage(message, SendMessageOptions.DontRequireReceiver); } } ``` Prefer explicit drivers (`LocomotionAnimDriver`) for core gameplay. Behaviours are good for SFX/VFX cues tied to a specific state when Animation Events are too clip-specific. ## 2.6 Verified vs unverified **Verified:** parameter types and `SetFloat`/`SetInteger`/`SetBool`/`SetTrigger`/`ResetTrigger` (Manual — Animation Parameters); Exit Time, Fixed Duration, Transition Duration/Offset, Interruption Source values, Ordered Interruption, Conditions AND semantics (Manual — Animation transitions); Any State / Sub-State Machine as standard Mecanim features. **Unverified:** `AnimStateEnterHook` message pattern; your specific interruption matrix for combat. **Not compiled:** all scripts in this part.