Unity3D アニメーションコントローラーによるオブジェクト操作
アニメーションコントローラーを使ってオブジェクトの基本的な操作
事前準備として予め下記を実施しておく
・Cubeオブジェクトの作成
・Cubeオブジェクトをスクリプトにアタッチ
・Animations、Scriptsフォルダの作成
■①アニメーションクリップを作成
Cubeオブジェクトを選択し、Animationウィンウの「Create」を押下
Animationsフォルダに任意の名前を付けて保存 ※今回は「anim1」という名前にした
「Cube」というアニメーターも同時に作成される
■②アニメーションクリップを編集する
「Add Property」⇒「Transform」⇒「Position」の右側の「+」を押下
0:30(0.5秒)のラインでY軸を「5」に変更
■③アニメーターを確認する
Animationsフォルダの「Cube」をダブルクリックして「Animator」ウィンドウを表示させる
■④アニメーターを編集する
最初はアニメーションが停止させてエンターキーを押下するとCubeが動くように設定する
「Animator」ウィンドウで「右クリック」⇒「Create State」⇒「Empty」を押下して新しいステートを作成する
「Entry」ステートを「右クリック」⇒「Set StateMachine Default State」を押下して「Stay」ステートを選択
矢印が「Entry」から「Stay」に繋がる
「Stay」ステートを「右クリック」⇒「Make Transition」を押下して「anim1」ステートを選択
矢印が「Stay」から「anim1」に繋がる
「anim1」ステートを「右クリック」⇒「Make Transition」を押下して「Stay」ステートを選択
矢印が「anim1」から「Stay」に繋がる
「Parameters」⇒「Bool」を押下して任意の名前を付ける ※今回は「flag」とする
「Stay」ステートから「anim1」にステートに向かう矢印をクリック
インスペクターの「Conditions」の「+」を押下して下記を設定
「flag true」
同様に「anim1」ステートから「Stay」ステートに向かう矢印をクリック
インスペクターの「Conditions」の「+」を押下して下記を設定
「flag false」
これでパラメーターに設定した「flag」がtrueなら「anim1」のクリップが動作する
「flag」がfalseなら「Stay」のクリップ(空のクリップ)が動作することになり、アニメーションは停止する
■⑤スクリプトを編集する
【スクリプトの概要】
1.アニメーターコンポーネントを取得
2.アニメーターにセットされている判定フラグ(flag)の状態を取得
3.エンターキーを押下したら判定フラグ(flag)を反転させてアニメーターにセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeManager : MonoBehaviour
{
//アニメーター
Animator anim;
//判定フラグ ※初期値:false
bool flag;
// Start is called before the first frame update
void Start()
{
//アニメーターコンポーネントを取得
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//アニメーターにセットされている判定フラグの状態を取得
flag = anim.GetBool("flag");
//エンターキー押下時
if (Input.GetKeyDown(KeyCode.Return)) {
//判定フラグを反転 ※true⇔false
flag = !flag;
//アニメーターにフラグをセット ※flagがtrueの時にCubeが動く
anim.SetBool("flag", flag);
}
}
}
エンターキーを押すとCubeオブジェクトが上下に動く
■応用編
上キー押下時:上に移動
右キー押下時:右に移動
エンターキー押下時:上右移動の切替え
・アニメーター
・ステートメントのパラメーター
Stay⇒Anim1:「bool run1 = true」
Anim1⇒Stay:「bool run1 = false」
Stay⇒Anim2:「bool run2 = true」
Anim2⇒Stay:「bool run2 = false」
Anim1⇒Anim2:「bool toRun2 = true」
Anim2⇒Anim1:「bool toRun1 = true」
・スクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeManager : MonoBehaviour
{
Animator anim;
bool run1;
bool run2;
bool toRun1;
bool toRun2;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//アニメーターのパラメーターを取得
run1 = anim.GetBool("run1");
run2 = anim.GetBool("run2");
toRun1 = anim.GetBool("toRun1");
toRun2 = anim.GetBool("toRun2");
//上キー押下時(上移動)
if (Input.GetKeyDown(KeyCode.UpArrow)) {
//「run1」(上への移動)のみ「true」
run1 = true;
run2 = false;
toRun1 = false;
toRun2 = false;
//各パラメーターをセット
anim.SetBool("run1", run1);
anim.SetBool("run2", run2);
anim.SetBool("toRun1", toRun1);
anim.SetBool("toRun2", toRun2);
}
//右キー押下時(右移動)
if (Input.GetKeyDown(KeyCode.RightArrow)) {
//「run2」(右への移動)のみ「true」
run1 = false;
run2 = true;
toRun1 = false;
toRun2 = false;
//各パラメーターをセット
anim.SetBool("run1", run1);
anim.SetBool("run2", run2);
anim.SetBool("toRun1", toRun1);
anim.SetBool("toRun2", toRun2);
}
//エンターキー押下時(上移動と右移動を切替)
if (Input.GetKeyDown(KeyCode.Return)) {
//「上移動」と「右移動」のパラメーターを反転
run1 = !run1;
run2 = !run2;
toRun1 = false;
toRun2 = false;
//各パラメーターをセット
anim.SetBool("run1", run1);
anim.SetBool("run2", run2);
anim.SetBool("toRun1", toRun1);
anim.SetBool("toRun2", toRun2);
}
}
}