Search Unity

No Random.onUnitCircle?

Discussion in 'Scripting' started by CodeMonke234, Jul 1, 2015.

  1. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    There is
    insideUnitCircle Returns a random point inside a circle with radius 1 (Read Only).
    insideUnitSphere Returns a random point inside a sphere with radius 1 (Read Only).
    onUnitSphere Returns a random point on the surface of a sphere with radius 1 (Read Only).

    what happened to OnUnitCircle?

    Did someone just forget to put it in?
     
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    ...
    float force = 600.0f;
    float angle = Random.Range(0, 2f * Mathf.PI);
    float x = Mathf.Cos(angle) * force;
    float y = Mathf.Sin(angle) * force;
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Vector2 onUnitCircle = Random.insideUnitCircle.normalized;
     
    Bunny83, Kiwasi and CodeMonke234 like this.
  4. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    nice!
     
  5. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    Elegant ;-)
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    What if insideUnityCircle returns zero?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    ... then normalizing it should return zero as per the docs.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    I've learned things since 2015 :p
    A better OnUnitCircle is just:

    Code (csharp):
    1. var angle = Random.value * 2 * Mathf.PI;
    2. return new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
    I assume that the way InsideUnitCircle is made is just that, but multiplied by Random.value.

    Also OP hinted at that being the solution, and I was just too daft to catch it 6 years ago? idk, it's been a while.
     
    mopthrow likes this.
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I mean, then the result won't be "OnUnitCircle".
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    True but you didn't ask about that, you asked...

     
  11. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    That was a hint that onUnitCircle = insideUnitCircle.normalized is not a correct solution :)
     
  12. aksiluh

    aksiluh

    Joined:
    Feb 15, 2021
    Posts:
    1
    As a sidenote, you actually have to multiply by Mathf.Sqrt(Random.value) to get evenly distributed positions. Rejection sampling (pick random values unless get position inside circle) is bit faster on average but they probably don't use it because it will not be that stable in terms of performance.
     
    Bunny83 likes this.