微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何找出通往 NavMeshAgent 目标的路线

如何解决如何找出通往 NavMeshAgent 目标的路线

我想在英雄走过之前提前知道路线,并通过线Render绘制,告诉我是否可以在不让身体在前面的情况下以任何方式从agent中找出路线他已经划清界限,我将非常感谢任何信息

截图中,英雄使用NavMeshAgent穿过点,我能找到路线或指向目标吗

enter image description here

解决方法

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。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。