Version: 2017.2
public bool CalculatePath (Vector3 targetPosition, AI.NavMeshPath path);

参数

targetPosition 所请求路径的最终位置。
path 生成的路径。

返回

bool 如果找到路径,则为 true。

描述

计算到指定点的路径并存储生成的路径。

此函数可用于提前规划路径,以避免游戏中需要该路径时发生延迟。 另一个用途是在移动代理之前 检查目标位置是否可到达。

using UnityEngine;
using UnityEngine.AI;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform target; private NavMeshAgent agent; void Start() { agent = GetComponent<NavMeshAgent>(); NavMeshPath path = new NavMeshPath(); agent.CalculatePath(target.position, path); if (path.status == NavMeshPathStatus.PathPartial) { } } }