1.创建工程。在Package Manager中导入AI Navigation
2.给地形添加NavMeshSurface组件
简单点Bake看一下效果。
给需要寻路的物体添加Nav Mesh Agent组件
给需要寻路的物体创建一个脚本,用于控制。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class xunlu : MonoBehaviour
{
public NavMeshAgent agent;
public Transform target;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
agent.SetDestination(target.position);
}
}