2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ステージをランダムに移動させる

Posted at

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

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?