RPGの「始まりの街」でランダムに歩いているNPCみたいなやつ
AIThirdPersonController
EthanがもっているAIThirdPersonController
を使用
※Ethanを削除して動かしたいキャラをAIThirdPersonController
の配下に置くが、キャラにrigidbodyがついていると追従してくれないので設定しない
NavMeshAgent
キャラがどのようにシーンを動くのかを決める
AICharacterControl
AICharacterControl
スクリプトのTargetにWalkTarget
をセット
WalkTarget
をランダムな位置に表示して、そこを目がけてキャラを動かせる
RandomPosition.cs
using UnityEngine;
using System.Collections;
public class RandomPosition : MonoBehaviour {
void Start () {
StartCoroutine (RePositionWithDelay());
}
IEnumerator RePositionWithDelay() {
while (true) {
SetRandomPosition ();
// コルーチンを遅延させてから再開させる
yield return new WaitForSeconds (5);
}
}
void SetRandomPosition() {
float x = Random.Range (-5.0f, 5.0f);
float z = Random.Range (-5.0f, 5.0f);
Debug.Log ("x,z: " + x.ToString ("F2") + ", " + z.ToString ("F2"));
transform.position = new Vector3 (x, 0.0f, z);
}
}
参考
https://github.com/oreilly-japan/unity-virtual-reality-projects-ja