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.How can close shapes with more than 3 points without any visible gaps?
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 = 5;
[Tooltip("The number of vertices in the circle.")]
public int Points = 100;
[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);
}
}
If you always want your points to connect then I recommend enabling “loop”. I modified your script and annotated the changes so you can see what I did.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircleDrawing : MonoBehaviour
{
[Tooltip("The radius of the circle, measured in world units.")]
public float Radius = 5;
[Tooltip("The number of vertices in the circle.")]
public int Points = 100;
[Tooltip("The color of the circle.")]
public Color Color;
[Tooltip("The delay between creating shape points.")]
public float CreateDelay = 0.01f;
private LineRenderer lineRenderer;
public void Awake()
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
lineRenderer.material.color = Color;
lineRenderer.loop = true; //Added loop so that points can connect.
lineRenderer.startWidth = lineRenderer.endWidth = 0.5f;
lineRenderer.positionCount = Points + 1; //+1 to close the shape || You don't need this if you use "loop"
Draw();
}
//Suggestion, make this public so it can be called from other scripts or buttons and then this method can activate a coroutine.
private void Draw()
{
StartCoroutine(DrawShapes());
}
IEnumerator DrawShapes()
{
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;
yield return new WaitForSeconds(CreateDelay); //You can use WaitForSecondsRealtime so that it isn't affected by TimeScale=0 and can be used in a pause menu or something
}
}
private void OnDestroy()
{
Destroy(lineRenderer.material);
}
}
I made the changes you suggested and I realized that the width for the last line always ended up slightly thinner than the others as in https://imgur.com/a/wOSdd
Also I wanted to animate the shapes by a small portion of a line not line by line. As seen in https://imgur.com/a/gOJ0z
I hope this gives a better idea of what I am trying to achieve