首页 > 解决方案 > 如何统一获取线渲染器的所有点位置?

问题描述

我想获取线渲染器线中的所有点位置,我需要这些点位置,因为我想逐行绘制线渲染器,然后让球在这条线上移动,所以我想使用线的点位置移动这个球到画完后让球在这条线上移动

这是我使用线条渲染器绘制线条的代码

public LineRenderer lineRend;
public EdgeCollider2D edgeColl;

List <Vector2> points;

public void Update_line(Vector2 mouse_pos)
{
    if (points == null)
    {
        points = new List<Vector2> ();

        set_point(mouse_pos);

        return;
    }

    if (Vector2.Distance(points[points.Count - 1], mouse_pos) > .1f)
    {
        set_point(mouse_pos);
    }

}

void set_point(Vector2 point)
{
    points.Add(point);

    lineRend.positionCount = points.Count;
    lineRend.SetPosition(points.Count - 1, point);

    if (points.Count > 1)
    {
        edgeColl.points = points.ToArray();
    }
}

}

下图说明了我想在我的游戏中做什么,位于图像底部的球我希望它在画线上移动

插图图像

那么我必须通过代码执行此操作吗?

标签: c#unity3d

解决方案


lineRend.GetPositions(out Vector3[] allPoints);

GetPositions在 out 变量中返回线渲染器上的所有点。它的数组Vector3

https://docs.unity3d.com/ScriptReference/LineRenderer.GetPositions.html


推荐阅读