using UnityEngine;
namespace NextMind.Examples.Utility
{
///
/// This component will place children of this GameObject on a circle around it.
///
public class CircleLayout : MonoBehaviour
{
///
/// The cicrle radius.
///
[SerializeField]
private float radius = 1;
///
/// The angle offset in degrees.
///
[SerializeField]
private float startOffset = 0;
///
/// Should we position the children around the Z axis?
///
[SerializeField]
private bool aroundZAxis = true;
///
/// Should the children look at the circle's center?
///
[SerializeField]
private bool lookAtCenter = false;
///
/// Position the the children in a clockwise order?
///
[SerializeField]
private bool clockwise = false;
public void Start()
{
PlaceObjects();
}
private void PlaceObjects()
{
int childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
float degAngle = 360f * ((float)i / childCount) + startOffset;
float radAngle = degAngle * Mathf.Deg2Rad;
Transform t = transform.GetChild(i);
float sinValue = radius * Mathf.Sin(radAngle);
if (clockwise)
{
sinValue *= -1;
}
t.localPosition = new Vector3(radius * Mathf.Cos(radAngle), aroundZAxis? t.localPosition.y:sinValue, aroundZAxis? sinValue : t.localPosition.z);
if (lookAtCenter)
{
t.LookAt(this.transform);
}
}
}
}
}