So I made a test game for whenever I push a button, it create a class object from a factory class. I want to make it so everytime I spawn an object, it keeps from overlapping other objects. Also, I want to keep the sphere and light object together everytime I spawn. Can you help me figure out how to do that?
public class Sphere : Shape
{
public Sphere()
{
float speed = 10f;
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(0, 0, 0);
sphere.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
sphere.transform.Rotate(Vector3.up, speed * Time.deltaTime);
Destroy(sphere, 30.0f);
GameObject lightGameObject = new GameObject("Light");
Light lightComp = lightGameObject.AddComponent<Light>();
lightComp.color = Random.ColorHSV();
lightGameObject.transform.position = new Vector3(0, 0, 0);
lightGameObject.transform.Rotate(Vector3.up, speed * Time.deltaTime);
Destroy(lightComp, 30.0f);
}
}
Are these primitive objects moving around while you’re spawning new ones? If so, that’s not really a trivial task. Unless you have some specific behaviour in how they move (i.e. move along a certain axis at a specific velocity), but worst case scenario, you could brute force it by picking a position, take the radius from any nearby object(s), and move away from those objects by that radius amount.
If they’re stationary… well that’s easy. You know the radius of each sphere, so simply offset from the previous position by that (sphere1.radius + sphere2.radius) amount.
As for keeping the light and sphere together… why not just add the light to the sphere as a child?
I want to keep the light primitive too. How would I make that a child? Yes. Its stationary, and a new primitive sphere is made eveytime I click the button. But its a primitve sphere, so it doesn’t have a name. How would I make it so I can click the first time, give the first sphere a position, but click again and give the second sphere another position and so on?
public class Sphere : Shape
{
public Sphere()
{
float speed = 10f;
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(0, 0, 0);
sphere.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
sphere.transform.Rotate(Vector3.up, speed * Time.deltaTime);
Destroy(sphere, 30.0f);
GameObject lightGameObject = new GameObject("Light");
Light.transform.SetParent(sphere); //this sets the light as a subobject of the sphere, to keep them together
Light lightComp = lightGameObject.AddComponent<Light>();
lightComp.color = Random.ColorHSV();
lightGameObject.transform.position = new Vector3(0, 0, 0);
lightGameObject.transform.Rotate(Vector3.up, speed * Time.deltaTime);
//Destroy(lightComp, 30.0f); //not needed the sphere will destroy it
}
}
To stop them from spawning on top of each other.
you can create a List of positions to spawn them at. then remove the position from the list when used.
Or you could do a check to see if there is already a sphere at the location before setting the position.