【Unity道場 2017】PlayMakerによる初めてのUnityプログラミング
- 7. 7
PlayMakerとは
• 特徴
• 状態遷移ベースのフロー設計ができる
• ロジックをノンコーディングで記述できる
• 実行中プログラムの状態を視覚的に把握でき、デバッグ
効率が高い
• 既存コードや他アセットとの連携が容易
• ユーザー独自の「アクション」の開発が可能
• 全てをPlayMakerだけで作成することができる。
• C#スクリプトと連携させてプロジェクトの一部にだけ
PlayMakerを適用することもできる
- 11. 11
FSMとは
• FSM(Finite State Machine:有限状態機械)と呼
ばれるUMLの状態遷移図に似た形式で記述
• 主な構成要素
• ステート:オブジェクトの状態
• イベント:状態遷移を起こすトリガーとなるもの
• アクション:ある状態におけるオブジェクトの振る
舞い
• 変数:FSMが扱うオブジェクトや数値などの値を
格納する
※有限状態機械(英: finite state machine, FSM)は別の呼び方で有限オートマトン(英: finite automaton, FA)もある
- 12. 12
FSMとは
• 具体例:プレイヤ・キャラクタ
• ステート
• 「待機」、「歩き」、「走り」、「ジャンプ中」など
• イベント
• 「待機」から「歩き」、「歩き」から「走り」などのへのス
テート変化を引き起こすもの(一般的にはユーザからの入力
操作をトリガとする)
• アクション
• 「待機」時の具体的な挙動、「歩き」時の具体的な挙動など
各ステートにおける具体的な挙動
• 変数
• プレイヤの移動方向(Vector3型)と速度(float型)など
- 35. 35
ステージの作成
• ステージはPlaneをベースにします。
• Planeを作成してPosition (0, 0, 0)、Scale(2,1,2)と
して名前を「Ground」に変更
• 詳細
• [GameObject]-[3D Object]-[Plane]でPlaneを作成
• InspectorのTransformのPositionを(0, 0, 0)に設定
• InspectorのTransformのScaleを(2, 1, 2)に設定
• HierarchyのPlaneをクリックして少し待つと編集可能に
なるので、「Ground」に変更(MacユーザはRETURNキ
ーで、WindowsユーザはF2キーでも編集可能)
- 37. 37
プレイヤーの作成
• プレイヤーは球(Sphere)です。
• Sphereを作成してPosition (0, 0.5, 0) として名前
を「Player」に変更
• 詳細
• [GameObject]-[3D Object]-[Sphere]でSphereを作成
• InspectorのTransformのPositionを(0, 0.5, 0)に設定
• HierarchyのSphereをクリックして少し待つと編集可能
になるので、「Player」に変更(MacユーザはRETURN
キーで、WindowsユーザはF2キーでも編集可能)
- 58. 58
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
- 60. 60
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
float型
インスペクター表示(public)
Rigidbody型
インスペクター非表示(private)
- 61. 61
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
float型変数が2つ
Vector3型変数
- 67. 67
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
アクションとして記述する
- 82. 82
キー入力
• Virtual Inputの初期設定
• 今回はHorizontal(水平)と
Vertical(垂直)を使用します。
• 値は -1から1までの範囲
• 二箇所に同じ記述がありますが、
上はキーボード(矢印キーと
WASDキー)で、下はコントロ
ーラの十字キーが設定されてい
ます。
- 83. 83
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
InputのGetAxis()を使用してキーの入力状態を取得。
そこで得られた値をそれぞれの変数に代入している。
- 92. 92
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
InputのGetAxis()で得られた値をX方向(左右)とZ方向
(前後)に設定してmovementを求める
Movementにspeedを掛けた値だけ力を加える
- 98. 98
移動処理
• Set Vector3 XYZを次のように
設定してください。
• 各変数の右にある二重線をク
リックすると、変数による指
定と値による指定を切り替え
ることができます。
• Yは値による指定に切り替え
て0を入力しています。
• 毎フレーム計算をするために
Every Frameをチェックしま
す。
- 101. 101
スクリプト(イメージ)
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
InputのGetAxis()で得られた値をX方向(左右)とZ方向
(前後)の力にするためにVector3を求めている
Movementにspeedを掛けた値だけ力を加える
- 103. 103
移動処理
• Set Vector3 XYZとAdd Force
の間にVector3 Multiplyアクシ
ョンを追加します。
• Vector3 Variableをmovement
に、Multiply Byをspeedに設
定(右の二重線をクリックし
て変数を使用するのを忘れず
に)
• Every Frameをチェックしま
す。
- 116. 116
カメラの移動
• もう一つGet Positionを追加
• Game Objectを[Specify Game Object]に変更
• すぐ下に入力欄が増えるので、変数を指定に切り
替える
• 変数playerを選択
• VectorでNew Variable…]を実行
• New Variableと入力欄が表示されるので
[playerPos]と入力し、[Create Variable]ボタンを押
す
- 130. 130
ステージ枠の作成
名前 Position Rotation
West Wall -10, 0, 0 0, 0, 0
East Wall 10, 0, 0 0, 0, 0
North Wall 0, 0, 10 0, 90, 0
South Wall 0, 0, -10 0, 90, 0
Scaleはすべて(0.5, 2, 20.5)
- 133. 133
収集アイテムの作成
• アイテムっぽくするために自転させます
• スクリプトのイメージ
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour {
void Update ()
{
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
}
}
回転する
X軸15度
Y軸30度
Z軸45度
毎フレーム
クラス名
PlayMakerではFSM名
1フレームの時間
- 138. 138
収集アイテムの作成
• 収集アイテムをプレハブにします
• プレハブ(Prefab)
• シーン上で様々な設定を施したゲームオブジェクを資産(Asset)にして再
利用することができる仕組み。
• PrefabをシーンにD&Dすると、同じ設定のゲームオブジェクを作成でき
ます(インスタンス化するとも言う)。
• ProjectビューのPrefabを変更するとシーンに複製されたゲームオブジェ
クもまとめて変更できる
• Prefabからシーンに作成されたゲームオブジェクと変更し、Inspectorの
上にある「Apply」ボタンを押すと変更した内容をProjectビューの
Prefabに適用する。結果、同じPrefabから作成されたゲームオブジェク
も同じように変更される
• Prefabからシーンに作成されたゲームオブジェクと変更し、Inspectorの
上にある「Apply」ボタンを押すと変更を破棄します。
- 149. 149
衝突の発生条件
• 静的コライダー と 静的コライダー
• 衝突イベントが発生しない
• 静的コライダー と動的コライダー
• 衝突イベントが発生する
• 動的コライダー と動的コライダー
• 衝突イベントが発生する
- 204. 204
スクリプト(イメージ)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
<<< 続く >>>
Unity UIのパッケージ名
スコアおよびクリア
メッセージ用
アイテムを取得した数
初期化処理
- 205. 205
スクリプト(イメージ)
<<< 続き >>>
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
<<< 続く >>>
アイテムを取得した数を1つ増やして、
得点を更新
- 222. 222
Unity UIのボタン対応
• Unity UIのボタンが押されるとOnClick()呼ばれ
るイベントが発生します。
• PlayMakerではu GUI Proxy FullのAdd-onパッ
ケージをインストールすると「ON CLICK」と
いう名前のカスタムイベントが発生します。
- 224. 224
処理概要
• Unity UIボタンを追加
• Play Maker U Gui Component Proxyを追加(FSM内でUnity UIのイベントを受信する代理スクリ
プト)
• Restart FSMを追加
• Restartステートを追加
• カスタムイベント > UGUI > ON CLICKイベントを追加
• RestartステートにLoad Levelアクションを追加し、Load Nameに「MiniGame」と入力
• PlayerController FSMの修正
• restart変数(GameObject型)を追加、インスペクタに表示
• Initステート
• [Restart]ボタンを非アクティブにする
• Winステート
• [Restart]ボタンをアクティブにする
• InspectorからRestartボタンへの参照を設定
• UnityのBuild SettingsにMiniGameシーンを追加
Editor's Notes
- 執筆時点では「Ecosystem Brower」(Browserのsが抜けている)のタイプミスをしているみたいです。
- 執筆時点では「Ecosystem Brower」(Browserのsが抜けている)のタイプミスをしているみたいです。
- 執筆時点では「Ecosystem Brower」(Browserのsが抜けている)のタイプミスをしているみたいです。