1:建造一个简单的场景
2:选中两个球之外所有的物体,然后在Inspector——Static——Navigation Static,选中要寻路的物体添加在导航栏选中Component——Navigation——Nav Mesh Agent组件,在导航栏打开Window——Navigation窗口,在Navigation窗口里面选择Bake然后再选择右下角的Bake。
3:上面所有的设置好后上代码,把这个代码组件放到寻路的物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AI : MonoBehaviour {
public GameObject target;
private NavMeshAgent navMeshAgent;
void Start ()
{
navMeshAgent = GetComponent<NavMeshAgent>();
if(navMeshAgent==null)
{
navMeshAgent = gameObject.AddComponent<NavMeshAgent>();
}
}
void Update ()
{
navMeshAgent.SetDestination(target.transform.position);
}
}
然后把目标拖到代码组件的Target里面
然后运行就可以了。