- Home /
Procedurally generated (Small)Universe
Hi, I am working on an rpg space based game and I want to procedurally generate the unverse for reasons of efficiency. Here is my script so far it's pretty basic but is working kind of... the issue I am having is when it randomly spawns a star it is ment to spawn a random amount of planets like a solar system. but when I run game everything spawns ok except the planets spawn from the point of the empty gameobject the script is attached to (as a child of it's "host Star") I have tried running 2 different scripts one to spawn the stars and then each star with a script to spawn the planets but both ways run into the same problem..... public GameObject Seed; public int SpacingMax = 50; public int SpacingMin = 25; public int ScaleMax = 10; public int ScaleMin = 5;
void Start ()
{ int size = Random.Range(5, 10); float[,,] SpaceTime; SpaceTime = new float[size, size, size];
for (int a = 0; a < SpaceTime.GetLength(0); a++)
{
for (int b = 0; b < SpaceTime.GetLength(0);b++)
{
for (int c = 0; c < SpaceTime.GetLength(0); c++)
{
SpaceTime[a, b, c] = Random.Range(0, SpacingMax);
}
}
}
for (int a = 0; a < SpaceTime.GetLength(0); a++)
{
for (int b = 0; b < SpaceTime.GetLength(0); b++)
{
for (int c = 0; c < SpaceTime.GetLength(0); c++)
{
if (SpaceTime[a, b, c] >= SpacingMin)
{
int s1 = Random.Range(ScaleMin, ScaleMax);
int s2 = Random.Range(ScaleMin, ScaleMax);
int Scale = (s1 + s2) / 2;
int space = Random.Range(SpacingMin, SpacingMax);
GameObject UniVerse = (GameObject)Instantiate(Seed, new Vector3(a * space, b * space, c * space), Quaternion.identity);
UniVerse.transform.localScale = new Vector3(Scale, Scale, Scale);
UniVerse.transform.parent = this.transform;
}
}
}
}
}
}
what I would like to know is.. am I on the right track? if so anymore direction would be handy and if not what would be a better way to do this?
Answer by remption · Jul 10, 2016 at 09:43 PM
I'm thinking this has to do with the the fact that you're setting the planet's position in global space when you instantiate at line 23 (under assumption this is the planet you're instantiating - if not, the principal remains the same). Instead, when you make a new planet, you should:
1) set its transform's parent to the star it belongs to
2A) set its transform.localPosition
to that vector you're using in line 23 new Vector3(a * space, b * space, c * space)
.
Local space for a transform means "relative to parent's transform". So, if your parent star is rotated or scaled, they will affect the planet's positioning when you use transform.localPosition
to set it. So, alternatively to 2A for each planet, you can (and probably should) use:
2B) this.transform.position = this.transform.parent.position + new Vector3(a * space, b * space, c * space);
Thank you so much. Have resolved my problems by removing the transfom.position out of the main spawning script. and replaced it with a scrpt attached to the planet seeds int a = Random.Range(-$$anonymous$$Space, maxSpace); int b = Random.Range(-$$anonymous$$Space, maxSpace); int c = Random.Range(-$$anonymous$$Space, maxSpace);
this.transform.position = this.transform.parent.position + new Vector3(a,b,c);
and works great still needs some tweaking but it works. Cheers