簡體   English   中英

如何找出到 NavMeshAgent 目標的路線

[英]How can I find out the route to the NavMeshAgent target

我想在英雄走過之前提前知道路線,並通過line Render的方式繪制,告訴我是否有可能以任何方式從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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM