首页 > 解决方案 > 无法在 Unity 中将路径点与线渲染器连接起来

问题描述

我正在尝试生成一个路径系统,其中一条线随着时间的推移而呈现。我的代码是:

public IEnumerator DrawPath(List<Vector3> pathWay)
    {

        lineRenderer.positionCount = 0;

        points = new Vector3[pathWay.Count];

        for (int i = 0; i < pathWay.Count; i++)
        {
            Vector3 checkpointpos = pathWay[i];
            points[i] = new Vector3(checkpointpos.x, checkpointpos.y, checkpointpos.z);
        }


        lineRenderer.startWidth = 1f;
        lineRenderer.endWidth = 1f;
        lineRenderer.positionCount = points.Length;

        for (int i = 0; i < points.Length; i++)
        {
            //Mathf.Lerp
            yield return new WaitForSeconds(0.1f);
            lineRenderer.SetPosition(i, points[i]);
        }
        
    }

输出:

在此处输入图像描述

List<Vector3>从 A* Pathfinding 免费版插件中获得。任何形式的帮助将不胜感激。提前致谢。

标签: c#unity3d

解决方案


好的,我解决了这个问题。

问题是一次设置行数,所以每当我设置一个新向量时,它从位置 0 开始,这就是我遇到问题的原因。

解决方案:

 public IEnumerator DrawPath(List<Vector3> pathWay)
    {

        lineRenderer.positionCount = 0;
        points = new Vector3[pathWay.Count];

        lineRenderer.startWidth = 1f;
        lineRenderer.endWidth = 1f;

        for (int i = 0; i < pathWay.Count; i++)
        {
            Vector3 checkpointpos = pathWay[i];
            points[i] = new Vector3(checkpointpos.x, checkpointpos.y, checkpointpos.z);

            yield return new WaitForSeconds(0.1f);
            lineRenderer.positionCount++;
            lineRenderer.SetPosition(i, points[i]);
        }
    }

输出:

在此处输入图像描述


推荐阅读