there's random onUnitSphere, but it doesn't work as random.onUnitCircle if it's being reduced from Vector3 to Vector2.
what are there other ways of choosing a random point on a circle.(not inside it, not outside, just on the very edge)
there's random onUnitSphere, but it doesn't work as random.onUnitCircle if it's being reduced from Vector3 to Vector2.
what are there other ways of choosing a random point on a circle.(not inside it, not outside, just on the very edge)
THE EASIEST WAY IS THIS:
Random.insideUnitCircle.normalized;
Just random inside unit circle first, and then normalized it. This will convert the vector to be 1 in magnitude, which is the same as random on unit circle. Hope this helps!
Similar to Peter’s answer, you could get a random angle and pass that to a custom function that accepts an angle and radius.
It would be akin to something like:
Vector2 GetUnitOnCircle(float angleDegrees, float radius) {
// initialize calculation variables
float _x = 0;
float _y = 0;
float angleRadians = 0;
Vector2 _returnVector;
// convert degrees to radians
angleRadians = angleDegrees * Mathf.PI / 180.0f;
// get the 2D dimensional coordinates
_x = radius * Mathf.Cos(angleRadians);
_y = radius * Mathf.Sin(angleRadians);
// derive the 2D vector
_returnVector = new Vector2(_x, _y);
// return the vector info
return _returnVector;
}
You could get a random value between `0` and `2pi` radians then find the sin and cos of that and make them your values in a `Vector2`.
Another way:
Use Unity methods to get a random point.
Normalize the vector to be one unit long.
Multiply by radius to get desired length.
public static Vector2 RandomOnUnitCircle2( float radius)
{
Vector2 randomPointOnCircle = Random.insideUnitCircle;
randomPointOnCircle.Normalize();
randomPointOnCircle *= radius;
return randomPointOnCircle;
}
public static Vector3 RandomOnUnitSphere( float radius)
{
Vector3 randomPointOnCircle = Random.insideUnitSphere;
randomPointOnCircle.Normalize();
randomPointOnCircle *= radius;
return randomPointOnCircle;
}
I was surprised to see so much resistance to trig here. As game programmers, we should either strive to make our code optimal (not use a bunch of high-level calls that bring in extra overhead) as well as improve what we know as far as not just programming, but mathematics. Peter G deserves the answer to this thread, as he provided the answer. I can’t say whether or not it is the absolute best option, but it is most certainly hard to think of something that can compete. Using Peter G’s suggestion, I was able to produce this:
float randomAngle = Random.Range(0f, Mathf.PI * 2f);
Vector2 randomPointAround2DCircumference = new Vector2(Mathf.Sin(randomAngle), Mathf.Cos(randomAngle)).normalized;
Wrapping this in a function should now be fairly easy:
public Vector2 GetPointOnUnitCircleCircumference()
{
float randomAngle = Random.Range(0f, Mathf.PI * 2f);
return new Vector2(Mathf.Sin(randomAngle), Mathf.Cos(randomAngle)).normalized;
}
I have tested this in Unity and I am seeing the intended results. Enjoy!
I must say I got scared away when I saw the answer about sinuses and cosinues and "pi" and radians, and converting them to vectors
so I just did something low-tech, but it works. for reference:
var circleCentre = Vector3.zero;
var circleDirection = Vector3.zero;
var ray = new Ray(circleCentre, circleDirection);
function Update () {
if(Input.GetKeyDown(KeyCode.Return))
{
ray.direction = Vector3(Random.Range(-1.0,1.0), Random.Range(-1.0,1.0), 0);
//and just to have some visual clue:
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = ray.GetPoint(2);
cube.transform.localScale = Vector3(.1,.1,.1);
}
}
Thank you for those high-tech answers though. I hope I'll be able to work on such hardcore mathematical stuff soon enough.