如何在不留间隙的情况下为线渲染器形状设置动画 [英] How to animate line renderer shapes without leaving a gap

查看:96
本文介绍了如何在不留间隙的情况下为线渲染器形状设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码根据点数使用线渲染器创建形状.对于大于3的点(三角形等),第一个和最后一个点不会像其他点那样闭合形状.

I am using the code below to create shapes with a line renderer based on the number of points. For points greater than 3 (triangle shape and so on) the first and last points don't close the shape in the the way that the other points do.

1 .如何关闭超过3个点且没有可见间隙的形状?

1.How can close shapes with more than 3 points without any visible gaps?

2 .如何为形状设置动画,使其在特定的持续时间内绘制线条(可能使用协程)?

2.How can I animate the shape so it draws the lines over a specific duration (possibly using a coroutine)?

public class CircleDrawing : MonoBehaviour
{

    [Tooltip("The radius of the circle, measured in world units.")]
    public float Radius = 2;

    [Tooltip("The number of vertices in the circle.")]
    public int Points = 5;

    [Tooltip("The color of the circle.")]
    public Color Color;

    private LineRenderer lineRenderer;

    public void Awake()
    {
        lineRenderer = gameObject.AddComponent<LineRenderer>();
        lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
        lineRenderer.material.color = Color;
        lineRenderer.startWidth = lineRenderer.endWidth = 0.5f;
        lineRenderer.positionCount = Points + 1;    //+1 to close the shape
        Draw();
    }

    private void Draw()
    {
        float angle = 0f;
        for (int i = 0; i <= Points; i++)
        {
            float x = Radius * Mathf.Cos(angle) + transform.position.x;
            float y = Radius * Mathf.Sin(angle) + transform.position.y;
            lineRenderer.SetPosition(i, new Vector3(x, y, 0.01f)); //Z is slightly behind the paddle so it draws in front
            angle += (2f * Mathf.PI) / Points;
        }
    }

    private void OnDestroy()
    {
        Destroy(lineRenderer.material);
    }
}

推荐答案

如果确保LineRenderer的最后一个点与第一个点相同,则它应始终封闭任何给定的形状.像for (int i = 0; i < Points - 1; i++)这样运行for循环(所以每个点除了最后一个点,也是<而不是<=). for循环完成后,用lineRenderer.SetPosition(Point - 1, lineRenderer.GetPosition(0));关闭形状.

If you make sure that the last point of your LineRenderer is the same as the first point it should always close any given shape. Run the for loop like this for (int i = 0; i < Points - 1; i++) (so each point but the last one, also < and not <=). Then close the shape with lineRenderer.SetPosition(Point - 1, lineRenderer.GetPosition(0)); when the for loop is done.

请注意,数组从0开始,这就是Point - 1是lineRenderer的最后一点的原因.

Note here that Arrays start at 0, that's why Point - 1 is the last point of your lineRenderer.

对于动画,我不知道一种简单的方法.我要做的是随着时间的推移使用协程将每个点移向最终目的地.例如,您首先添加第一个点,然后将第二个点添加到第一个点的顶部.然后,将第二点(随着时间在协程中移动)朝其最终位置移动(使用SetPosition进行移动).到达最终位置后,在其顶部添加第三个点,然后将其移至最终位置.对每个点重复一次.

For the animation I don't know an easy way to do it. What I would do is use a coroutine to move each point toward is final destination over time. For example you start by adding the first point and you add the second point on top of the first point. Then you move (over time in a coroutine) the second point toward it's final position (use SetPosition to move it). When it has reached it's final position add the third point on top of it, and move it to it's final position. Repeat for every points.

这篇关于如何在不留间隙的情况下为线渲染器形状设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆