# Help, need to place objects in a circle

Is there anyone who knows how to place several object at the outside of a circle?
i’m struggeling to know how to tell objects to relocate at a cirkle, and not overlap.
i’m using these codes, that allows me to tell objects to move in a cirkle, but i want them to be fixed placed instead

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
float ang = 0;
float cx = 0;
float cy = 0;
float x = 0;
float y =0;

// Start is called before the first frame update
void Start()
{
print(“cubed”);

}
// Update is called once per frame
void Update()
{

x = cx + rad * Mathf.Cos(ang);
y = cy + rad * Mathf.Sin(ang);

transform.localPosition = new Vector3(y,0f,x);
if (ang<2*Mathf.PI)
{
ang +=Mathf.PI/900;
}
else
{
ang = 0;
}

}
}

``````GameObject objtospawn;
int count;

private void Start()
{
for(int i = 0; i < count; ++i)
{
float circleposition = (float)i / (float)count;
float x = Mathf.Sin( circleposition * Mathf.PI * 2.0f ) * radius;
float z = Mathf.Cos( circleposition * Mathf.PI * 2.0f ) * radius;
Instantiater(objtospawn, new Vector3( x , 0.0f , z ), Quaternion.identity);
}
}
``````

Code untested! Should spawn the object count number of times equally distributed on a circle on the xz plane. This only runs once with Start. Any reason you do this every frame in Update?

1 Like

looks like a great solution, unfortunatly i’m not the most advanced in programming, so i’m not sure how to apply this script?? anyway you can help me out ?

Anyway you can specify what you have problems with? What does not work? What the issue is?
I’m not sure how to help when I don’t know what is wrong.

i’m searching for a solution, that allows me to tell gameobjects that are created, to relocate in a pattern on a circle. for instance telling a cube with a subcategory of a sphere to both move to the circle. The idea is that i want to prove that a group of objects can be sorted out to form a circle around a fix point or the camera I’ve linked a picture illustrationg the concept of it, when having the elements inside a 3D space, and then when the game is played, they move to form the circle around the target or camera hope that can clearefy some of it.

So you still don’t tell what you need help with. Where is the issue? My snippet should give you a good start.
If you don’t know Unity/programming the wisest choice would be to read a book or do some tutorials.

Note: My definition of helping does not mean doing the work for someone else. It means giving hints, suggestions or pointing to mistakes. You will have a hard time finding someone doing your (home?)work for you. But you will find plenty people lending a hand with CONCRETE issues.

i appreciate the answer, and the help given. I’m most certainly not looking for someone to just hand me all the answers, but the program i’m doing is also a bit more complex than just the question i’m asking. in fact i’m looking for methods on how to tell different objects by their B7AA codes or other usefull standardisations in the building industry. The task is to load in a full building or hospital, and eventually end up with a VR view where the user don’t nessecerly look at the 3D representation of the building, but split it up into categories of walls, windows, floors, doors and so on. these categories should be displayed in a circle around the user, hinch spawning the game objects in a circle. the question i’m looking for was to see if anyone had a stepping stone on makking this work with gameobjects, so that i eventually could make my own script telling building objects by their individual codes to do almost the same.
i hope that clearefies some of my lack of knowledge in this case unfortunatly. But regardless, i do say thanks and respect the help i got.

That sounds like you need more of a circle menu to select a certain type? Look for “circle menu”, “radial menu” or “radial layout” in tutorials or the asset store for a ready made solution. And I still don’t know what you are asking for. My code snippet should spawn count instances of an object in a circular manner. Does it not work? What else do you need? You just describe what you want to achieve but NOT what is lacking or what you need help with. I find this somehow irritating.

You could also define an array in your class, put an object of each type (door, wall, window) into it and spawn one of them instead of the same object like in my example. But the question if wether you need this in 3d world space where the user may not see it depending on his position and orientation. Or you need it as a user interface where it is displayed no matter where the user looks.

The more precise information you give from the beginning the less time and effort people waste guessing what you need and writing stuff that you don’t need.