Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with solar system script please? Parenting issues.

Discussion in 'Scripting' started by markashburner, Jul 9, 2019.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Hi

    I am creating a procedural solar system that spawns from script when you click on a star.

    Now I want two different solar systems to spawn...one micro Solar System and one macro Solar System...both solar Systems are exactly the same but just at different scales. the micro Solar System is parented under the Micro game object and the macro solar system is parented under the Macro Solar System

    You can see an example of this in the screenshot below:



    The script that achieves this is written like so and it works fine but I just have one little hiccup with the moons that I am hoping someone here will be able to help me solve.

    Code (CSharp):
    1. void Start() {
    2.             System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
    3.             watch.Start();
    4.             nameGenerator = GetComponent<Lexic.NameGenerator>();
    5.             nameGenerator.namesSourceClass = "Lexic.FantasyFemaleNames2";
    6.             nameGenerator.Initialise();
    7.  
    8.             //Generate Planets
    9.             GameObject microParent = new GameObject("Micro");
    10.             GameObject macroParent = new GameObject("Macro");
    11.  
    12.             int planets = Random.Range(0, numberOfPlanets);
    13.             int moons = Random.Range(0, numberOfMoons);
    14.             int moonsTotal = 0;
    15.             for (int i=0; i<planets; i++) {
    16.                 GameObject planet = GenerateBody(
    17.                
    18.                      ////this is where the micro Solar System is parented too
    19.                     microParent,
    20.                     ////this is where the macro Solar System is parented too
    21.                     macroParent
    22.                     "Planet " + nameGenerator.GetNextRandomName(),
    23.                     planetColor,
    24.                     planetSettings.coloursStartRange,
    25.                     planetSettings.coloursEndRange,
    26.                     planetEccentricityRange,
    27.                     planetDistanceRange,
    28.                     planetSizeRange,
    29.                     planetSettings.heightScale,
    30.                     planetSettings.startValueDistance,
    31.                     planetSettings.detailDistances,
    32.                     planetSettings.calculateMsds,
    33.                     planetSettings.detailMsds,
    34.                     planetSettings.colliders,
    35.                     PlanetType.Terra
    36.                 );
    37.  
    38.            
    39.                 //Generate Moons
    40.            
    41.                 for (int j=0; j<moons; j++) {
    42.                         GenerateBody(
    43.                      ////this is where the micro Moon is parented too
    44.                         microPlanet,
    45.                      ////this is where I am trying to get the macro Moon parented too
    46.                         macroPlanet,
    47.                         "Moon " + nameGenerator.GetNextRandomName(),
    48.                         moonColor,
    49.                         moonSettings.coloursStartRange,
    50.                         moonSettings.coloursEndRange,
    51.                         Vector2.zero, //moons have a circular orbit
    52.                         moonDistanceRange,
    53.                         moonSizeRange,
    54.                         planetSettings.heightScale,
    55.                         planetSettings.startValueDistance,
    56.                         planetSettings.detailDistances,
    57.                         planetSettings.calculateMsds,
    58.                         planetSettings.detailMsds,
    59.                         planetSettings.colliders,
    60.                         PlanetType.Lunar
    61.                     );
    62.                 }
    63.                 moonsTotal += moons;
    64.            
    65.             }
    66.             //Generate Comets
    67.             int comets = Random.Range(0, numberOfComets);
    68.             for (int k=0; k<comets; k++) {
    69.                 GenerateBody(
    70.  
    71.                     microParent,
    72.                     macroParent,
    73.                     "Planet " + nameGenerator.GetNextRandomName(),
    74.                     cometColor,
    75.                     cometSettings.coloursStartRange,
    76.                     cometSettings.coloursEndRange,
    77.                     cometEccentricityRange,
    78.                     cometDistanceRange,
    79.                     cometSizeRange,
    80.                     cometSettings.heightScale,
    81.                     cometSettings.startValueDistance,
    82.                     cometSettings.detailDistances,
    83.                     cometSettings.calculateMsds,
    84.                     cometSettings.detailMsds,
    85.                     cometSettings.colliders,
    86.                     PlanetType.Comet
    87.  
    88.                 );
    89.             }
    90.  
    91.  
    92.             macroParent.transform.parent = gameObject.transform;
    93.             macroParent.transform.localPosition = Vector3.zero;
    94.             microParent.transform.parent = gameObject.transform;
    95.             microParent.transform.localPosition = Vector3.zero;
    96.  
    97.             watch.Stop();
    98.             Debug.Log(
    99.                 string.Format(
    100.                     "Generated {0} planets, {1} moons, and {2} comets in {3}ms",
    101.                     planets, moonsTotal, comets, watch.ElapsedMilliseconds
    102.                 )
    103.             );
    104.         }
    As you can see here in this screenshot the moons are not quite parenting properly for example Orbit Moon Betharath should be a child of Orbit Planet Porath, both moons should be children of the planet but instead Moon Betharath is a child of the other moon - Orbit Mooon Arigakari....



    How do I get both moons to be children of Orbit Planet Porath?

    This is part of the script where the planets generate their bodies.

    Code (CSharp):
    1. GameObject GenerateBody(GameObject microParent, GameObject macroParent, string name, Color color, Color32[] colourStartRange, Color32[] colourEndRange, Vector2 eccentricityRange, Vector2 distanceRange, Vector2 sizeRange, float heightScale, float startValue, float[] detailDistances, bool calculateMsds, float[] detailMsds, bool[] generateColliders, PlanetType planetType)
    2.         {
    3.      
    4.      
    5.             GameObject orbitObj = new GameObject("Orbit " + name);
    6.            ////this is where I am parenting
    7.             orbitObj.transform.parent = microParent.transform;
    8.             orbitObj.transform.localPosition = Vector3.zero;
    9.             microPlanet = orbitObj;
    10.  
    11.             //add an orbit component, and fill it out
    12.             CelestialOrbit orbit = orbitObj.AddComponent<CelestialOrbit>();
    13.             CelestialOrbitPath orbitPath = orbitObj.AddComponent<CelestialOrbitPath>();
    14.             line = orbitObj.GetComponent<LineRenderer>();
    15.             line.material = new Material(Shader.Find("Particles/Standard Unlit"));
    16.      
    17.             line.SetWidth(0.005f, 0.005f);
    18.             orbit.periapsis = Random.Range(distanceRange.x, distanceRange.y);
    19.             orbit.eccentricity = Random.Range(eccentricityRange.x, eccentricityRange.y);
    20.             orbit.inclination = Random.Range(inclinationRange.x, inclinationRange.y);
    21.             orbit.longitude = Random.Range(longitudeRange.x, longitudeRange.y);
    22.             orbit.argument = Random.Range(argumentRange.x, argumentRange.y);
    23.             orbit.meanAnomaly = Random.Range(-180f, 180f);
    24.             orbit.ComputeStaticProperties(); //need to do this so that semi major axis is available
    25.             orbit.period = 2*Math.PI*Math.Sqrt(orbit.semiMajorAxis*orbit.semiMajorAxis*orbit.semiMajorAxis/gravitationParameter);
    26.  
    27.        
    28.  
    29.             //create the model for the body
    30.             GameObject planetObj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    31.        
    32.             planetObj.name = name + " Micro";
    33.             planetObj.GetComponent<Renderer>().material.color = color;
    34.             line.startColor = color;
    35.             line.endColor = color;
    36.        
    37.             planetObj.transform.parent = orbitObj.transform;
    38.             planetObj.transform.localScale = Random.Range(sizeRange.x, sizeRange.y) * Vector3.one;
    39.             planetObj.transform.localPosition = Vector3.zero;
    40.             /////PlanetaryTerrain
    41.             ///
    42.        
    43.            GameObject orbitPlanetaryTerrainObj = new GameObject("Orbit " + name);
    44.             macroPlanet = orbitPlanetaryTerrainObj;
    45.             ////this is where I am parenting
    46.             orbitPlanetaryTerrainObj.transform.parent = macroParent.transform;
    47.             orbitPlanetaryTerrainObj.transform.localPosition = Vector3.zero;
    48.             CelestialOrbit orbitPlanetaryTerrain = orbitPlanetaryTerrainObj.AddComponent<CelestialOrbit>();
    49.  
    50.             orbitPlanetaryTerrain.periapsis = Random.Range(distanceRange.x * 100000, distanceRange.y * 100000);
    51.             orbitPlanetaryTerrain.eccentricity = Random.Range(eccentricityRange.x, eccentricityRange.y);
    52.             orbitPlanetaryTerrain.inclination = Random.Range(inclinationRange.x, inclinationRange.y);
    53.             orbitPlanetaryTerrain.longitude = Random.Range(longitudeRange.x, longitudeRange.y);
    54.             orbitPlanetaryTerrain.argument = Random.Range(argumentRange.x, argumentRange.y);
    55.             orbitPlanetaryTerrain.meanAnomaly = Random.Range(-180f, 180f);
    56.             orbitPlanetaryTerrain.ComputeStaticProperties(); //need to do this so that semi major axis is available
    57.             orbitPlanetaryTerrain.period = 2 * Math.PI * Math.Sqrt(orbitPlanetaryTerrain.semiMajorAxis * orbitPlanetaryTerrain.semiMajorAxis * orbitPlanetaryTerrain.semiMajorAxis / gravitationParameter);
    58.  
    59.             GameObject planetaryTerrainObj = new GameObject(name);
    60.             Planet planet = planetaryTerrainObj.AddComponent<Planet>();
    61.             planetaryTerrainObj.SetActive(false);
    62.             planetaryTerrainObj.name = name;
    63.  
    64.             planet.detailDistances = detailDistances;
    65.  
    66.  
    67.  
    68.             float[] detailD = planet.detailDistances;
    69.  
    70.             for (int i = 0; i < detailD.Length; i++)
    71.             {
    72.                 if (i == 0)
    73.                     detailD[i] = startValue;
    74.                 else
    75.                     detailD[i] = (detailD[i - 1] / 2f);
    76.  
    77.             }
    78.             planet.calculateMsds = calculateMsds;
    79.             planet.detailMsds = detailMsds;
    80.  
    81.             planet.generateColliders = generateColliders;
    82.  
    83.             planetaryTerrainObj.transform.parent = orbitPlanetaryTerrainObj.transform;
    84.             planetaryTerrainObj.transform.localPosition = Vector3.zero;
    85.  
    86.             // planet.shader = Shader.Find("PlanetaryTerrain/PlanetFadeShaderBump");
    87.             //  planet.planetMaterial = new Material(planet.shader);
    88.             planet.GetComponent<Planet>().radius = Random.Range(sizeRange.x * 1000000, sizeRange.y * 1000000);
    89.             planet.GetComponent<Planet>().planetName = name;
    90.  
    91.             planet.heightScale = heightScale;
    92.             planet.planetType = planetType;
    93.  
    94.             return orbitObj;
    95.         }
    96.  
    97.    
    98.     }
    If someone could help me out that would be amazing thanks :)
     
    Last edited: Jul 9, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In line 9 of last script you do

    Code (CSharp):
    1. microPlanet = orbitObj;
    I think this is it. The place where parent planet gets replaced with orbit.
     
  3. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    I need that because that outlines the parent for the moons. microPlanet is an empty gameObject so is macroPlanet...the microPlanet is assigned on line 9 and macroPlanet is assigned on line 44 in that last script from two private variables..

    Code (CSharp):
    1. private GameObject microPlanet;
    2. private GameObject macroPlanet;
    As you can see:
    Code (CSharp):
    1.  macroPlanet = orbitPlanetaryTerrainObj;
    2.  
    Code (CSharp):
    1. for (int i=0; i<planets; i++) {
    2.                 GameObject planet = GenerateBody(
    3.          
    4.                      ////this is where the micro Solar System is parented too
    5.                     microParent,
    6.                     ////this is where the macro Solar System is parented too
    7.                     macroParent
    8.                     "Planet " + nameGenerator.GetNextRandomName(),
    9.                     planetColor,
    10.                     planetSettings.coloursStartRange,
    11.                     planetSettings.coloursEndRange,
    12.                     planetEccentricityRange,
    13.                     planetDistanceRange,
    14.                     planetSizeRange,
    15.                     planetSettings.heightScale,
    16.                     planetSettings.startValueDistance,
    17.                     planetSettings.detailDistances,
    18.                     planetSettings.calculateMsds,
    19.                     planetSettings.detailMsds,
    20.                     planetSettings.colliders,
    21.                     PlanetType.Terra
    22.                 );
    23.      
    24.                 //Generate Moons
    25.      
    26.                 for (int j=0; j<moons; j++) {
    27.                         GenerateBody(
    28.                      ////this is where the micro Moon is parented too
    29.                         microPlanet,
    30.                      ////this is where I am trying to get the macro Moon parented too
    31.                         macroPlanet,
    32.                         "Moon " + nameGenerator.GetNextRandomName(),
    33.                         moonColor,
    34.                         moonSettings.coloursStartRange,
    35.                         moonSettings.coloursEndRange,
    36.                         Vector2.zero, //moons have a circular orbit
    37.                         moonDistanceRange,
    38.                         moonSizeRange,
    39.                         planetSettings.heightScale,
    40.                         planetSettings.startValueDistance,
    41.                         planetSettings.detailDistances,
    42.                         planetSettings.calculateMsds,
    43.                         planetSettings.detailMsds,
    44.                         planetSettings.colliders,
    45.                         PlanetType.Lunar
    46.                     );
    47.                 }
     
    Last edited: Jul 9, 2019
  4. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Don't change the parent object / orbit if its a moon.
    (Create a field / flag for the planet and check against it upon generation)

    Or pass the same parent for all moons.

    Alternatively, do not store those parents in the class.
    Store them on stack only (so that generate methods do not mess around with variables).