# Instantiate prefabs in a circle or elipsoid

Hey again!

I have a query. I'd like to instantiate objects in a for loop, but have them placed so they appear to be in a circle or ellipsoid, preferably ellipsoid.

I know it has something to do with maths, kind of wishing I didn't drop out, but it should be easy for someone to solve!

I am using the code snippet bellow to give an idea

``````    for (i = 0; i < max; i++)
{
var newbead : GameObject = Instantiate (beadPrefab, Vector3(X  , Y, 32 ), Quaternion.identity);}

``````

So, what formulae would be used to calculate the values X and Y ?

~ Nathan

Here you go. The comments explain most of it, although you may want to read up on radians. (They are just another way of measuring angles, as opposed to degrees).

I've also renamed a few vars for clarity.

``````var numPoints = 20;
var centrePos = Vector3(0,0,32);

for (var pointNum = 0; pointNum < numPoints; pointNum++)
{

// "i" now represents the progress around the circle from 0-1
// we multiply by 1.0 to ensure we get a fraction as a result.
var i = (pointNum * 1.0) / numPoints;

// get the angle for this step (in radians, not degrees)
var angle = i * Mathf.PI * 2;

// the X & Y position for this angle are calculated using Sin & Cos
var x = Mathf.Sin(angle) * radiusX;
var y = Mathf.Cos(angle) * radiusY;

var pos = Vector3(x, y, 0) + centrePos;

// no need to assign the instance to a variable unless you're using it afterwards:

}

``````

Since you mentioned a circle, I'm going to assume you meant 'ellipse' rather than 'ellipsoid'.

Given a center point (Cx, Cy), an angle theta, and two radii, radiusX and radiusY, you can compute a point on the ellipse as follows:

``````x = Cx + cos(theta) * radiusX;
y = Cy + sin(theta) * radiusY;

``````

To generate evenly distributed points on an ellipse, iterate over the range [0, 2pi) using a fixed step corresponding to how many points you want (e.g. if you want 20 points, the step will be 2pi/20).

This will create the points in the XY plane. If you need them to be in another cardinal plane or to be arbitrarily oriented, you can apply an appropriate transform to the points after they're generated. (In this case you can skip Cx and Cy and just incorporate the translation into the transform itself.)