Answers for "how to instantiate gameobject in a circle in unity 2d"

C#
1

how to get object to spawn in a curcle

for (int i = 0; i < numObjects; i++)
{
    float theta = i * 2 * Mathf.PI / numObjects;
    float x = Mathf.Sin(theta)*radius;
    float y = Mathf.Cos(theta)*radius;
  
    GameObject ob = new GameObject();
    ob.transform.parent = transform;
    ob.transform.position = new Vector3(x, y, 0);  
}
Posted by: Guest on May-05-2020
0

instantiate object in circle

float radius = 1f;
 int amountToSpawn;
 for (int i = 0; i < amountToSpawn; i++)
 {
     float angle = i * Mathf.PI*2f / amountToSpawn;
     Vector3 newPos = new Vector3(Mathf.Cos(angle)*radius, y, Mathf.Sin(angle)*radius);
     GameObject go = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), newPos, Quaternion.identity);
 }
Posted by: Guest on June-14-2020

Code answers related to "how to instantiate gameobject in a circle in unity 2d"

C# Answers by Framework

Browse Popular Code Answers by Language