Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

c# - Unity: Randomized solarsystems, planet type and size issues

I am trying to give randomized values to a set of planets in a solar system, and I am having issues which I can't fix. I use a System.Random method on a dictionary of enum values which specify the planets type and size (type: grassland, oceanic, volcanic...), (Size: Small, medium, big...) These work fine as when I have a load of chunky if else statements it makes the correct solar systems. I switched these out for switch statements, as I heard they are better for efficiency. This is where the problem started, the switch statements simply return either a sprite or a vector3 depending on which random key from the dictionary they receive. But my game spawns all of the solar systems but with no randomization, the solar systems have a max planets variable which is currently set at '8' but for a row of 6-7 solar systems every planet will be the same type and size, but different locations. Then the next 6-7 solar systems will be a different type and size.

I will paste the method which I am having the issues in. I am quite new to C# and programming, so any advice would be appreciated! Thanks.

public void GeneratePlanets()
{
    int numberOfPlanetsInSystem = UnityEngine.Random.Range(0, GetMaxPlanets());
  
        for (int j = 0; j < numberOfPlanetsInSystem; j++)
        {

            int i = UnityEngine.Random.Range(0, GetMaxPlanets());
            // Make a planet!
            Planet planet = new Planet();
            planets[i] = planet;
            planet.Name = GeneratePlanetName(i);
            planet.PlanetType = GeneratePlanetType();
            planet.PlanetSize = GeneratePlanetSizeDictionary();
            Debug.Log(planet.PlanetType);

        
            GameObject go = new GameObject();
            SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
            sr.name = planet.Name;
            sr.sprite = GetPlanetType(planet.PlanetType);
            go.transform.localScale = GetPlanetSize(planet.PlanetSize);
            go.transform.position = this.Position + new Vector3(UnityEngine.Random.Range(-200, 200), UnityEngine.Random.Range(-200, 200), 0); // Fix later (not hardcoded values).
        
            

        }
   
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...