Search Unity

Need help with Random Vector2 Positions and maintaining distances

Discussion in 'Scripting' started by Calm, Jan 7, 2015.

  1. Calm


    Feb 11, 2014
    I'm somewhat new to programming.
    I'm trying to write a recursive function in c# in Unity3d that will generate objects at random vector2 coordinates. I want to make sure that each object is at least at a distance of "x" magnitude from any other object.

    //Origin = Max Distance from Center of Screen
    private Vector2 RecursPos(float origin) {

    //Sets Initial Location
    this.x = Random.Range(-origin, origin);
    this.y = Random.Range(-origin, origin);

    Vector2 thisObjectPosition = new Vector2(this.x, this.y);

    //Run through object List<>
    for (int i = 0; i < ObjectList.Count; i++) {

    //Set Position of Object in
    Vector2 checkPos = new Vector2(ObjectList.x, ObjectList.y);

    //Check if Distance of thisObject and ObjectList < 2 then give new Location
    if (Vector2.Distance(checkPos, thisPos) < 2) {
    return RecursPos(origin);


    I know I'm missing something but I can't figure it out. I've been working on this for hours and at my wits end. Any help, even pointing me to a resource to learn how to do this would be greatly appreciated.

    Thank you!
  2. hpjohn


    Aug 14, 2012
    Vector2 checkPos = new Vector2(ObjectList.x, ObjectList.y);
    Should probably be
    Vector2 checkPos = new Vector2(ObjectList[ i ].x, ObjectList[ i ].y);
  3. Juice-Tin


    Jul 22, 2012
    There's really no reason to use recursive there. Recursive is very tricky and VERY rarely used. (Been programming games for about 10+ years and only ever had to use it once)

    Here's a much simpler way. It simply continues to randomize the point and check against the other objects as long as it's too close. When it's not close to any object, the function ends and returns the point it found.

    Code (CSharp):
    1. //Origin = Max Distance from Center of Screen
    2. private Vector2 GetPos(float origin) {
    3.    //do{  }while(); = continues to do a block of code while the while() condition is true
    4.    do {
    5.      //create random point
    6.      Vector2 thisPos = new Vector2(Random.Range(-origin, origin), Random.Range(-origin, origin));
    8.      //default to a success
    9.      bool success = true;
    11.      //loop through all objects
    12.      for (int i = 0; i < ObjectList.Count; i++) {
    13.        Vector2 checkPos = new Vector2(ObjectList[i].x, ObjectList[i].y);
    15.       //check the distance to each object
    16.        if (Vector2.Distance(checkPos, thisPos) < 2) {
    17.          //if it's too close to even just 1 object, success is false
    18.          success = false;
    19.        }
    20.      }
    21.    }while (!success);
    22.    //so while success if false, it will stay stuck in that loop, randomizing the position, and checking all objects
    24.     //when success stays true, then it can exit the loop and end the function.
    25.     return thisPos;
    26. }