1、首先在场景中铺设一个Plane,设置scale为(5,5,5);
2、添加一个Cube,命名一个Wandering的C#脚本,将其挂载到Cube当中;
3、代码如下:
using UnityEngine;
using System ;
public class Wandering : MonoBehaviour {
public float speed=1;
public int channge=1;
//public float time=1f;
//public int ok;
// Use this for initialization
void Start () {
transform.position = new Vector3 (UnityEngine.Random.Range (-20f, 20f), 0.5f, UnityEngine.Random.Range (-20f, 20f));
}
// Update is called once per frame
void Update () {
//time += 0.01f;
/*if(time %2f<=0.1f){
ok = UnityEngine.Random.Range (1, 10);
if(ok==1)
channge = UnityEngine.Random.Range (1, 4);
}*/
if (channge == 1) {
transform.position += new Vector3 (speed * Time.deltaTime,0,0);
Quaternion lookRot = Quaternion.LookRotation (new Vector3 (1f, 0, 0));
transform.rotation = Quaternion.Slerp (transform.rotation, lookRot, speed * Time.deltaTime);
}
if (channge == 2) {
transform.position -=