![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/10/PC-annoy-woman.png?w=1256&ssl=1)
複数のアニメーションをつなげて再生するにはどうしたら良いですか?
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/10/Smile-man.png?w=1256&ssl=1)
Input Systemを使うと直感的にできるよ!
Input Systemは、ゲームパッド・キーボード・マウス・センサー等の各種入力デバイスからの入力を汎用的・統合的に取り扱うために導入されたUnityの新しい入力システムです。
Unity for Pro
今回は、キーボード操作で複数のアニメーションを連続再生する方法について解説していきます。
▼完成イメージ▼
では、早速始めていきましょう!
プロジェクトは前回の続きから始めていきますので、手順に不安のある方は以下の記事内容を実施してから、戻ってきてください。
ここでは、キャラクターとアニメーションは、mixamoを使用します。
アニメーションのダウンロード
mixamoからお好きなアニメーションをダウンロードします。ここでは、「歩く」→「走る」→「ブレイクダンス」という流れにするために、「歩く」と「走る」を備えたアニメーションをダウンロードします。(ブレイクダンスは前回の記事でダウンロード済み)
mixamoを開き、[Animations]>[Walk(任意)]>[DOWNLOAD]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/1-mixamo-motion1.jpg?resize=800%2C416&ssl=1)
必要事項を入力し、[DOWNLOAD]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/10-animation-FBX-download.png?resize=800%2C316&ssl=1)
もう一つ、同様の手順でアニメーションをダウンロードします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/2-mixamo-motion2.jpg?resize=800%2C389&ssl=1)
アニメーションの設定
Unityにダウンロードしたアニメーションを取り込みます。
[Assets]>[Import New Asset…]をクリックして、先ほどダウンロードをした「〜.fbx」ファイルを選択します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/3-New-Assets-Import.png?resize=728%2C476&ssl=1)
インポートが完了したら、アニメーションの設定をします。
Projectウインドウのアニメーションを選択し、Inspectorウインドウの[Rig]>[Animation Type]から、プルダウンメニューで[Humanoid]を選択して、[Apply]で確定します。
同様の操作を全てのアニメーションで実施します。(解説では3つ)
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/4b129097844584e34b58af53cf6998d9.png?w=1256&ssl=1)
次に、アニメーションの設定をしていきます。Projectウインドウのアニメーションを選択し、Inspectorウインドウの[Animation]>[Loop Time]にチェックを入れて、[Apply]で確定します。
同様の操作を全てのアニメーションで実施します。(解説では3つ)
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/5-Loop-time.png?resize=922%2C1024&ssl=1)
Input Systemのインストール
[Window]>[Package Manager]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/6-PackageManeger-input-syestem1.png?resize=800%2C477&ssl=1)
[Package Manager]が開きますので、[Packages:Unity Registry]を選択して、検索窓で”inout”と入力します。[Input System]が表示されたら、選択して、[Install]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/7-PackageManeger-input-syestem2.png?resize=800%2C295&ssl=1)
Warningが表示されますので、内容を確認して、[Yes]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/8-PackageManeger-input-syestem3.png?resize=832%2C464&ssl=1)
変更を保存する場合は、[Save]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/9-PackageManeger-input-syestem4.png?resize=888%2C328&ssl=1)
Player Inputの設定
Hierarchyウインドウのキャラクター[Worker]を選択し、Inspectorウインドウの[Add Component]>[Player Input]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/10-Player-Input-add.png?resize=800%2C515&ssl=1)
Hierarchyウインドウのキャラクター[Worker]を選択し、Inspectorウインドウの[Player Input]>[Create Actions…]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/11-Player-Input-add1.png?resize=800%2C680&ssl=1)
[Assets]の配下を保存先に指定して、名前を入力して[Save]をクリックします。ここでは、[WorkerActions]をという名前を指定しました。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/12-motion-action-add.png?resize=800%2C581&ssl=1)
Projectウインドウに作成したPlayer Input[WorkerActions]が配置されますので、Inspectorウインドウの[Player Input]>[Actions]にドラッグして離します。
キーボード操作でキャラクターを操作するため、Default Scheme:[Keyboard&Mouse]に変更します。Behavior:[Invoke Unity Events]に変更します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/13-Player-action-set2.png?resize=800%2C617&ssl=1)
ProjectウインドウのPlayer Input[WorkerActions]をクリックすると、Input Actionウインドウが開きます。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/14-Input-action-window1.png?resize=800%2C617&ssl=1)
[Action]>[Move]>[WASD]を開くと、それぞれの操作と紐づいたキーが表示れされます。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/15-Input-action-window2.png?resize=800%2C498&ssl=1)
Hierarchyウインドウのキャラクター[Worker]を選択し、Inspectorウインドウの[Events]>[New script]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/16-NewScript-add.png?resize=800%2C692&ssl=1)
任意のファイル名を設定して、[Create and Add]で確定します。
ここでは、WorkerController]としました。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/16-NewScript-add-name.png?resize=468%2C620&ssl=1)
追加したScriptを開いてコードを書いていきます。
スクリプトの追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WorkerController : MonoBehaviour
{
Vector2 moveDirection;
public float moveSpeed = 5;
public float maxForwardSpeed = 10;
float desiredForwardSpeed;
float forwardSpeed;
const float stageAceel = 5;
const float stageDecel = 10;
Animator anima;
bool IsMoveInput
{
get { return !Mathf.Approximately(moveDirection.sqrMagnitude, 0f); }
}
public void OnMove(InputAction.CallbackContext context)
{
moveDirection = context.ReadValue<Vector2>();
}
private void Move(Vector2 direction)
{
if (direction.sqrMagnitude > 1f)
direction.Normalize();
desiredForwardSpeed = direction.magnitude * maxForwardSpeed;
float acceleration = IsMoveInput ? stageAceel : stageDecel;
forwardSpeed = Mathf.MoveTowards(forwardSpeed, desiredForwardSpeed, acceleration * Time.deltaTime);
anima.SetFloat("ForwardSpeed", forwardSpeed);
}
void Start()
{
anima = this.GetComponent<Animator>();
}
void Update()
{
Move(moveDirection);
}
}
参考に単一アニメーションで動作するスクリプトも載せていきます。(今回は使用しません)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class WorkerController : MonoBehaviour
{
Vector2 moveDirection;
public float moveSpeed = 5;
public void OnMove(InputAction.CallbackContext context)
{
moveDirection = context.ReadValue<Vector2>();
}
private void Move(Vector2 direction)
{
transform.Translate
(direction.x * moveSpeed * Time.deltaTime, 0,
direction.y * moveSpeed * Time.deltaTime);
}
void Start()
{
}
void Update()
{
Move(moveDirection);
}
}
イベントの追加
Hierarchyウインドウのキャラクター[Worker]を選択し、Inspectorウインドウの[Events]>[Player]>[Move(CallbackContext)]のオブジェクトの枠にドラッグして離します。
そして、[No Function]>[WorkerController]>[OnMove]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/17-move-EVENT-add.jpg?resize=800%2C591&ssl=1)
アニメータの設定
前回の続きからやっている場合、アニメータにアニメーションが追加されていますので、削除しておきます。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/18-auto-motion-delete.png?resize=800%2C467&ssl=1)
マウスの左クリックで[Create Sub-State Machine]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/19-Create-sub-stateMachine.png?resize=800%2C615&ssl=1)
Sub-State Machineの名前を任意に設定します。
ここでは、[Movement]とします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/20-Create-sub-stateMachine-name.png?resize=556%2C152&ssl=1)
[Movement]が追加されますので、クリックして開きます。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/21-Create-sub-stateMachine-add.png?resize=800%2C488&ssl=1)
マウスの左クリックから[Create State]>[From New Blend Tree]をクリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/22-Create-state-from-new-blend-tree.png?resize=800%2C439&ssl=1)
From New Blend Treeの名前を任意に設定します。
ここでは、[Move]とします。
[Move]が追加されますので、クリックします。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/23-Create-state-from-new-blend-tree-add.png?resize=800%2C530&ssl=1)
先ほど、Scriptで指定した”ForwardSpeed”に変更します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/24-program-conect-1.png?resize=800%2C422&ssl=1)
Blend Tree上で、左クリックから、[Add Motion]をします。今回3つのアニメーションを繋げるため、3回同じ作業を繰り返します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/fa53337f2949ea40328b9ae962e2fedc.png?resize=550%2C230&ssl=1)
Inspectorウインドウにアニメーションが3つ追加されます。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/26-Add-motion-result-1.png?resize=550%2C688&ssl=1)
「mixamo」では、アニメーション名が「mixamo.com」になっていることがありますので、わかりやすい名前に変更します。Projectウインドウのアニメーションを選択し、Inspectorウインドウの[Edit]を選択します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/27-animation-name-edit1.png?resize=1016%2C970&ssl=1)
Inspectorウインドウの[Animation]から、名前を変更して、[Apply]で確定します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/28-animation-name-edit2.png?resize=1020%2C1012&ssl=1)
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/29-threthold-change.jpg?resize=800%2C219&ssl=1)
[Blend Tree]をクリックして、Inspectorウインドウでプルダウンメニューから[Motion]を選択します。Parameterの時間も短いようであれば適当に大きくしておきます。解説では25にしています。
動作確認
[▶︎]ゲームプレイをクリックします。
キーボードの[W]をクリックすることで、アニメーションが遷移します。
![](https://i0.wp.com/takugymnastics.com/wp-content/uploads/2021/11/15-Input-action-window2.png?resize=800%2C498&ssl=1)
[歩く]→[走る]→[ブレイクダンス]の3つの動作を連続でつなげることができました。
以上で完成です。お疲れ様でした。