June 8, 2015
Drawing a circle using Gizmos in Unity3D
In Unity game engine editor, there are options to draw a box or a sphere in Gizmos. At times, we may need to draw simple 2d circles using Gizmos. But there is no API for drawing circles in gizmos. There is a method to draw lines in gizmos, we can use that method to draw multiple connected lines to make a circle. The code is given below;
#pragma strict @script ExecuteInEditMode() var Radius:float = 4; function OnDrawGizmosSelected(){ var T:Transform = GetComponent(Transform); Gizmos.color = Color.white; var theta:float = 0; var x:float = Radius*Mathf.Cos(theta); var y:float = Radius*Mathf.Sin(theta); var pos:Vector3 = T.position+Vector3(x,0,y); var newPos:Vector3 = pos; var lastPos:Vector3 = pos; for(theta = 0.1;theta<Mathf.PI*2;theta+=0.1){ x = Radius*Mathf.Cos(theta); y = Radius*Mathf.Sin(theta); newPos = T.position+Vector3(x,0,y); Gizmos.DrawLine(pos,newPos); pos = newPos; } Gizmos.DrawLine(pos,lastPos); }
You can add this script into a component and when we select the gameobject we can see the circle. Thanks for reading.
[Total: 12 Average: 2.6/5]