The question is answered, right answer was accepted
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 ! :)
Answer by xxmariofer · Feb 13, 2019 at 07:51 PM
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?
Answer by Elfrick · Feb 13, 2019 at 08:08 PM
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 :/
here it is :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Galaxy : $$anonymous$$onoBehaviour
{
public int nbreStars = 300;
public float DistBetweenStars;
public float maxWidthPosition;
public float $$anonymous$$WidthPosition;
public float maxHeightPosition;
public float $$anonymous$$HeightPosition;
// Start is called before the first frame update
void Start()
{
int NbreEchec = 0;
for (int i = 0 ; i < nbreStars ; i++)
{
Vector3 StarPosition = new Vector3(Random.Range(maxWidthPosition, $$anonymous$$WidthPosition), 0, Random.Range(maxHeightPosition, $$anonymous$$HeightPosition));
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;
}
}
}
}
hello, then the problem is with other part of your code probably i have tested it and worked fine. ill try uploading a gif. but here is the code,
private void Awake()
{
StartCoroutine(enumsss());
}
IEnumerator enumsss()
{
int counter = 0;
for (int i = 0; i < 10000; i++)
{
Vector3 pos = new Vector3(UnityEngine.Random.Range(maxWidthPosition, $$anonymous$$WidthPosition), 0, UnityEngine.Random.Range(maxHeightPosition, $$anonymous$$HeightPosition));
Collider[] sphereExclusion = Physics.OverlapSphere(pos, 4);
if (sphereExclusion.Length == 0)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.position = pos;
counter = 0;
}
else { counter++;
if(counter > 500)
{
Debug.Log("HAVE TESTED 50 TI$$anonymous$$ES WITHOUT LUC$$anonymous$$");
yield break;
}
}
yield return new WaitForEndOfFrame();
}
}
Follow this Question
Related Questions
Very confused... Physics.OverlapSphere working, but not NonAlloc version? 2 Answers
Physics.OverlapSphere not detecting collision! Collision help. 1 Answer
Find what size sphere collider can fit in a point 1 Answer
Configerable joint problem !! 0 Answers
How to do a Placement object properly? 0 Answers