常见寻路方法:路点寻路,单元格寻路,网格寻路
一,导航网格寻路(NavMesh)
实现方法:(1)烘焙寻路路面(2)给寻路者添加代理组件NavMeshAgnet(3)程序实现寻路。
实例:首先搭建场景,这里使用plane作为路面,cube作为障碍物。

?标记场景中对象设置为静态:

?烘焙路面,选中路面plane,在顶部Windows栏中找到点击Navigation,点击Bake进行烘焙。

?新建sphere作为目标点与capsule作为寻路者

?给capsule添加Nav Mesh Agent组件

?添加寻路脚本,注意寻路组件需要添加命名空间:using UnityEngine.AI;
API:寻路组件.SetDestination(目标点位置);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
/// <summary>
///
/// </summary>
public class NavMeshText : MonoBehaviour
{
//目标点位置
public Transform target;
//寻路组件
NavMeshAgent nav;
void Start ()
{
//获取组件
nav = GetComponent<NavMeshAgent>();
}
void Update ()
{
//设置目标点进行寻路
nav.SetDestination(target.position);
}
}
将脚本挂载给capsule添加寻路目标sphere,运行实现寻路
|