Use Unity's LineRenderer to draw a circle on a GameObject

Would you like to draw circles at runtime with C# in Unity? This article contains an extension method that allows you to add a DrawCircle function to your GameObjects.

I’m preparing a Unity exercise to draw our Solar System and need some helper circles to indicate orbits like this:



Unity does not have a DrawCircle method so we need to create it ourselves. For this, we use a component called LineRenderer that allows you to draw lines on a GameObject. We write the code as an extension method to extend GameObject with a method called DrawCircle. To add it to your project, do this:

using UnityEngine;

public static class GameObjectEx
    public static void DrawCircle(this GameObject container, float radius, float lineWidth)
        var segments = 360;
        var line = container.AddComponent<LineRenderer>();
        line.useWorldSpace = false;
        line.startWidth = lineWidth;
        line.endWidth = lineWidth;
        line.positionCount = segments + 1;

        var pointCount = segments + 1; // add extra point to make startpoint and endpoint the same to close the circle
        var points = new Vector3[pointCount];

        for (int i = 0; i < pointCount; i++)
            var rad = Mathf.Deg2Rad * (i * 360f / segments);
            points[i] = new Vector3(Mathf.Sin(rad) * radius, 0, Mathf.Cos(rad) * radius);


How to call the new DrawCircle extension method?

var go1 = new GameObject { name = "Circle" };
go1.DrawCircle(1, .02f);

Since we use line.useWorldSpace = false, the circle will be drawn relatively to the GameObject. That means that our extension method does not need to transform or rotate the individual points. If you want a transformation, you can add that to the GameObject like this:

var go2 = new GameObject { name = "Circle2" };
go2.transform.Rotate(30f, 0, 0);
go2.transform.Translate(new Vector3(.5f, 0, 0));
go2.DrawCircle(1.5f, .02f);

This is how it looks when you draw both circles in Unity:


Grüsse aus Berlin!
This is a cookie free and popup free website.
Join me in making the web enjoyable again by removing the need for annoying cookie warnings and silly newsletter overlays!