Unityアニメーションシステムとは

Animationシステムは、キャラクターやオブジェクトに動きを付ける機能です。

アニメーションでできること

  • 🏃 キャラクターの歩行、走行、ジャンプ
  • 🎬 カットシーンの作成
  • 🎨 UIのスムーズな動き
  • 🌊 環境オブジェクトの動的な動き
  • 🎮 攻撃、スキルエフェクト

アニメーションの基本構成

3つの主要コンポーネント

  • Animation Clip: アニメーションデータ(動きの記録)
  • Animator Controller: 状態遷移を管理(歩く→走る)
  • Animator: GameObjectに付けるコンポーネント

Animation Clipの作成

新規アニメーションの作成

1. GameObjectを選択
2. Window > Animation > Animation
3. 「Create」ボタンをクリック
4. ファイル名を付けて保存(例: Walk.anim)

キーフレームの記録

1. 録画ボタン(赤丸)をクリック
2. タイムラインの位置を移動
3. Inspectorでプロパティを変更(位置、回転、スケールなど)
4. 自動的にキーフレームが記録される

例: シンプルな回転アニメーション

0:00 → Rotation: (0, 0, 0)
1:00 → Rotation: (0, 180, 0)
2:00 → Rotation: (0, 360, 0)

Animator Controllerの作成

新規Controllerの作成

Project > 右クリック > Create > Animator Controller
名前を付ける(例: PlayerAnimator)

Animatorウィンドウ

Window > Animation > Animator

State(状態)の追加

1. Animatorウィンドウで右クリック
2. Create State > Empty
3. MotionにAnimation Clipを設定

State Machine - 状態遷移

基本的な状態

  • Entry: 開始地点
  • Any State: どの状態からでも遷移可能
  • Exit: 終了

Transitionの作成

1. 状態を右クリック > Make Transition
2. 遷移先の状態をクリック
3. 矢印(Transition)が作成される

Transitionの設定

Has Exit Time: チェックあり = 時間経過で遷移
              チェックなし = 条件のみで遷移

Exit Time: 遷移開始のタイミング(0-1)
Transition Duration: 遷移にかかる時間
Transition Offset: 遷移先の開始位置

ParametersとConditions

Parameterの種類

  • Float: 小数(速度など)
  • Int: 整数(武器IDなど)
  • Bool: 真偽値(歩いているか)
  • Trigger: 一時的なフラグ(ジャンプ)

Parameterの追加

Animator ウィンドウ > Parameters タブ > +

例:
Speed (Float): 移動速度
IsGrounded (Bool): 地面にいるか
Jump (Trigger): ジャンプボタンが押された

Conditionの設定

Transitionを選択 > Inspector > Conditions

例:
Speed > 0.1 の時、Idle → Walk に遷移
Jump がトリガーされた時、Walk → Jump に遷移

スクリプトからの制御

基本的な操作

public class PlayerAnimation : MonoBehaviour
{
    private Animator animator;

    void Start()
    {
        animator = GetComponent();
    }

    void Update()
    {
        // Float パラメータの設定
        float speed = Input.GetAxis("Vertical");
        animator.SetFloat("Speed", Mathf.Abs(speed));

        // Bool パラメータの設定
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        animator.SetBool("IsRunning", isRunning);

        // Trigger パラメータの設定
        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("Jump");
        }
    }
}

パラメータの取得

float speed = animator.GetFloat("Speed");
bool isGrounded = animator.GetBool("IsGrounded");
int weaponID = animator.GetInteger("WeaponID");

現在の状態の取得

AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);

// 特定の状態にいるか
if (stateInfo.IsName("Jump"))
{
    Debug.Log("ジャンプ中");
}

// 正規化された時間(0-1)
float normalizedTime = stateInfo.normalizedTime;

Animation Events

イベントの追加

1. Animation ウィンドウでAnimation Clipを開く
2. イベントを追加したいフレームをクリック
3. イベントボタン(旗マーク)をクリック
4. 関数名を入力

イベント関数の作成

public class CharacterAnimation : MonoBehaviour
{
    // Animation Eventから呼ばれる
    void PlayFootstepSound()
    {
        // 足音を再生
        AudioSource.PlayClipAtPoint(footstepClip, transform.position);
    }

    void AttackHit()
    {
        // 攻撃の当たり判定
        Collider[] hits = Physics.OverlapSphere(attackPoint.position, attackRadius);
        foreach (Collider hit in hits)
        {
            if (hit.CompareTag("Enemy"))
            {
                hit.GetComponent()?.TakeDamage(damage);
            }
        }
    }
}

Blend Tree - ブレンドツリー

Blend Treeの作成

Animator ウィンドウ > 右クリック > Create State > From New Blend Tree

1D Blend(1次元ブレンド)

1つのパラメータで複数のアニメーションをブレンドします。

Parameter: Speed

0.0 → Idle
0.5 → Walk
1.0 → Run

// Speedの値に応じて自動的にブレンド

2D Blend(2次元ブレンド)

2つのパラメータでブレンドします(前後左右の移動など)。

Parameters: Horizontal, Vertical

(0, 1)  → Walk Forward
(1, 0)  → Walk Right
(-1, 0) → Walk Left
(0, -1) → Walk Backward

スクリプトでBlend Tree制御

float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

animator.SetFloat("Horizontal", horizontal);
animator.SetFloat("Vertical", vertical);

LayerとAvatar Mask

Layerの追加

Animator ウィンドウ > Layers タブ > +

Weight: レイヤーの影響度(0-1)
Blending: Override(上書き)/ Additive(加算)

用途例

Base Layer: 全身のアニメーション(歩く、走る)
Upper Body Layer: 上半身のみ(武器を構える、攻撃)

// 歩きながら上半身で攻撃できる

Avatar Maskの作成

Project > 右クリック > Create > Avatar Mask

// 上半身のみを有効化
Head: ✓
Left Arm: ✓
Right Arm: ✓
Spine: ✓
Left Leg: ✗
Right Leg: ✗

Root Motionとは

Root Motionの有効化

Animator > Apply Root Motion: チェック

アニメーションの動きがそのままGameObjectに適用されます。

用途

  • ✅ キャラクターの移動がアニメーションに含まれている場合
  • ✅ リアルな攻撃の踏み込み
  • ⚠️ スクリプトで移動を制御する場合はオフ

実用的なアニメーション例

例1: シンプルなキャラクター制御

public class CharacterController : MonoBehaviour
{
    private Animator animator;
    private Rigidbody rb;
    public float speed = 5.0f;

    void Start()
    {
        animator = GetComponent();
        rb = GetComponent();
    }

    void Update()
    {
        float moveZ = Input.GetAxis("Vertical");
        float moveX = Input.GetAxis("Horizontal");

        Vector3 movement = new Vector3(moveX, 0, moveZ);
        float moveMagnitude = movement.magnitude;

        // アニメーションパラメータ設定
        animator.SetFloat("Speed", moveMagnitude);

        // 移動
        rb.MovePosition(transform.position + movement * speed * Time.deltaTime);

        // 向きを変える
        if (movement != Vector3.zero)
        {
            transform.forward = movement;
        }

        // ジャンプ
        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("Jump");
        }
    }
}

例2: 攻撃アニメーション

public class AttackController : MonoBehaviour
{
    private Animator animator;
    private bool isAttacking = false;

    void Start()
    {
        animator = GetComponent();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0) && !isAttacking)
        {
            Attack();
        }
    }

    void Attack()
    {
        isAttacking = true;
        animator.SetTrigger("Attack");
    }

    // Animation Eventから呼ばれる
    void AttackEnd()
    {
        isAttacking = false;
    }
}

例3: ダメージアニメーション

public class DamageAnimation : MonoBehaviour
{
    private Animator animator;

    public void TakeDamage(int damage)
    {
        animator.SetTrigger("Hit");
        
        // ダメージ量に応じてアニメーション変更
        if (damage > 50)
        {
            animator.SetBool("IsHeavyHit", true);
        }
    }
}

パフォーマンス最適化

1. Culling Mode

Animator > Culling Mode

Always Animate: 常にアニメーション計算(重い)
Cull Update Transforms: カメラ外では骨の更新のみ停止
Cull Completely: カメラ外では完全停止(推奨)

2. 不要なParameterを削除

  • 使っていないParameterは削除
  • Transitionの条件を最小限に

3. Animation Compressionの設定

Animation Clip > Inspector > Compression

Off: 圧縮なし(高品質、大容量)
Keyframe Reduction: キーフレーム削減
Optimal: 自動最適化(推奨)

トラブルシューティング

アニメーションが再生されない

  • Animator ControllerがAnimatorに設定されているか
  • Animation ClipがStateに設定されているか
  • Transitionの条件が正しいか

アニメーションがカクカク

  • Animation ClipのSample Rateを上げる
  • Interpolationを確認

Transitionが遅い

  • Transition Durationを短く
  • Exit Timeを調整

まとめ

Unityアニメーションシステムで滑らかで表現豊かな動きを実装できます。

重要ポイント

  • Animation Clipでアニメーションデータ作成
  • Animator ControllerでState Machine構築
  • ParameterとConditionで遷移制御
  • Blend Treeで複数アニメーションをブレンド
  • Animation Eventで正確なタイミング制御

次のステップ

  • Humanoid RigとIKシステム
  • Timelineでシネマティックシーン
  • Animation Riggingで手続き的アニメーション
  • Cinemachineとの連携