0
\$\begingroup\$

Why does the following code not work?

public float ThetaScale = 0.0001f;
private int Size;
private LineRenderer LineDrawer;
private float Theta = 0f;

void Start ()
 {
  LineDrawer = GetComponent<LineRenderer>();
  LineDrawer.SetWidth(0.1f, 0.1f);
  LineDrawer.material = new Material(Shader.Find("Mobile/Particles/Additive"));
  LineDrawer.SetColors(Color.blue, Color.blue);
 }


void DrawingCircle(float radius)
{
Theta = 0f;
Size = (int)((1f / ThetaScale) + 1f);
LineDrawer.SetVertexCount(Size);
for(int i = 0; i < Size; i++){
    Theta += (2.0f * Mathf.PI * ThetaScale);
    float x = radius * Mathf.Cos(Theta);
    float y = radius * Mathf.Sin(Theta);
    LineDrawer.SetPosition(i, new Vector3(x, y, 0));
}}


void Update ()
{
    for(int l = 0; l < 3; l++)
    {
        DrawingCircle(l);
    }
 }

I want to plot a multiple circles like that

enter image description here

What's a good way to draw multiple circles?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ What does your current code do, and how does it differ from what you want? \$\endgroup\$
    – DMGregory
    Nov 25, 2019 at 23:13
  • \$\begingroup\$ A LineRenderer is a component like any other. You don't need to update it every frame even when nothing changed. \$\endgroup\$
    – Philipp
    Nov 26, 2019 at 11:49
  • 1
    \$\begingroup\$ Note also that you are using one render for all three circles but you are overwriting the previous circle each time so you will only ever see the last circle \$\endgroup\$
    – Ed Marty
    Nov 26, 2019 at 13:49

1 Answer 1

1
\$\begingroup\$

I think your confusion comes from a misunderstanding of what the LineRenderer component does.

A LineRenderer isn't a "line drawer". It doesn't draw lines. It visualizes a game object in form of a visible polygon strip in 3d space. Just like a SpriteRenderer visualizes a game object as a sprite or a MeshRenderer visualizes a game object as a 3d mesh.

So when you want 3 separate lines in your game, you need 3 different game objects with a LineRenderer each. When their coordinates don't change, you only need to set them once. It's not necessary to reassign their components in every update.

\$\endgroup\$
1
  • \$\begingroup\$ I think you could get away with one GameObject with three line renderer components attached to it. \$\endgroup\$ Dec 26, 2019 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .