Problem with the position in OverlapSphere

Hi everyone,

I want to place spheres but to avoid having them overlap, I’m trying to use : Physics.OverlapSphere.
I’m using a for loop to generate a new position each time and if it detects a collision, it tries again.
My problem is that it seems that the OverlapSphere does not update with the new position. By using a huge radius of exclusion, the problem becomes very visual.

Here is my code :

using System.Collections;
using UnityEngine;

public class Galaxy : MonoBehaviour
{
    public int nbreStars = 300;
    public int Rmax = 100;
    public float DistBetweenStars;

    // Start is called before the first frame update
    void Start()
    {
        int NbreEchec = 0;

        for (int i = 0 ; i < nbreStars ; i++)
        {
            float distance = Random.Range (0, Rmax);         
            float angle = Random.Range(0, 2 * Mathf.PI);       

            Vector3 StarPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle)); 

            Collider[] sphereExclusion = Physics.OverlapSphere(StarPosition, DistBetweenStars); 
                                                                                  

            if (sphereExclusion.Length == 0) 
            {
                GameObject StarGO = GameObject.CreatePrimitive(PrimitiveType.Sphere); 
                StarGO.transform.position = StarPosition;                
                NbreEchec = 0;
            }
            else  
            {
                NbreEchec++;
                i--;       
            }

            if (NbreEchec > nbreStars)                                          
                                        
            {
                Debug.LogError("Impossible de créer l'étoile, pas assez d'espace ou espace entre les étoiles trop grand !");
                break;
            }

            
        }
    }
}

The Debug.LogError says “Can’t create star, not enough space or space between stars to big”
NbreEchec is the number of failing to place the star.
NbreStar is the number of stars to place and Rmax is the radius.

So, what do you think I’m doing wrong ? To me, the Physics.OverlapSphere should take a new position for each loop but it seems it doesn’t and I don’t understand why…

Thank you in advance for your help ! :slight_smile:

hello, can you test this.

first create 4 floats that will be the max position that the starts will be appearing, since i imagine your game inst infinite

public float maxWidthPosition;
public float minWidthPosition;

public float maxHeightPosition;
public float minHeightPosition;

and change the code inside the for untill you create the overlapcollider to this

Vector3 StarPosition = new Vector3(Random.Range(maxWidthPosition, minWidthPosition), 0, Random.Range(maxHeightPosition, minHeightPosition));

Collider[] sphereExclusion = Physics.OverlapSphere(StarPosition, DistBetweenStars);

Also the way you are checking the if there is enough space for creating the stars is weird too. is there any reason you want to try creating the stars the exact same number of times as the spawn creation failed?

Thank you for your answer @xxmariofer ,
It did not fix the issue, with your 4 floats, it created a square space for the stars to be placed.

There is no specific reason for trying as many time to place a star as the number of stars. I just set up an arbitrary number to stop an infinite “for” loop if the code doesn’t find space to place a star.

I don’t know why the OverlapSphere ignores the “for” loop and doesn’t update to the new position :confused: