アニメーションを適用する基本的な流れ
3D モデルのインポート
手順1
Assets フォルダ内で右クリックして「Import New Asset…」を選択する等してアニメーションさせたい 3D モデルをインポートする。

手順2
インポートした 3D モデルを選択してインスペクターから「Extract Textures…」や「Extract Materials…」を押し、必要に応じてテクスチャやマテリアルを生成する。

手順3
インポートした 3D モデルを選択してインスペクターから「Rig > Animation Type」で(人型の場合は)「Humanoid」を選択する。

手順4
右下の「Apply」を押してリグを生成する。
「リグ」とは、3Dキャラクターに動きを付ける際の仕組みやコントローラーのことを指します。
3Dアニメーションのリグについて

Animation Clip のインポートと適用
手順5
FBX 等の形式のアニメーションをインポートし、Assets フォルダ内でモデルのトグルを開いて Animation Clip ファイルを選択している状態で複製(Ctrl + D)する等して Animation Clip ファイルのみを抽出する。

手順6
Assets フォルダ内で右クリックして「Create > Animator Controller」で Animator Controller を作成する。

手順7
作成した Animator Controller をダブルクリックしてグラフを開く。

手順8
グラフ内の何も無い場所で右クリックして「Create State > Empty」で空のステートを作成する。

手順9
作成したステートを選択し、ステート名を「Idle」等に変更して Motion に待機モーション等の Animation Clip をアサインする。

手順10
同様にして歩行モーション等のステートをグラフ内に作成し、初期モーション(オレンジ色のステート)上で右クリックして「Make Transition」を選択する。

手順11
作成した Transition を歩行モーション等のグレーのステートに接続し、同様にグレーのステートからオレンジ色のステートにも Transition を作成して接続する。

手順12
左上の「Paremeters」を選択して「+▼」からパラメーターの型を選択して適切なパラメーター名を設定する。
(今回は Bool 型の「Run」パラメーター)

手順13
オレンジ色のステートとグレーのステート間の2本の Transition を1本ずつ選択してインスペクターの「Conditions」の右下の「+」を押し、先ほど作成したパラメーターを追加して、この Transition が有効となる時のパラメーターの状態を設定する。
(下の画像の例では Bool 型の Run パラメーターの値が true の時に待機モーションから走行モーションに遷移する)

アニメーション遷移の制御
手順14
冒頭でインポートした 3D モデルをシーン上に配置し、インスペクターからそのゲームオブジェクトに Animator コンポーネントをアタッチする。

手順15
Animator コンポーネントの「Controller」には Animator Controller を、「Avatar」には 3D モデルの Avatar をアサインする。

手順16
MonoBehaviour クラスを継承したクラスを作成して Animator のパラメーターの値を制御する処理を記述して 3D モデルのゲームオブジェクトにアタッチする。
(以下のコードの例ではスペースキーが押される度に「Run」パラメーターの値を切り替えている)
using UnityEngine;
public class Sample : MonoBehaviour
{
private Animator animator;
private void Start()
{
animator = GetComponent<Animator>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetBool("Run", !animator.GetBool("Run"));
}
}
}
手順17
ゲームを実行して動作を確認する。
(下の例ではスペースキーを押す度に待機モーションと走行モーションが切り替わる)