- Home /
Need help checking distance between multiple objects,How do I check the distance between multiple objects at one time?
Hey! Im trying to create a random solar system and im having this issue with the planets spawning inside of eachother occasionally. Any help would be appreciated.
GameObject[] planetBodys;
float distBetweenPlanets;
public float minDist;
public PlanetCreator pCreator;
private void Start()
{
}
void Update()
{
planetBodys = GameObject.FindGameObjectsWithTag("PlanetBody");
foreach (GameObject planet in planetBodys)
{
distBetweenPlanets = Vector3.Distance(transform.position, planet.transform.position);
}
Debug.Log(distBetweenPlanets);
if(distBetweenPlanets != 0)
{
if(distBetweenPlanets < minDist)
{
transform.position = pCreator.GeneratedPosition();
Debug.Log("Moved!");
}
}
}
i tried this and also this
public SphereCollider trigger;
public Transform planet;
public PlanetCreator pCreator;
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "PlanetBody")
{
planet.transform.position = pCreator.GeneratedPosition();
print("Collision Detected!");
}
}
private void Update()
{
trigger.isTrigger = true;
trigger.radius = 500;
planet = transform.parent;
pCreator = planet.GetComponent<PlanetCreator>();
}
I couldn't get either to work.
,Hey! Im creating a procedural star system and I need some help figuring out how I can prevent these planet meshs from spawning inside of each other.
GameObject[] planetBodys;
float distBetweenPlanets;
public float minDist;
public PlanetCreator pCreator;
private void Start()
{
}
void Update()
{
planetBodys = GameObject.FindGameObjectsWithTag("PlanetBody");
foreach (GameObject planet in planetBodys)
{
distBetweenPlanets = Vector3.Distance(transform.position, planet.transform.position);
}
Debug.Log(distBetweenPlanets);
if(distBetweenPlanets != 0)
{
if(distBetweenPlanets < minDist)
{
transform.position = pCreator.GeneratedPosition();
Debug.Log("Moved!");
}
}
}
I tried to do this but this doesn't work. Any help or suggestions would be much apricated. I also tried to use a trigger sphere but i couldnt get that to work either
Answer by remagify · Dec 20, 2020 at 12:12 PM
Didn't mean to double post that, but here is an example of whats going on
Your answer
Follow this Question
Related Questions
Instantiated children wont update position 2 Answers
Space unit size 2 Answers
Level Up Particle Effect 1 Answer
Moon rotation problem. 2 Answers
Advice on my Ship AI... 4 Answers