Unity Animation

作者 gamedev-skillsd4b0e35550c5無授權條款1.3K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫11 天前更新

Drive Unity 6.3 LTS character animation with Animator Controllers: states, transitions, parameters, blend trees, animation layers, and humanoid Avatar IK. Use when wiring an Animator, setting parameters from script (SetFloat/SetBool/SetTrigger), building blend trees, or when the user mentions Animator, Mecanim, state machine, blend tree, or .controller.

AI 產生的概覽

說明 Unity 6.3 LTS 動畫控制器:參數、轉換、混合樹、圖層與人形 IK。

功能
此技能提供設定 Unity Animator/Mecanim 系統的說明:加入 Animator、定義參數、建立含轉換的狀態機、透過 C# 指令碼驅動參數、以混合樹混合移動動畫、用 Avatar Mask 分層動畫,以及套用人形 IK。內容包含設定浮點、布林與觸發器參數、交叉淡化狀態,以及等待狀態播放完畢的程式範例,並附上常見問題清單。隨附的參考文件更深入介紹混合樹、動畫圖層與人形 IK。
適用情境
適用於將動畫剪輯接上狀態機、透過參數從指令碼驅動動畫、混合待機/走路/跑步的移動動畫、在移動之上疊加上半身動作,或為人形骨架加入腳部與手部 IK 的情境。適合含有 .controller 與 .anim 資源,或帶有 Avatar 的骨架模型的專案。
執行需求
需要 Unity 6.3 LTS(6000.3)以及 Animator/Mecanim 系統和 C# 指令碼;此技能不隨附指令碼,只有說明文件與一份參考文件。

Unity Animation (Animator / Mecanim)

Control animation state with Unity 6.3 LTS's Animator and Animator Controllers: parameters, transitions, blend trees, layers, and humanoid IK. Targets Unity 6.3 LTS (6000.3).

When to use

  • Use when connecting animation clips into a state machine, driving them from script via parameters, blending locomotion (idle→walk→run), layering an upper-body action over movement, or adding foot/hand IK on a humanoid rig.
  • Use when the project has *.controller (Animator Controller) and *.anim assets, or a rigged model with an Avatar.

When not to use: simple non-skeletal value tweens (UI fades, position lerps) are better done with a tween/coroutine — see unity-csharp-scripting. Timeline cutscenes are a separate tool. 2D sprite frame animation also uses the Animator but with sprite keyframes.

Core workflow

  1. Add an Animator to the model and assign an Animator Controller; for a humanoid model, set its rig to Humanoid so it has an Avatar (enables retargeting and IK).
  2. Define parameters on the controller — Float (Speed), Bool (IsGrounded), Int, Trigger (Jump) — and states with transitions whose conditions read those parameters.
  3. Set parameters from script, never poke states directly: SetFloat, SetBool, SetInteger, SetTrigger. The state machine resolves transitions for you.
  4. Blend continuous motion with a Blend Tree (one Float like Speed drives idle↔walk↔run) instead of many discrete states + transitions.
  5. Layer additive/override motion (e.g. an upper-body "aim" layer with an Avatar Mask) and control its layerWeight.
  6. Verify in the Animator window during Play mode — the live state highlights and parameter values update, so you can see exactly which transition fired (or didn't).

Patterns

1. Drive locomotion + a one-shot action from script

csharp
using UnityEngine;
[RequireComponent(typeof(Animator))]public class CharacterAnim : MonoBehaviour{    private Animator _anim;    // Cache parameter hashes — faster and typo-proof vs string lookups every frame.    private static readonly int Speed     = Animator.StringToHash("Speed");    private static readonly int IsGrounded= Animator.StringToHash("IsGrounded");    private static readonly int Jump      = Animator.StringToHash("Jump");
    private void Awake() => _anim = GetComponent<Animator>();
    public void Tick(float planarSpeed, bool grounded)    {        _anim.SetFloat(Speed, planarSpeed);     // drives a 1D blend tree (idle/walk/run)        _anim.SetBool(IsGrounded, grounded);    // gates a falling/landing transition    }
    public void DoJump() => _anim.SetTrigger(Jump);  // fire-and-forget; auto-resets after use}

2. Smooth a noisy input into a blend parameter

csharp
// dampTime smooths Speed so the blend tree doesn't snap; great for analog sticks._anim.SetFloat(Speed, targetSpeed, 0.1f /* dampTime */, Time.deltaTime);

3. Play / cross-fade a state directly (bypassing parameter conditions)

csharp
// Useful for hit reactions where you want an immediate, explicit transition._anim.CrossFade("Hit", 0.1f);                    // blend over 0.1s normalized// Or jump instantly:  _anim.Play("Hit");

4. Wait until the current state finishes

csharp
private System.Collections.IEnumerator AfterAttack(){    var info = _anim.GetCurrentAnimatorStateInfo(0);   // layer 0    yield return new WaitForSeconds(info.length);      // approximate clip length    // ...follow-up logic}

Pitfalls

  • SetTrigger missed or "sticks" — triggers are consumed by the next satisfied transition and auto-reset; if no transition consumes it, it can fire later unexpectedly. Use ResetTrigger to clear, or prefer a Bool when the condition is a sustained state.
  • String parameter typos fail silently — a misspelled name just does nothing. Use Animator.StringToHash and cache the int hashes.
  • Transition feels laggy — Has Exit Time makes the transition wait for the clip to reach a normalized time. Uncheck it for responsive, condition-driven transitions (jump, hit).
  • Character slides or won't move — Apply Root Motion is on but your code also moves the transform (or vice versa). Decide: root motion or scripted movement, not both.
  • Upper-body layer overrides the whole body — set the layer's Blend mode (Override vs Additive), assign an Avatar Mask, and tune layerWeight (0–1).
  • IK does nothing — IK only applies inside OnAnimatorIK, requires "IK Pass" enabled on the layer, and needs a Humanoid Avatar.

References

  • For blend trees (1D vs 2D Freeform/Directional), animation layers + Avatar Masks, and humanoid IK (OnAnimatorIK, SetIKPositionWeight, SetIKPosition, look-at), read references/blend-trees-and-ik.md.
  • Primary docs: Unity Manual "Animation" section and ScriptReference/Animator.

Related skills

  • unity-csharp-scripting — the MonoBehaviour and coroutine timing used above.
  • unity-physics — moving the body that the animation visualises.
  • game-ai — deciding when to play which animation state.

來源與署名

來源:gamedev-skills/awesome-gamedev-agent-skills位於skills/unity/unity-animation提交d4b0e35

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架