The question is answered, right answer was accepted
OverlapSphere won't accept a variable as position (stays instead at 0,0,0?)
Hi everyone,
i have the same problem as @Elfrick here: https://answers.unity.com/questions/1601488/problem-with-the-position-in-overlapsphere.html
The fix he was told uses cartesian coordinates, but i like to retain the orbital arrangement and want to know what I did wrong.
I tried many things, but i cant get the Collider to accept the variable, it seems to use (0,0,0) instead of the new coordinates. It should be impossible to have a sphere in the middle of the Collider, too, but there it is. The code should check the space around each sphere and prevent other spheres to be created near another sphere.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Galaxy : MonoBehaviour
{
// TODO: Have these values import from user settings
public int numberOfStars = 300;
public int maximumRadius = 100;
public float minDistBetweenStars;
// Start is called before the first frame update
void Start()
{
int failCount = 0;
for (int i = 0; i < numberOfStars; i++)
{
float distance = Random.Range(0, maximumRadius);
float angle = Random.Range(0, 2 * Mathf.PI);
var startPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
// Vector3 startPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
Collider[] positionCollider = Physics.OverlapSphere(startPosition, minDistBetweenStars);
if (positionCollider.Length == 0)
{
GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = startPosition;
failCount = 0;
}
else
{
i--;
failCount++;
}
if (failCount > numberOfStars)
{
Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars is too big!");
break;
}
}
}
// Update is called once per frame
void Update()
{
}
}
After changing the code to the one in the comments, i get this picture: 
Answer by IMinoI · Mar 19, 2020 at 11:14 AM
@xxmariofer, can you reupload the gif? The code you posted has the same problem, as there will be a circular void around the origin, not around othere spheres.
Interestingly, if you set the position of the OverlapSphere at the origin (0,0,0), other than the first object, you can't generate other spheres, even if you set the Radius of OverlapSphere to 0. I believe it has something to do with GameObject.CreatePrimitive(PrimitiveType.Sphere), which will be created at the origin, too. But i can't seem to find away to create it elsewhere, just transform.position it to somewhere else after the creation at the origin.
The situation with Overlapsphere does not make any sense at all. Even setting a static position like (10,0,10) as the position of the OverlapSphere with Vector3 will generate a void around the origin.
i didnt have the gif anymore so i have just created an empty project, create an script and pasted the code, worked without issues, maxwidth $$anonymous$$width... were max 100 and $$anonymous$$ 0
https://drive.google.com/open?id=1oVTcD9-qpeomj3aFuFPVnW5ZZkUocB7b
USE THE CODE I POSTED IN THE CO$$anonymous$$$$anonymous$$ENT NOT THE ONE HE POSTED WITH $$anonymous$$Y FIXES
Thanks for the answer @xxmariofer ! I tried your script you posted in your comment, but raised the radius of the OverlapSphere to 60 and get the same problem as before. I posted a picture of it in the main post up above (overlapsphereb.jpg). The code is: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Galaxy : $$anonymous$$onoBehaviour {
void Start(){
int counter = 0;
for (int i = 0; i < 10000; i++)
{
Vector3 pos = new Vector3(UnityEngine.Random.Range(100, 0), 0, UnityEngine.Random.Range(100, 0));
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("Test");
yield break;
}
}
}
}
// void Update()
// {
// }
}
But thats not the code I posted, check the last comments. Copy the exact code and little by little start doing changes but start with the coroutine (ienumerstor)