Unity3d 如何让游戏对象随机巡逻?

Unity3d 如何让游戏对象随机巡逻?,unity3d,Unity3d,我正在制作一个钓鱼游戏,我想知道是否有办法让游戏对象随机选择一个航路点,这样它们就不会遵循相同的模式 public class Simple_AI_patrol : MonoBehaviour { public Transform[] wayPoints; public float speed; public int currentPoint; public bool doPatrol = true; Vector3 velocity; Vector3 moveDirection; Vecto

我正在制作一个钓鱼游戏,我想知道是否有办法让游戏对象随机选择一个航路点,这样它们就不会遵循相同的模式

public class Simple_AI_patrol : MonoBehaviour {

public Transform[] wayPoints;
public float speed;
public int currentPoint;
public bool doPatrol = true;
Vector3 velocity;
Vector3 moveDirection;
Vector3 target;
Rigidbody rb;


void Start () {
    rb = GetComponent<Rigidbody> ();
}


void FixedUpdate () {
        if(currentPoint < wayPoints.Length){
            target = wayPoints [currentPoint].position;
            moveDirection = target - transform.position;
            velocity = rb.velocity;

            if(moveDirection.magnitude < 1){
                currentPoint = Random.Range (0, wayPoints.Length);// or currentPoint++
            }else{
                velocity = moveDirection.normalized * speed;
            }
        }else{
            if(doPatrol){
                currentPoint = 0;
            }else{
                velocity = Vector3.zero;
            }
        }
        rb.velocity = velocity;
        transform.LookAt (target);
    }
}
公共类简单AI巡逻:单一行为{
公共转换[]航路点;
公众浮标速度;
公共点;
公共bool doPatrol=真;
矢量3速度;
矢量3移动方向;
矢量3靶;
刚体rb;
无效开始(){
rb=GetComponent();
}
无效固定更新(){
if(当前点<航路点长度){
目标=航路点[currentPoint]。位置;
移动方向=目标-变换位置;
速度=rb.速度;
如果(移动方向.幅值<1){
currentPoint=Random.Range(0,wayPoints.Length);//或currentPoint++
}否则{
速度=移动方向。归一化*速度;
}
}否则{
中频(多帕托){
电流点=0;
}否则{
速度=矢量3.0;
}
}
rb.速度=速度;
transform.LookAt(目标);
}
}

将其放入Start函数,为随机数生成器提供一个相当独特的种子


Random.InitState((int)System.DateTime.Now.Ticks)

请注意,如果您在同一个标记上实例化所有自治代理,它们将以相同的种子结束。不过,这可能仍然是最好的选择。