Skip to content

Instantly share code, notes, and snippets.

@Ashwinning
Last active November 3, 2016 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ashwinning/89fa09b3aa3de4fd72c946a874b77658 to your computer and use it in GitHub Desktop.
Save Ashwinning/89fa09b3aa3de4fd72c946a874b77658 to your computer and use it in GitHub Desktop.
Getting a random point between two concentric circles in Unity3D #gistblog #unity3d #c#

Getting a random point between two concentric circles in Unity3D

/// <summary>
/// Returns a random point in the space between two concentric circles.
/// </summary>
/// <param name="minRadius"></param>
/// <param name="maxRadius"></param>
/// <returns></returns>
Vector3 GetRandomPointBetweenTwoCircles(float minRadius, float maxRadius)
{
    //Get a point on a unit circle (radius = 1) by normalizing a random point inside unit circle.
    Vector3 randomUnitPoint = Random.insideUnitCircle.normalized;
    //Now get a random point between the corresponding points on both the circles
    return GetRandomVector3Between(randomUnitPoint * minRadius, randomUnitPoint * maxRadius);
}

/// <summary>
/// Returns a random vector3 between min and max. (Inclusive)
/// </summary>
/// <returns>The <see cref="UnityEngine.Vector3"/>.</returns>
/// <param name="min">Minimum.</param>
/// <param name="max">Max.</param>
Vector3 GetRandomVector3Between(Vector3 min, Vector3 max)
{
    return min + Random.Range(0, 1) * (max - min);
}

GetRandomVector3Between taken from https://gist.github.com/Ashwinning/269f79bef5b1d6ee1f83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment