1.对我们的可寻路场景物体进行Navagition的烘焙(Bake),可寻路的物体我们将它设置为静态(static),我们在Areas中设置要bake的物体为什么区域,修改权重(Cost)。 2.给物体添加NavMeshAgent 3.添加脚本,引入UnityEngine.AI命名空间
代码语言:javascript复制 private NavMeshAgent navMeshAgent;
public float speed;
public Transform Target;
private void Awake()
{
navMeshAgent = GetComponent<NavMeshAgent>();
navMeshAgent.speed = speed;
}
private void Start()
{
navMeshAgent.SetDestination(Target.position);
}
OffMeshLink:
Link allowing movement outside the planar navigation mesh. OffMeshLink component allows you to incorporate navigation shortcuts which cannot be represented using a walkable surface. For example, jumping over a ditch or a fence, or opening a door before walking through it, can all be described as Off-mesh links. Tips: 有小圆圈说明成功创建了
OffMesh.PNG
Areas 在Navigation中添加新的Areas
Areas.PNG
NavMeshObstacle: 在场景中添加动态的障碍物 将场景中新产生的物体设置为寻路中的障碍,默认网格不会重新烘焙,勾选Carve之后会make a cut-out in the navmesh.
A NavMeshObstacle is cylindrical in shape and can move around the surface of the NavMesh with a specified velocity. By default, the obstacle will only affect the agent's avoidance behaviour rather than the pathfinding. This means that the agent will ignore the obstacle when plotting a path but will sidestep around it while moving along the path. If carving is enabled, the obstacle will create a temporary "hole" in the NavMesh. The hole will be recognised by the pathfinding, so paths will be plotted to avoid the obstacle. This means that if, say, an obstacle blocks a narrow gap, the pathfinding will seek an alternative route to the target. Without carving, the agent will head for the gap but won't be able to pass until the obstacle is clear.