- ブループリントクラスとプレハブとの違い
- ブループリントクラスの親子関係(Nested Prefabs)
- ブループリントクラスの派生(Prefab Variants)
using UnityEngine;
public class MasterWander : MonoBehaviour
{
public float speed = 3.0f;
public float rotationSpeed = 2.0f;
public float wanderRadius = 2.0f;
public float wanderDistance = 5.0f;
public float wanderJitter = 1.0f;
private Vector3 wanderTarget;
void Start()
{
wanderTarget = transform.position + Random.insideUnitSphere * wanderRadius;
wanderTarget.y = transform.position.y; // Y軸は固定
}
void Update()
{
WanderBehavior();
}
void WanderBehavior()
{
// ランダムな方向に少しずつターゲットを移動
Vector3 randomPoint = Random.insideUnitSphere * wanderJitter;
randomPoint.y = 0; // Y軸は固定
wanderTarget += randomPoint;
wanderTarget = wanderTarget.normalized * wanderRadius;
// 前方にターゲットをオフセット
Vector3 targetPosition = transform.position + transform.forward * wanderDistance + wanderTarget;
targetPosition.y = transform.position.y;
// 目的地の方向に回転
Vector3 direction = (targetPosition - transform.position).normalized;
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp( transform.rotation,
targetRotation,
rotationSpeed * Time.deltaTime);
// 前進
transform.position += transform.forward * speed * Time.deltaTime;
}
}
using UnityEngine;
public class MasterController : MonoBehaviour
{
public GameObject masterSpawn; // 作成するゲームオブジェクト
public int numberOfObjects = 10; // 作成するオブジェクトの個数
public float radius = 5f; // 半径
void Start()
{
SpawnObjects();
}
void SpawnObjects()
{
for (int i = 0; i < numberOfObjects; i++)
{
// ランダムな位置を決める
Vector3 randomPosition = transform.position + (Random.insideUnitSphere * radius);
randomPosition.y = 0;
// 指定された位置にオブジェクトを生成
Instantiate(masterSpawn, randomPosition, Quaternion.identity);
}
}
}
using UnityEngine;
public class FollowMaster : MonoBehaviour
{
private Transform masterTransform;
[SerializeField] private float followDistance = 2.0f;
[SerializeField] private float speed = 5.0f;
[SerializeField] private float rotationSpeed = 5.0f;
public float wanderJitter = 1.0f;
public float wanderRadius = 2.0f;
public float wanderDistance = 5.0f;
private Vector3 wanderTarget;
private void Update()
{
if (transform.parent != null)
{
Vector3 randomPoint = Random.insideUnitSphere * wanderJitter;
randomPoint.y = 0; // Y軸は固定
wanderTarget += randomPoint;
wanderTarget = wanderTarget.normalized * wanderRadius;
// 前方にターゲットをオフセット
masterTransform = transform.parent;
// リーダーの背後をターゲットにする
Vector3 targetPosition = masterTransform.position - masterTransform.forward * followDistance + wanderTarget;
transform.position = Vector3.Lerp(transform.position, targetPosition, speed * Time.deltaTime);
// リーダーの向きに合わせて回転
Quaternion targetRotation = Quaternion.LookRotation(masterTransform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
}
using UnityEngine;
public class MasterController2 : MonoBehaviour
{
public GameObject masterSpawnA; // 1つ目のゲームオブジェクト
public GameObject masterSpawnB; // 2つ目のゲームオブジェクト
public int numberOfObjects = 10; // 作成する合計オブジェクト数
public float radius = 5f; // 生成する範囲の半径
void Start()
{
SpawnObjects();
}
void SpawnObjects()
{
int halfObjects = numberOfObjects / 2; // 均等に分割
for (int i = 0; i < halfObjects; i++)
{
SpawnRandomObject(masterSpawnA);
SpawnRandomObject(masterSpawnB);
}
// 奇数の場合、どちらか一方を追加
if (numberOfObjects % 2 != 0)
{
SpawnRandomObject(Random.value > 0.5f ? masterSpawnA : masterSpawnB);
}
}
void SpawnRandomObject(GameObject obj)
{
Vector3 randomPosition = transform.position + (Random.insideUnitSphere * radius);
randomPosition.y = transform.position.y; // 高さを固定する(地面に配置する場合)
Instantiate(obj, randomPosition, Quaternion.identity);
}
}
変数の作成
Unityと同じように変数を定義します。初期値の設定
wanderTarget、targetPosition、directionの初期値は不要です。Vector型の(0.0, 0.0, 0.0)です。speed | 300.0 |
rotationSpeed | 20.0 |
wanderRadius | 200.0 |
wanderDistance | 500.0 |
wanderJitter | 100.0 |
Begin Playイベントノード
wanderTargetの初期値を設定します。Event Tickイベントノード
WanderBehaviorというカスタムイベントノードを作成して接続します。WanderBehaviorカスタムイベントノード
randomPointとwanderTargetの値を作成します。targetPositionの作成
Sequenceノードの2番目の実行ワイヤーをSet targetPositionノードに接続します。directionの作成
directionの値はFind Look at Rotationノードで作成します。目的地の方向に回転
Sequenceノードの3番目の実行ワイヤーをSet Actor Rotationノードに接続します。前進
Set Actor Rotationノードの後に実行ワイヤーをSet Actor Locationノードに接続します。Make VectorノードのZの値はレベル上にあるFloorのZの値と同じ20.0を入力しておきます。numberOfObjects | 10 |
radius | 20.0 |
BeginPlayイベントノード
SpawnOpjectsカスタムイベントノードに接続します。変数の作成
Unityと同じように変数を定義します。followDistance | 100.0 |
wanderJitter | 3.0 |
wanderRadius | 50.0 |
BeginPlayイベントノード
BeginPlayイベントノードには何も接続しません。Event Tickイベントノード
Event Tickイベントノードの全体図を表します。親アクタの存在チェック
親アクタが存在した場合に実行ワイヤーが先に進むようにしています。先の処理はSequenceノードで分けてます。ある程度揺れながら追従するようにする
wanderTargetを定義して、揺れながら追従するようにしています。前進
SetActorLocationノードに、親アクタの位置から前向きの単位ベクトルにfollowDistanceの距離を掛けた値を引いた値をtargetPositionをSet Actor LocationノードのNew Locationピンに接続します。目的地の方向に回転
アクタの回転の計算をします。変数
ブループリントの親子関係でInkfishアクタに設定した同じ変数をPetアクタに設定します。初期値も同じにします。followDistance | 100.0 |
wanderJitter | 3.0 |
wanderRadius | 50.0 |
BeginPlayイベントノード
ブループリントの親子関係のInkfishアクタに設定したBeginPlayイベントノードと同じく何も接続しません。Event Tickイベントノード
ブループリントの親子関係でInkfishアクタに設定した同じTickイベントノードをPetアクタに設定します。詳細はブループリントの親子関係のInkfishアクタに設定したTickイベントノードを参照してください。BeginPlayイベントノード
BeginPlayイベントノードの全体図を表します。