All balls must be created from script and must form a perfect circle on the plane. Thank you
At first, create script and name it “Circle”. Copy the code which I wrote for you and paste it into the script. Next, create empty GameObject and add this script to it. You have also to create the ball prefab, and drop it into the slot “Ball Prefab” of this script. There is a couple of public variables. “Center” means the center of the circle. “Radius” means the distance of each ball prefab from the “Center”. “Number of balls” - you know that. Circle will be drawn on the x and y axis. If you want it to be drawn on different axes, you have to modify variable “position” inside GetPointOnCircle method accordingly. Here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Circle : MonoBehaviour
{
[SerializeField]
private Transform ballPrefab;
[SerializeField]
private Vector3 center = Vector3.zero;
[SerializeField]
private float radius = 2f;
[SerializeField]
private int numberOfBalls = 10;
private List<Vector3> listOfPoints;
private void Awake()
{
this.CalculatePointsOnCircle();
this.InstantiateBallsOnCircle();
}
private void CalculatePointsOnCircle()
{
this.listOfPoints = new List<Vector3>();
float step = 2* Mathf.PI / this.numberOfBalls;
for(int n = 0; n < this.numberOfBalls; n++)
{
this.listOfPoints.Add
(
this.GetPointOnCircle(this.center, step*n, this.radius)
);
}
}
private void InstantiateBallsOnCircle()
{
foreach (Vector3 point in listOfPoints)
{
Instantiate<Transform>(this.ballPrefab, point, Quaternion.identity).parent = transform;
}
}
private Vector3 GetPointOnCircle(Vector3 center, float t, float radius)
{
Vector3 position;
position.x = center.x + radius * Mathf.Cos(t);
position.y = center.y + radius * Mathf.Sin(t);
position.z = center.z;
return position;
}
}