- Home /
OverlapSphere is really confusing
I have the following codes =>
GameObject g1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
g1.transform.position = new Vector3(2, 0, 0);
GameObject g2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
g2.transform.position = new Vector3(-2, 0, 0);
Collider[] col = Physics.OverlapSphere(Vector3.zero, 0.5f);
Debug.Log(col.Length);
GameObject g3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Now this code will output Debug.Log 2
. So my OverlapSphere is actually overlapping with g1 and g2.... But how is this possible ? It should not overlap...
Also, now in my exemple 2, I use some scale
[Header("Galaxy")]
public int numberOfStars = 300;
public int maximumRadius = 100;
public float minDistBetweenStars = 2f;
[Header("Star")]
public float minimumSize = 1f;
public float maximumSize = 2f;
void Start()
{
for(int i = 0; i < numberOfStars; i++)
{
float distance = Random.Range(0, maximumRadius);
float angle = Random.Range(0, 2 * Mathf.PI);
float starSize = Random.Range(minimumSize, maximumSize);
Vector3 starPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
Collider[] starCollider = Physics.OverlapSphere(starPosition, (starSize * 0.5f) + minDistBetweenStars);
if (starCollider.Length == 0)
{
GameObject star = GameObject.CreatePrimitive(PrimitiveType.Sphere);
star.transform.position = starPosition;
star.transform.localScale = new Vector3(starSize, starSize, starSize);
}
else
{
i--;
}
}
}
This Overlap, will trigger, but I will still have some overlapping sphere in many situations...
I am kind of confused by something that should be simple.
Comment
Your answer
Follow this Question
Related Questions
How is OverlapSphere used? 2 Answers
Physics Overlap Sphere issue 0 Answers
Making a sphere move and rotate like a real ball on ground 2 Answers
overlap half sphere? 0 Answers
Ball jump on collision problem 1 Answer