Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Setting distance between two vectors of spawning objects - 2D

Discussion in 'Scripting' started by Rotzak, Jul 24, 2018.

  1. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    Hello boys and girls,

    I have an issue scripting with c#.

    This is a part of my script:

    Vector3 carPosOne = new Vector3(Random.Range(minPos, maxPos), transform.position.y, transform.position.z);

    Vector3 carPosTwo = new Vector3(Random.Range(minPos, maxPos), transform.position.y, transform.position.z);

    dist = Vector3.Distance(carPosOne, carPosTwo);


    I'm spawning two objects and I want to set a minimum distance between those two objects. I know how to get this distance by vector3.Distance(carPosOne, carPosTwo) but how can I adjust this so the minimum distance is 2?

    Thanks.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If dist is less than 2 you can just generate another random position and check again. Though technically this has an infinitesimally small chance of being an infinite loop. The code updated below will have the most random feel out of the 3 I'm posting.

    Code (csharp):
    1. Vector3 carPosOne = new Vector3(Random.Range(minPos, maxPos), transform.position.y, transform.position.z);
    2.  
    3. dist = 0f;
    4. while (dist < 2f)
    5. {
    6.     Vector3 carPosTwo = new Vector3(Random.Range(minPos, maxPos), transform.position.y, transform.position.z);
    7.  
    8.     dist = Vector3.Distance(carPosOne, carPosTwo);
    9. }
    You can also choose values for minPos and maxPos that don't overlap between the two results, and are at least 2 apart. This code below will basically segregate each position so one car always starts on one side, and the other car always starts on the other side. So really just their total distance will feel random.

    Code (csharp):
    1. Vector3 carPosOne = new Vector3(Random.Range(1000f, 2000f), transform.position.y, transform.position.z);
    2.  
    3. Vector3 carPosTwo = new Vector3(Random.Range(1f, 998f), transform.position.y, transform.position.z);
    4.  
    5. dist = Vector3.Distance(carPosOne, carPosTwo);
    6.  
    Or you could use values for the 2nd Random.Range based on the results from the first one. This code will have a reduced likelihood of both cars being positioned on one of the two extremes of the minPos/maxPos inputs than in the original random positions, as it will favor straddling the mid point between minPos and maxPos.

    Code (csharp):
    1.  
    2. Vector3 carPosOne = new Vector3(Random.Range(minPos, maxPos), transform.position.y, transform.position.z);
    3.  
    4. float secondMin = minPos;
    5. float secondMax = maxPos;
    6. float halfway = ((maxPos - minPos) / 2) + minPos;
    7. if (carPosOne.x > halfway)
    8. {
    9.     secondMax = carPosOne.x - 2f;
    10. }
    11. else
    12. {
    13.     secondMin = carPosOne.x + 2f;
    14. }
    15.  
    16.  Vector3 carPosTwo = new Vector3(Random.Range(secondMin, secondMax), transform.position.y, transform.position.z);
    17.  
    18. dist = Vector3.Distance(carPosOne, carPosTwo);
    You can certainly tweak any of these ideas, or come up with your own along these lines, to get your desired results. (note that I haven't tested any of this code, just wrote it directly into the forums off the top of my head so apologies if there are typos - there's also probably better ways of doing this if thinking about it for more than the 10 minutes I spent).
     
    Last edited: Jul 24, 2018
    Rotzak likes this.
  3. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    Thank you very much. Will try it tommorow.
     
  4. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    Thanks man, I've used your third method. Nice thinking!

    Code (CSharp):
    1.  if (carTimer <= 0)
    2.         {
    3. Vector3 carPosOne = new Vector3(Random.Range(-maxPos, maxPos), transform.position.y, transform.position.z);
    4.  
    5.             float secondMin = -maxPos;
    6.             float secondMax = maxPos;
    7.             float halfway = 0;
    8.  
    9.             if (carPosOne.x > halfway)
    10.             {
    11.                 secondMax = carPosOne.x - 2f;
    12.             }
    13.  
    14.             else
    15.             {
    16.                 secondMin = carPosOne.x + 2f;
    17.             }
    18.  
    19. Vector3 carPosTwo = new Vector3(Random.Range(secondMin, secondMax), transform.position.y, transform.position.z);
    20.  
    21.             dist = Vector3.Distance(carPosOne, carPosTwo);
    22.  
    23.             Debug.Log(dist);
    24.  
    25.             carNo = Random.Range(0, 7);
    26.  
    27.             Instantiate(cars[carNo], carPosOne, transform.rotation);
    28.  
    29.             Instantiate(cars[carNo], carPosTwo, transform.rotation);
    30.  
    31.             carTimer = delayCarTimer;