AI navigation

基础

image-20240905151937886

属性讲解

Agent Radius:代理半径,决定了烘焙路面距离墙面的距离,当出现两堵墙距离过近导致没有路径出现的情况,可以适当调小该属性

Height:代理高度,限制代理对象的高度,调整数值可以让代理角色通过桥洞或者隧道等有高度限制的场景

Step Height:台阶高度,在这个高度范围内的 台阶 会生成烘焙路径

Max Slope:可以生成烘焙路径的斜坡最大倾斜角度

自动生成外链

Drop Distance:下落高度

Jump Distance:跳跃距离

Component

image-20240905153222293

属性讲解

Nav Mesh Agent:自动寻路代理对象,需要自动寻路的添加该组件

image-20240905153434727

Nav Mesh Obstacle:被其余系统驱动的移动物体,需要Agent主动避开

image-20240905153614516

Nav Links

image-20240905153711292

NavMeshdifler

image-20240905153859128

NavMeshSurface

image-20240905154004547

使用

image-20240905154004547

在需要烘焙的游戏对象上添加以上组件,保持默认设置,点击有右下角的bake按钮就可以在该游戏对象上生成烘焙路径,Agent可以在该对象表面进行寻路操作

每次子对象的变动,都需要重新点击Bake按钮进行烘焙

自动生成外链

image-20240905155334872

手动生成外链

image-20240905155516486

特殊区域

image-20240905155640683 image-20240905155743066

Agent

image-20240905153434727

寻路代理,需要进行寻路操作的游戏对象附加该组件

用法示例

让Agent可以自动寻路到指定position

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using UnityEngine;
using UnityEngine.AI;

public class MoveTo : MonoBehaviour
{
private NavMeshAgent agent;

[SerializeField] Transform target; //要追踪的目标

private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
void Start()
{
agent.destination = target.position;
}


}

A星寻路

优势

相较于Unity自带的寻路系统,这个插件可以将网格保存下来。不需要每次都动态的去搜索一下。

下载路径

http://arongranberg.com/astar/download