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.

Just add the transform.position to your Instantiate():

  Instantiate(prefab, transform.position + pos, Quaternion.identity);

Sorry if I misunderstood you but try adding a position Vector (your middle point) to pos.

@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!