Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xentarok · Jun 26, 2013 at 04:20 PM · generatorplanetsstars

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.
alt text

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:

alt text

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

1.jpg (133.6 kB)
2.jpg (82.9 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Em3rgency · Jun 26, 2013 at 04:29 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Em3rgency · Jun 26, 2013 at 04:55 PM 0
Share

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.

avatar image Xentarok · Jun 26, 2013 at 04:59 PM 0
Share

Well, I'm making a strategy game, so what if I'd like to have for example number of population assigned to the planet?

avatar image robertbu · Jun 26, 2013 at 05:07 PM 0
Share

Any integer value can be used as the seed, so population should work.

avatar image Em3rgency · Jun 26, 2013 at 05:24 PM 0
Share

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.

avatar image Eric5h5 · Jun 26, 2013 at 05:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Disable GameObject Only Father Not Children 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges