Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Augustus_Argento · Nov 29, 2021 at 05:32 PM · instantiaterandomspawnfield of viewasteroid

Trying to make a field of meteors forever, but I can tell I'm doing it horribly

What I'm trying to do is to make a field of asteroids, where there are a set of ten random grids that can spawn around a player-controlled rocket as it moves. A grid is a 4x4 square area where a random amount (between 1 and 4) of meteors spawn in random location.

I can successfully make one grid (a random amount of randomly placed meteors), but I'm having trouble storing the grids, and I have no idea how to spawn the grids around the rocket as it moves (I want to do this so the game doesn't try to spawn meteors literally everywhere, thus killing my computer).

Here's the code I have so far, as you can see it is very bad:

 public class CloneMe : MonoBehaviour
 {
     // Start is called before the first frame update
     public GameObject self;
     public int i;
     public bool cloneThis = true;
     void Start()
     {
         duplicateMe();
     }
     public void duplicateMe()
     {
         // makes sure this only runs on the original meteor
         if (cloneThis == true)
         {
             int amountOfClones = Random.Range(1, 3);
             // makes a random amount of meteors in random positions
             for (i = 0; i < amountOfClones; i++)
             {
                 Vector2 firstPosition;
                 Vector2 secondPosition;
                 Vector2 thirdPosition;
                 Vector2 fourthPosition;
                 int differenceInXPosition;
                 if (amountOfClones == 1)
                 {
                     firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     transform.position = new Vector2(firstPosition);
                     var newObj = Instantiate(self);
                     newObj.GetComponent<CloneMe>().cloneThis = false;
                     differenceInXPosition = 0;
                 }
                 if (amountOfClones == 2)
                 {
                     firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     secondPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     transform.position = new Vector2(firstPosition);
                     var newObj = Instantiate(self);
                     transform.position = new Vector2(secondPosition);
                     newObj = Instantiate(self);
                     newObj.GetComponent<CloneMe>().cloneThis = false;
                     differenceInXPosition = System.Math.Abs(firstPosition.x - secondPosition.x);
                 }
                 if (amountOfClones == 3)
                 {
                     firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     secondPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     thirdPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     transform.position = new Vector2(firstPosition);
                     var newObj = Instantiate(self);
                     transform.position = new Vector2(secondPosition);
                     newObj = Instantiate(self);
                     transform.position = new Vector2(thirdPosition);
                     newObj = Instantiate(self);
                     newObj.GetComponent<CloneMe>().cloneThis = false;
                     differenceInXPosition = System.Math.Max(firstPosition.x, secondPosition.x, thirdPosition.x) - System.Math.Min(firstPosition.x, secondPosition.x, thirdPosition.x);
                 }
                 if (amountOfClones == 4)
                 {
                     firstPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     secondPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     thirdPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     fourthPosition = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                     transform.position = new Vector2(firstPosition);
                     var newObj = Instantiate(self);
                     transform.position = new Vector2(secondPosition);
                     newObj = Instantiate(self);
                     transform.position = new Vector2(thirdPosition);
                     newObj = Instantiate(self);
                     transform.position = new Vector2(fourthPosition);
                     newObj = Instantiate(self);
                     newObj.GetComponent<CloneMe>().cloneThis = false;
                     differenceInXPosition = System.Math.Max(firstPosition.x, secondPosition.x, thirdPosition.x, fourthPosition.x) - System.Math.Min(firstPosition.x, secondPosition.x, thirdPosition.x, fourthPosition.x);
                         }
         
             }
         }
         cloneThis = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }
 

The code for one grid is as follows:

 public class CloneMe : MonoBehaviour
 {
     // Start is called before the first frame update
     public GameObject self;
     public int i;
     public bool cloneThis = true;
     void Start()
     {
         duplicateMe();
     }
     public void duplicateMe()
     {
         // makes sure this only runs on the original meteor
         if (cloneThis == true)
         {
             int amountOfClones = Random.Range(1, 3);
             // makes a random amount of meteors in random positions
             for (i = 0; i < amountOfClones; i++)
             {
                 transform.position = new Vector2(Random.Range(0, 4), Random.Range(0, 4));
                 var newObj = Instantiate(self);
                 newObj.GetComponent<CloneMe>().cloneThis = false;
             }
         }
         cloneThis = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }


as you can probably guess my head hurts so simple words please and thank you

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

161 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 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 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 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 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 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 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 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 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

Spawn Random Object script spawns too much 1 Answer

Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers

How to spawn objects on a list of Transforms ? 2 Answers

Script component seems to be missing when spawning prefab? 2 Answers

Problem with Instantiated clones 1 Answer


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