From unity
Unity 6 animation system guide. Use when working with Animator Controllers, animation state machines, blend trees, animation clips, Avatar system, humanoid rigs, root motion, animation events, Timeline, or Cinemachine. Based on Unity 6.3 LTS documentation.
npx claudepluginhub cdata/aria-skills --plugin unityThis skill uses the workspace's default tool permissions.
Unity's Mecanim animation system is built on three interconnected components:
Provides GDScript and C# examples for Godot 4.3+ animations using AnimationPlayer for playback, AnimationTree for blending and state machines, sprite animation, and code-driven effects.
Unity 6 Cinemachine camera system guide. Use when working with virtual cameras, camera blending, follow cameras, FreeLook, camera shake, or state-driven cameras. Covers Cinemachine 3.x API. Based on Unity 6.3 LTS documentation.
Guides Three.js animations using keyframe tracks, AnimationClip, AnimationMixer, morph targets, skeletal rigs, and blending. Use for object animation, GLTF playback, procedural motion.
Share bugs, ideas, or general feedback.
Unity's Mecanim animation system is built on three interconnected components:
The Animator component is attached to GameObjects and references both the Animator Controller and Avatar assets needed for playback.
An Animator Controller asset arranges Animation Clips and Transitions for a character or animated GameObject.
Creating: Right-click in Project window > Create > Animator Controller
Four types are available:
| Type | Description | Script Method |
|---|---|---|
| Float | Decimal number | SetFloat() / GetFloat() |
| Int | Whole number | SetInteger() / GetInteger() |
| Bool | True/false | SetBool() / GetBool() |
| Trigger | Auto-resetting bool | SetTrigger() / ResetTrigger() |
using UnityEngine;
public class PlayerAnimController : MonoBehaviour
{
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
// Note: Uses legacy Input Manager for simplicity. See unity-input for the new Input System.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool fire = Input.GetButtonDown("Fire1");
animator.SetFloat("Forward", v);
animator.SetFloat("Strafe", h);
animator.SetBool("Fire", fire);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("Enemy"))
{
animator.SetTrigger("Die");
}
}
}
Replaces animation clips in an Animator Controller while keeping structure, parameters, and logic intact. Useful for multiple characters sharing the same state machine but using different clips.
Critical: Set transition exit times in normalized time (not seconds) when using Override Controllers, or exit times may be ignored if override clips have different durations.
Each state in the Animator Controller represents a distinct action. Special states include:
Transitions define how states blend into each other.
| Setting | Description |
|---|---|
| Has Exit Time | Transition triggers at a normalized time (e.g., 0.75 = 75% complete) |
| Transition Duration | Blend period; in seconds (Fixed Duration) or fraction of source state |
| Transition Offset | Where destination state begins playback (0.5 = midpoint) |
| Conditions | Parameter-based rules; all must be satisfied simultaneously |
| Interruption Source | Which transitions can interrupt: None, Current State, Next State, or combinations |
| Ordered Interruption | Whether transition parsing stops at current transition or any valid one |
When both Has Exit Time and Conditions are set, Unity only checks conditions after the exit time.
Scripts inheriting StateMachineBehaviour attach to states. Callbacks: OnStateEnter, OnStateUpdate, OnStateExit, OnStateMove, OnStateIK. All receive (Animator animator, AnimatorStateInfo stateInfo, int layerIndex).
public class AttackState : StateMachineBehaviour
{
public AudioClip attackSound;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
AudioSource.PlayClipAtPoint(attackSound, animator.transform.position);
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// Cleanup when leaving state
}
}
Blend Trees smoothly blend between multiple similar animations based on parameter values, unlike transitions which switch between distinct states over time.
Critical requirement: Animations must be of similar nature and timing. Foot contact points should align in normalized time (e.g., left foot at 0.0, right foot at 0.5).
Creating: Right-click in Animator Controller > Create State > From New Blend Tree. Double-click to enter the graph. Add child motions via the Inspector.
// Driving a locomotion blend tree from script
void Update()
{
float speed = new Vector3(rb.velocity.x, 0, rb.velocity.z).magnitude;
float direction = Vector3.SignedAngle(transform.forward,
rb.velocity.normalized, Vector3.up);
animator.SetFloat("Speed", speed, 0.1f, Time.deltaTime);
animator.SetFloat("Direction", direction, 0.1f, Time.deltaTime);
}
The Avatar system identifies models as humanoid and maps body parts for animation retargeting.
Root motion transfers animation-driven movement to the GameObject's Transform.
| Setting | Purpose |
|---|---|
| Bake Into Pose (Rotation) | Orientation stays on body; GameObject receives no rotation |
| Bake Into Pose (Y) | Vertical motion stays on body; enable for all except jumps |
| Bake Into Pose (XZ) | Horizontal motion stays on body; enable for idle clips to prevent drift |
| Based Upon | Body Orientation (mocap), Original (keyframed), Feet (prevents floating) |
Animator.gravityWeight is driven by Bake Into Pose Position Y: enabled = 1, disabled = 0.
// Custom root motion handling
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class RootMotionController : MonoBehaviour
{
Animator animator;
CharacterController controller;
void Start()
{
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
animator.applyRootMotion = false; // We handle it manually
}
void OnAnimatorMove()
{
// Apply root motion through CharacterController
Vector3 deltaPosition = animator.deltaPosition;
deltaPosition.y -= 9.81f * Time.deltaTime; // Add gravity
controller.Move(deltaPosition);
transform.rotation *= animator.deltaRotation;
}
}
Animation events trigger functions at designated points in the animation timeline.
using UnityEngine;
public class FootstepHandler : MonoBehaviour
{
public AudioClip[] footstepSounds;
public GameObject dustPrefab;
// Called by animation event -- function name must match event
public void PlayFootstep(int footIndex)
{
if (footstepSounds.Length > 0)
{
int clipIndex = Random.Range(0, footstepSounds.Length);
AudioSource.PlayClipAtPoint(footstepSounds[clipIndex], transform.position);
}
}
// Called by animation event passing an Object parameter
public void SpawnEffect(Object effectPrefab)
{
Instantiate((GameObject)effectPrefab, transform.position, Quaternion.identity);
}
}
Timeline creates cinematic content, gameplay sequences, audio sequences, and particle effects. Package: com.unity.timeline (v1.8.11, Unity 6.3 compatible).
| Track | Purpose |
|---|---|
| Animation | Controls Animator on bound GameObject |
| Audio | Plays AudioClips on bound AudioSource |
| Activation | Enables/disables bound GameObject |
| Signal | Fires events at specific times via SignalReceiver |
| Control | Triggers sub-Timelines, particle systems, or other Playable Directors |
| Playable | Custom track using Playables API |
Requires: Humanoid Avatar, IK Pass enabled on layer.
using UnityEngine;
public class IKController : MonoBehaviour
{
Animator animator;
public bool ikActive = true;
public Transform rightHandTarget;
public Transform lookTarget;
void Start()
{
animator = GetComponent<Animator>();
}
void OnAnimatorIK()
{
if (animator == null || !ikActive) return;
// Look at target
animator.SetLookAtWeight(1f);
animator.SetLookAtPosition(lookTarget.position);
// Right hand IK
animator.SetIKPositionWeight(AvatarIKGoal.RightHand, 1f);
animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1f);
animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandTarget.position);
animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandTarget.rotation);
}
}
// Play state immediately
animator.Play("Attack");
// Play with normalized time offset (start at 50%)
animator.Play("Attack", 0, 0.5f);
// Crossfade into state over 0.25 seconds
animator.CrossFadeInFixedTime("Run", 0.25f);
// Crossfade using normalized time
animator.CrossFade("Run", 0.1f);
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Attack"))
{
Debug.Log("Currently attacking");
}
if (stateInfo.normalizedTime >= 1.0f && !animator.IsInTransition(0))
{
Debug.Log("Animation finished");
}
// Use hash for performance
int runHash = Animator.StringToHash("Run");
if (animator.HasState(0, runHash))
{
animator.Play(runHash);
}
// Match character's hand to a ledge position during climb animation
animator.MatchTarget(ledgePosition, Quaternion.identity, AvatarTarget.RightHand,
new MatchTargetWeightMask(Vector3.one, 0f), 0.1f, 0.4f);
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class LocomotionController : MonoBehaviour
{
Animator animator;
static readonly int SpeedHash = Animator.StringToHash("Speed");
static readonly int GroundedHash = Animator.StringToHash("Grounded");
static readonly int JumpHash = Animator.StringToHash("Jump");
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
float speed = Input.GetAxis("Vertical");
animator.SetFloat(SpeedHash, Mathf.Abs(speed), 0.1f, Time.deltaTime);
animator.SetBool(GroundedHash, IsGrounded());
if (Input.GetButtonDown("Jump") && IsGrounded())
{
animator.SetTrigger(JumpHash);
}
}
bool IsGrounded()
{
return Physics.Raycast(transform.position + Vector3.up * 0.1f, Vector3.down, 0.2f);
}
}
using UnityEngine;
public class WeaponAnimSwap : MonoBehaviour
{
public AnimatorOverrideController swordOverride;
public AnimatorOverrideController bowOverride;
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
public void EquipSword()
{
animator.runtimeAnimatorController = swordOverride;
}
public void EquipBow()
{
animator.runtimeAnimatorController = bowOverride;
}
}
| Anti-Pattern | Problem | Solution |
|---|---|---|
| String parameters every frame | GC allocation, slow lookup | Cache hashes with Animator.StringToHash() |
| Humanoid rig for non-humanoid objects | 15-20% unnecessary CPU overhead | Use Generic animation type for props, animals, effects |
| Multiple Animators under shared root | Single-thread write limitation per hierarchy blocks parallelism | Give each Animator its own root GameObject |
| StateMachineBehaviour overuse | Creates synchronization points, breaks parallel evaluation | Minimize SMB callbacks; prefer script-side logic |
| RectTransform animation via Animator | Deterministic write issues | Use legacy Animation component for UI animation |
| Complex state machines for single-shot anims | Continuous transition evaluation even at idle | Use Animation component or Playables API instead |
Calling Animator.Update() manually | Bypasses parallel execution | Use PlayableGraph for manual control with parallelism |
| Not setting transition exit time to normalized | Override Controller clips may skip transitions | Always use normalized time for exit times |
Forgetting Rebind() after controller swap | Stale bindings cause incorrect playback | Call Rebind() when changing controllers at runtime |
| Bake Into Pose disabled on idle clips (XZ) | Drift accumulation on idle animations | Enable Bake Into Pose XZ for stationary clips |
applyRootMotion, speed, updateMode (Normal/AnimatePhysics/UnscaledTime), cullingMode, deltaPosition, deltaRotation, velocity, isHuman, layerCount, bodyPosition, gravityWeight
Play(state, layer, time), CrossFade(state, duration), CrossFadeInFixedTime(state, duration)SetFloat/Int/Bool/Trigger(name, value), GetFloat/Int/Bool(name), ResetTrigger(name)GetCurrentAnimatorStateInfo(layer), GetNextAnimatorStateInfo(layer), IsInTransition(layer)SetIKPosition/Rotation(goal, value), SetIKPositionWeight/RotationWeight(goal, weight), SetLookAtPosition(pos)MatchTarget(...), GetBoneTransform(bone), Rebind(), StringToHash(name), SetLayerWeight(layer, weight)