- Home /
Another Random Generation Problem
Hello.
I have yet another problem connected to my random generator. Well, technically two generators. You see, my generation scripts work like this: First one creates random star constellation in which every star takes you to a "system level" after clicking it.
Second one creates random star system via this code (LiczbaPlanet means number of planets):
using UnityEngine;
using System.Collections;
public class Generatorv2 : MonoBehaviour {
public Transform Sun;
public GameObject prefab;
private float currDist = 20f;
private float minDist = 40f; // Minimum distance from the previous planet
private float maxDist = 50f;
private float RandomFloat;
private float LiczbaPlanet;
void Start() {
LiczbaPlanet = Random.Range(1,9);
PlacePlanets();
}
void PlacePlanets() {
for (float i = 0f; i < LiczbaPlanet; i++) {
currDist = currDist + Random.Range(minDist, maxDist);
Vector3 v3 = Vector3.right * currDist;
v3 = Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up) * v3;
v3 += Sun.position;
Instantiate(prefab,new Vector3(v3.x, 0, v3.z), Quaternion.Euler(0,0,0));
}
}
void Update(){
}
}
Here's an example of randlomly generated system:
Now to the problem/question:
Is there any way in which i could have particular stars (in the star contellation) assigned to one randomly generated star system, so it would work like this:
I click a star and the code generates a random star system, and when i close it and click again on the same star i would have same star system.
I appreciate any kind of help/answar.
-Xentarok
P.S. - My english isn't very good, so forgive me my mistakes.
P.S.2 - If you have any questions about my question or you want more specific information, ask for it in the comment section.
[2]: http://answers.unity3d.com/questions/467090/random-generation-problem.html
Sorry, I'm too lazy to write out code, but the logic for this would be as follows:
Have each star have a boolean, that would be initially set to false, but once planets for it are generated, mark it to true.
Once generated, you should store each planet in an array of planets, and have that array in the star.
When you click on a star, make it check the boolean. If its false, generate random planets (and save them). If its true, cycle through the array and read all the info about each planet and place them accordingly.
Answer by robertbu · Jun 26, 2013 at 04:47 PM
The typical way to solve this problem is to use a random seed. For a given seed, random number generators will generate the same sequence of numbers. So to regenerate your planets, you reset the seed before you call the regenerate code. For each sun in your constellation, you assign or generate an integer. For example, you could multiply the x,y and z components of the position together and convert it to an integer. Or do x * 100 + y + 10 + z. You then set this integer to the seed before you generate the planets.
Note that .NET also has a random class. The .NET class allows you to create multiple class instances. I'm not sure where you are going with this project, but the .NET class is an option you might want to be aware of.
Yes, this would also work, if you're not planning on ever making some kind of changes to your planets or otherwise storing data on them, they can be re-generated each time.
Well, I'm making a strategy game, so what if I'd like to have for example number of population assigned to the planet?
Any integer value can be used as the seed, so population should work.
It would not if the population would change over time, as it often does in space strategy games.
I still maintain that the best course here would be to make say... a struct for a planet, with all the required data inside, and store each planet into an array. Each solar system would either have an array of planets with all their info, or would need to be generated and THEN stored into the array.
I agree about using a seed, but System.Random isn't a "C# class", it's a .NET/$$anonymous$$ono class, so it works for any language that uses .NET.
Your answer
Follow this Question
Related Questions
Random generation problem. 2 Answers
death script remains ineffective 1 Answer
Car enter and exit script not working 1 Answer
health problem 1 Answer