1.设置寻路网格
注:使用cube拼接而来,选中所有网格路径之后
选择window->navigation
点击bake即可。
2.
using UnityEngine;
using System.Collections;
public class PlayerAutoMove : MonoBehaviour {
private NavMeshAgent agent;
public float minDistance = 3;
public Transform target;//需要到达的点,可以自行在U3D中设置
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
if (agent.enabled)
{
if (agent.remainingDistance < minDistance)
{
agent.Stop();
agent.enabled = false;
}
}
if (Input.GetMouseButtonDown(0))
{
SetDestination(target.position);
}
}
public void SetDestination(Vector3 targetPos)
{
agent.enabled = true;
agent.SetDestination(targetPos);
}
}