Draw an Ellipse in Unity 3D

I’m trying to write a c-sharp code to draw an ellipse in Unity 3D.
Suppose we have the following parameters:

  1. Semi-major axis (a).
  2. Semi-minor axis (b).
  3. ellipse center (h,k).
  4. and 2D rotation (theta).

The method should look like:

void CreateEllipse(int a, int b, int h, int k, float theta)

How it possible to draw the ellipse using the above parameters.

I installed Unity 4 ; version 4.3.4f1 non-pro.

Any help please.

Based on @robertbu’s script. Here is a slightly improved version:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent (typeof(LineRenderer))]
public class Ellipse : MonoBehaviour 
{
	public Vector2 radius = new Vector2(1f, 1f);
	public float width = 1f;
	public float rotationAngle = 45;
	public int resolution = 500;

	private Vector3[] positions;
	private LineRenderer self_lineRenderer;
	
	
	void OnValidate()
	{
		UpdateEllipse();
	}
	
	public void UpdateEllipse()
	{
		if ( self_lineRenderer == null)
			self_lineRenderer = GetComponent<LineRenderer>();
			
		self_lineRenderer.SetVertexCount (resolution+3);
		
		self_lineRenderer.SetWidth(width, width);
		
		
		AddPointToLineRenderer(0f, 0);
		for (int i = 1; i <= resolution + 1; i++) 
		{
			AddPointToLineRenderer((float)i / (float)(resolution) * 2.0f * Mathf.PI, i);
		}
		AddPointToLineRenderer(0f, resolution + 2);
	}
	
	void AddPointToLineRenderer(float angle, int index)
	{
		Quaternion pointQuaternion = Quaternion.AngleAxis (rotationAngle, Vector3.forward);
		Vector3 pointPosition;
		
		pointPosition = new Vector3(radius.x * Mathf.Cos (angle), radius.y * Mathf.Sin (angle), 0.0f);
		pointPosition = pointQuaternion * pointPosition;
		
		self_lineRenderer.SetPosition(index, pointPosition);		
	}
}
  • Fixes a gap that shows up between the start and end of the LineRenderer ( by adding two extra points that hide it)
  • Works both in edit mode and runtime
  • Uses the transform’s position instead of the center variable ( make sure to disable Use World Position in the LineRenderer)
  • Updates the ellipse as soon as a change in values takes place ( instead of on Start only)
  • Width of the LineRenderer is editable from the script’s inspector, instead of the LineRenderer’s

Here is a bit of code that draws and ellipse using the LineRenderer. Attach it to a game object with a LineRenderer component added.

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(LineRenderer))]
public class Ellipse : MonoBehaviour {

	public float a = 5;
	public float b = 3;
	public float h = 1;
	public float k = 1;
	public float theta = 45;
	public int resolution = 1000;

	private Vector3[] positions;
	
	void Start () {
		positions = CreateEllipse(a,b,h,k,theta,resolution);
		LineRenderer lr = GetComponent<LineRenderer>();
		lr.SetVertexCount (resolution+1);
		for (int i = 0; i <= resolution; i++) {
			lr.SetPosition(i, positions*);*
  •  }*
    
  • }*

  • Vector3 CreateEllipse(float a, float b, float h, float k, float theta, int resolution) {*

  •  positions = new Vector3[resolution+1];*
    
  •  Quaternion q = Quaternion.AngleAxis (theta, Vector3.forward);*
    
  •  Vector3 center = new Vector3(h,k,0.0f);*
    
  •  for (int i = 0; i <= resolution; i++) {*
    

_ float angle = (float)i / (float)resolution * 2.0f * Mathf.PI;_
positions = new Vector3(a * Mathf.Cos (angle), b * Mathf.Sin (angle), 0.0f);
positions = q * positions + center;
* }*

* return positions;*
* }*
}

@Sonoshee it works perfect, thank you!