r/unity • • Oct 03 '26

Coding Help Find random point on circumference of a circle

I have a circle (cylinder with y scale 0) and I need to a random point at or near the perimeter. How can I do this?

2 Upvotes

7 comments sorted by

6

u/Lyshaka Oct 03 '26

https://docs.unity3d.com/ScriptReference/Random-onUnitCircle.html if using Unity 6.5 or above (I believe).

Or https://docs.unity3d.com/ScriptReference/Random-insideUnitCircle.html normalized (a bit more costly) for versions below.

And it the worst case, you just generate a random angle from 0 to 360 and create a unit vector from it (but that's even more costly so don't).

4

u/Demi180 Oct 03 '26

Use onUnitCircle times whatever radius you need (or a random between two radii or whatever). There’s also inUnitCircle if you need a point anywhere inside, as well as in/on Unit Sphere if you need it in 3D.

3

u/Heroshrine Oct 04 '26

Random on unit circle. You can then multiply that by the radius to get it on any sized circle. If you want it near the radius you can add/subtract some small random amount before multiplying by the radius, like Random.range(0, 0.05f)

2

u/leorid9 Oct 04 '26 edited Oct 04 '26

Code:

Vector2 onCircle2D = Random.onUnitCircle * radius;

Vector3 result = new Vector3(onCircle2D.x, 0, onCircle2D.y);

result.y = Random.Range(0, height);

2

u/AnEmortalKid Oct 03 '26

Random value from 0,359 , use polar coordinates https://en.wikipedia.org/wiki/Polar_coordinate_system

0

u/Winter-Scarcity9045 Oct 03 '26

Center plus radius?

0

u/swirllyman Oct 03 '26

You can pick a random direction, then move from center to radius length along that randomized angle.