1。在Bake 前要選障礙物勾選「Navigation Static」
2。選擇「Windows->Navigation」
3.。要自動行走的元件,在add component中加入導航代理 Nav Mesh Agent
導航程式
變數
public Transform target;
private NavMeshAgent _agent;
在start()中
_agent = GetComponent<NavMeshAgent>();
在update()中
_agent.destination= target.position;
亂數生成球,然後導航到目的
using UnityEngine;
using System.Collections;
public class produceballs : MonoBehaviour {
public GameObject ball1;
private Vector3 offset;
private float TimeCount;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
TimeCount -= Time.deltaTime;
if (TimeCount <=0) {
TimeCount = 5.0f;
float v = Random.Range (-15, -10);
float h = Random.Range (-100,100);
offset = transform.position + new Vector3 (5.0f*v, 2, h);
//transform.position = new Vector3 (50.0f, 2, 50.0f);
Instantiate(ball1,offset , transform.rotation) ;
}
}
}