- Home /
Instantiating Prefabs around a point other than zero to create a circle
Hey guys,
This is my first post here and I'm new to Unity + programming in general. I'm learning c# and so have been heavily relying on the manual.
I need to place a number of prefabs around a point and have so far used this from the manual:
void Start() {
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * Mathf.PI * 2 / numberOfObjects;
prefab.transform.position = transform.position;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; // original line
Instantiate(prefab , pos, Quaternion.identity);
}
}
Now this works great and places the objects in a neat circle as expected, but, I'm trying to place them around the transform.position of the class that's instantiating them; the idea is that once placed I'll use RotateAround to rotate them around transform.position to form a shield that you can shoot out.
Any ideas? I'm totally lost and my math skills are non-existent.
Thanks in advance.
Answer by robertbu · Aug 20, 2014 at 08:11 PM
Just add the transform.position to your Instantiate():
Instantiate(prefab, transform.position + pos, Quaternion.identity);
Thanks for the replies. I tried to directly add the transform but it placed them very far way. This however fixed it.
void Start() {
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * $$anonymous$$athf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3($$anonymous$$athf.Cos(angle), 0, $$anonymous$$athf.Sin(angle)) * radius; // original line
currentPos = transform.position;
desiredPos = new Vector3 (currentPos.x + pos.x, currentPos.y + pos.y, currentPos.z + pos.z);
prefab.transform.position = desiredPos;
Instantiate(prefab , desiredPos, Quaternion.identity);
}
}
Thanks again guys!
Now to figure out how to add them as children :-s
Instantiate returns a reference to the object you just spawned. Grab its transform and set a parent for it.
I don't know what type prefab
is, but every GameObject and component knows its associated transform:
//assume it's a GameObject for now?
//if it's some component, just fix the types in this example
GameObject clone = (GameObject)Instantiate(prefab, desiredPos, Quaternion.identity);
clone.transform.parent = someOtherTransform;
Answer by stulleman · Aug 20, 2014 at 08:14 PM
Sorry if I misunderstood you but try adding a position Vector (your middle point) to pos.
Answer by ImTheC · Sep 07, 2016 at 06:08 AM
@Maniak101, I know this was a long time ago, but I was having difficulty figuring this out myself. What I ended up doing was this:
// Instantiates a prefab in a circle around a specific point (shieldGen)
public GameObject prefab;
public int numberOfObjects;
public Transform shieldGen;
public float radius = 5f;
void Start() {
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
Instantiate(prefab, shieldGen.position + pos, Quaternion.identity);
}
}
Then, in the Inspector, I attached an empty gameobject to the "shieldGen" wherever I wanted the objects to spawn. In my case, it was an empty gameobject attached to the player gameobject.
Hope this helps anybody else searching for this issue!
Your answer
![](https://koobas.hobune.stream/wayback/20220613161108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving the camera with mouse not working 1 Answer
Why does my sphere object stop moving on collision? 1 Answer
How do I display text in current Unity in the first roll-a-ball project? 1 Answer
Incomprehensive auto death on 2D runner with energy depleting mechanics [Debugging] 1 Answer
Downsize A 3DS Model? 1 Answer