Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw a circle in Unity3D?

Tags:

unity3d

How to draw circle in Unity 3d? I want to draw a circle around different objects. The radiuses of the circles are different and the circles have textures - squares.

like image 288
Oksana Avatar asked Dec 04 '12 17:12

Oksana


1 Answers

I found a big error with this code. The number of points (Size) shouldn't be "(2 * pi / theta_scale) + 1" because this causes the circle to draw 6.28 times. The size should be "1 / theta_scale + 1". So for a theta_scale of 0.01 it needs to draw 100 points, and for a theta_scale of 0.1 it needs to draw 10 points. Otherwise it would draw 62 times and 628 times respectively. Here is the code I used.

using UnityEngine;
using System.Collections;

public class DrawRadar: MonoBehaviour {
    public float ThetaScale = 0.01 f;
    public float radius = 3 f;
    private int Size;
    private LineRenderer LineDrawer;
    private float Theta = 0 f;

    void Start() {
        LineDrawer = GetComponent < LineRenderer > ();
    }

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

If you modify the number in "Size" that is divided by ThetaScale, you can make a sweeping gauge/pie chart type graphic.

like image 133
MrMike Avatar answered Sep 21 '22 15:09

MrMike