- Home /

# I need to instantiate an object inside a donut instead of a circle

I need to instantiate an object inside a donut instead of a circle.

(donut or a custom game object)

```
public Transform prefab;
public int instances = 5000;
public float radius = 50f;
void Start()
{
for (int i = 0; i < instances; i++)
{
Transform t = Instantiate(prefab);
t.localPosition = Random.insideUnitCircle * radius;
t.SetParent(transform);
}
}
```

Thansks!

[1]: /storage/temp/129288-question.png

**Answer** by Bunny83
·
Dec 11, 2018 at 11:37 PM

Just do a two step calculation. First you can use `Random.insideUnitCircle.normalized`

to get a normalized direction vector. This just determines the direction. Now we just need to scale the length of that vector with appropriate values. Though if you want a uniform distribution we can not just use a radius that is within your desired range because you would get a higher density at the center as you can see here. The trick is the use the square root of the unit radius. So what we need to do is calculate the thicknees of the ring, map our "square rooted" radius onto this value and add the inner radius. This should give us a uniform distribution inside the ring.

```
// given:
float innerRadius = 35;
float outerRadius = 50;
// calculated
float ratio = innerRadius / outerRadius;
float radius = Mathf.Sqrt(Random.Range(ratio*ratio, 1f)) * outerRadius;
Vector3 point = Random.insideUnitCircle.normalized * radius ;
```

If the ring radius is rather large and the ring thickness relatively small the difference in distribution usually doesn't matter much. In this case we could simply do:

```
Vector3 point = Random.insideUnitCircle.normalized * Random.Range(innerRadius, outerRadius);
```

Though as i mentioned you get a slightly higher density at the inner radius and the density gets lower as you go to the outer radius.

*edit*

I just realised that my initiali approach may not work properly. I've just changed the proper solution to get the right distribution.

Also note that using `Random.insideUnitCircle.normalized`

to get a random normalized direction vector is just the quick and dirty solution. It does work fine, though we usually would simply calculate a random angle and use Sin and Cos to calculate a random vector:

```
float angle = Random.Range(0, 2f * Math.PI);
var dir = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
```

Now we can use `dir`

instead of `Random.insideUnitCircle.normalized`

Very useful!!

I use your code like that:

//code

```
public Transform prefab;
public int instances = 5000;
public float innerRadius = 35;
public float outerRadius = 50;
void Start()
{
for (int i = 0; i < instances; i++)
{
Vector3 point = Random.insideUnitCircle.normalized * Random.Range(innerRadius, outerRadius);
Instantiate(prefab, point, Quaternion.identity);
}
}
```

buttt.. how I can change the direcction? I need to rotate 90ª but this code does not allow it

Thanks!!!

[1]: /storage/temp/129295-donut.png

Well you presented your problem as a 2d problem. If you need a different plane, just do a "component re-mapping". We calculate a point in the normal x-y plane. If you need a point in the x-z plane, just put the y value in z.

If you don't know the size of your two radii yet i would recommend to use my first method. The simplified version could be used when the two radii are very close to each other. So for example outer=50 inner=40. However if you have a 50 : 25 ratio (so a factor of about 2) you may notice the different distribution. The spawns would be more likely to be closer to the inner radius than to the outer.

```
Instantiate(prefab, new Vector3(point.x, 0, point.y), Quaternion.identity);
```

ohh, I forgot to specify it was in 3D

okey, I have understood the problem of distribution. I think that for this case I can simplify it, just like you say.

thank you so much!!

**Answer** by Cornelis-de-Jager
·
Dec 11, 2018 at 11:29 PM

You will need to calculate the spawn direction first then the actual distance from the center. See my implementation below:

```
public Transform prefab;
public int instances = 5000;
public float radiusMin = 20f;
public float radiusMax = 50f;
void Start()
{
for (int i = 0; i < instances; i++)
{
var x = Random.Range(-1f, 1f);
var y = Random.Range(-1f, 1f);
var direction = new Vector3(x, 0f, y);
var radius = Random.Range( radiusMin, radiusMax);
Transform t = Instantiate(prefab);
t.localPosition = direction * radius;
t.SetParent(transform);
}
}
```

I get this error on "var direction = Vector3(x, y, 0f);" ->

"Error CS1955: Non-invocable member 'Vector3' cannot be used like a method. (CS1955) (Assembly-CSharp)"

sorry it needs a new tag. I've updated the code

Note that this will not result in a circular shape:

```
var x = Random.Range(-1f, 1f);
var y = Random.Range(-1f, 1f);
var direction = Vector3(x, y, 0f);
```

This is a square. Even when you normalize your direction you get heavy "banding" in the 4 diagonal directions since when normalizing the vector you squish the initial uniform distribution within the square into a circle. So the density of the 4 corners get pushed inwards and increase the density in those areas.

Apart from that, as i mentioned in my answer, using this radius calculation also doesn't produce a uniform distribution radially but instead you get a higher density at the center and a lower density at the outer perimeter.

Its not meant to be a circle. Its simply to get direction.

But you scale your direction with your radius. And your direction vector is a point within a square. If you scale this vector the resulting area will still be a square. Normalizing would fix this, however as i said you get a non uniform distribution.

It's works like the code of @Bunny83 but the same thing happens

it's fine but I want to change the angle 90º.

(but I want to change the angle)

No, this code can not produce a circular pattern in it's current form. Not without normalizing the direction.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.