Make a Circle using LineRenderer in Unity
Category : C# , Full Script , Game Development , Unity
Great little script for Unity that makes a LineRenderer component shape into a circle or ellipse, given the number of line segments along with x and y radius.
Script by MarkPixel. Original thread here. Full Script Below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using UnityEngine; using System.Collections; [RequireComponent(typeof(LineRenderer))] public class LineRenderer_Circle : MonoBehaviour { public int segments; public float xradius; public float yradius; LineRenderer line; void Start () { line = gameObject.GetComponent(); line.SetVertexCount (segments + 1); line.useWorldSpace = false; CreatePoints (); } void CreatePoints () { float x; float y; float z = 0f; float angle = 20f; for (int i = 0; i < (segments + 1); i++) { x = Mathf.Sin (Mathf.Deg2Rad * angle) * xradius; y = Mathf.Cos (Mathf.Deg2Rad * angle) * yradius; line.SetPosition (i,new Vector3(x,y,z) ); angle += (360f / segments); } } } |
Latest posts by Justin Fletcher (see all)
- UITableView scroll to new row only when at the end of table. - December 7, 2015
- Unity Shader for a Scanlines Effect - August 16, 2015
- Unity Shader Alpha Blended With Color - August 16, 2015
1 Comment
Henk
July 29, 2018 at 12:04 pmNot very useful, as you cannot set the location of the circle. It is always at the origin.
Did you even use this code or just copy+paste?