我怎样才能找到通往 NavMeshAgent 目标的路线

How can I find out the route to the NavMeshAgent target

我想在英雄走过之前提前知道路线,用线Render的方式画出来,告诉我有没有办法在不让body的情况下从agent那里找出路线在他面前,他会已经划清界限,我将非常感谢任何信息

截图中英雄使用NavMeshAgent移动通过点,我能找出路线或点到目标吗

public class DisplayedPath : MonoBehaviour
{
    public LineRenderer line; //to hold the line Renderer
    public Transform target; //to hold the transform of the target
    public NavMeshAgent agent; //to hold the agent of this gameObject

    private void Start()
    {
        line = GetComponent<LineRenderer>(); //get the line renderer
        agent = GetComponent<NavMeshAgent>(); //get the agent
                                              //   target = transform;
        StartCoroutine(getPath());
    }

    public IEnumerator getPath()
    {
        // line.SetPosition(0, transform.position); //set the line's origin

        agent.SetDestination(target.position); //create the path
        yield return new WaitForEndOfFrame(); //wait for the path to generate

        DrawPath(agent.path);

        agent.Stop();//add this if you don't want to move the agent
    }

    public void DrawPath(NavMeshPath path)
    {
        if (path.corners.Length < 2) //if the path has 1 or no corners, there is no need
            return;

        line.SetVertexCount(path.corners.Length); //set the array of positions to the amount of corners

        line.SetPosition(0, transform.GetChild(0).position); //set the line's origin


        for (var i = 1; i < path.corners.Length; i++)
        {
            Vector3 up = path.corners[i];
            up.y += 1.5f;
            line.SetPosition(i, up); //go through each corner and set that to the line renderer's position
        }
    }
}

感谢 TEEBQNE。