Chuyển đến nội dung
Skillpedia
Lập trình chung6.6k · #13844

unity-animation

Skill này làm gì

Skill này giải thích cách điều khiển hoạt ảnh nhân vật trong Unity bằng Animator Controller. Nó dành cho nhà phát triển làm hoạt ảnh cho nhân vật đã rig trong Unity 6.

Khi nào nên dùng

  • ✓Khi bạn cần nối hoạt ảnh đi bộ, chạy và nhảy vào một máy trạng thái trong Unity.
  • ✓Khi bạn muốn đổi hoạt ảnh từ script bằng tham số.
  • ✓Khi bạn muốn một hành động phần thân trên chạy đè lên chuyển động, hoặc chân và tay thích ứng với mặt đất.

Liên kết skill

https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unity/unity-animation/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unity/unity-animation/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm nhân vật Unity của tôi chuyển từ đứng yên sang đi bộ rồi chạy dựa trên tốc độ di chuyển của nó.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Skill giải thích cách xây trạng thái và chuyển tiếp với tham số, trộn hoạt ảnh bằng blend tree, xếp lớp hành động, và đặt giá trị từ script để máy trạng thái chọn đúng hoạt ảnh.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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

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

// 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)

// Useful for hit reactions where you want an immediate, explicit transition.
_anim.CrossFade("Hit", 0.1f);                    // 0.1 = 10% of the state's length, not seconds
_anim.CrossFadeInFixedTime("Hit", 0.15f);        // duration in seconds
// Or jump instantly:  _anim.Play("Hit");

4. Wait until the current state finishes

private System.Collections.IEnumerator AfterAttack()
{
    yield return null;                                 // let the Animator start the transition
    // During a transition the "current" state is the one being left; read the next one.
    var info = _anim.IsInTransition(0)
        ? _anim.GetNextAnimatorStateInfo(0)
        : _anim.GetCurrentAnimatorStateInfo(0);        // layer 0
    yield return new WaitForSeconds(info.length);      // seconds; approximate, varies in blend trees
    // ...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. Un
Câu hỏi thường gặp

Tôi có nên đổi trạng thái hoạt ảnh trực tiếp từ code không?

Không. Skill nói hãy đặt tham số từ code và để máy trạng thái quyết định các chuyển tiếp.

Avatar trong Unity là gì?

Avatar là thiết lập cho phép một mô hình humanoid tái sử dụng hoạt ảnh và dùng động học ngược IK.

Skill này có bao quát hiệu ứng mờ dần UI đơn giản không?

Không. Những thứ đó làm tốt hơn bằng tween hoặc coroutine, do một skill scripting bao quát.

Chi tiết

Mô tả gốc của tác giả

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.

repo: gamedev-skills/awesome-gamedev-agent-skills

Độ phổ biến

Sao GitHub
6.6k
Vị trí xếp hạng
#13844
Giấy phép
Apache-2.0
Cập nhật
28/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự