モブ子さん(rigidBody+カプセルコライダー)と壁(コライダーのみ)をprefabにしておいて動的生成で画面にばらまいて、
モブ子さんはランダム移動(前進して、立ち止まって、しばらくすると向きを変えるみたいな動き)
初期化の動的生成
using UnityEngine; using System.Collections; public class main : MonoBehaviour { public GameObject ponny; public GameObject wall; void Start () { for(int i=0;i<=30;i++) { Vector3 pos = new Vector3( Random.Range (-10f, 10f) , 0f , Random.Range (-10f, 10f) ); Instantiate (ponny,pos, Quaternion.identity); } for(int i=0;i<=30;i++) { Vector3 pos = new Vector3( Random.Range (-20f, 20f) , 0.89f , Random.Range (-20f, 20f) ); GameObject w = Instantiate (wall,pos, Quaternion.identity) as GameObject; w.transform.rotation = Quaternion.Euler( 0, Random.Range (0f,180f) , 0f); } } }
AIの動作。壁に当たると立ち止まる。
using UnityEngine; using System.Collections; public class AI : MonoBehaviour { Animator anim; float speed = 1f; float ax=0; float ay=0; Vector3 dir; float dis=0f; void Start () { anim= GetComponent<Animator>(); } void Update () { dis-=Time.deltaTime; if(dis<=0f) { ax = 0f; ay = 0f; dis=0f; } if(ax==0 && ay==0) { float flag = Random.Range (0f, 1000f); if(flag <= 5f ) { ax = Random.Range (-1, 2); ay = Random.Range (-1, 2); dis = Random.Range (3f, 10f); } } if (ay==0 && ax==0 ) anim.SetBool("Walk",false); else { dir = new Vector3( ax,0,ay) * Time.deltaTime * 2.5f; anim.SetBool("Walk",true); transform.rotation = Quaternion.LookRotation(dir); transform.position += dir; } } void OnCollisionEnter(Collision other){ ax=0; ay=0;dis=0f; } }