Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jun 04, 2018 at 03:27 PM by bitsacpce for the following reason:

Solved it myself

avatar image
0
Question by bitsacpce · Jun 04, 2018 at 09:26 AM · spawningplatformrandomspawning

random spawning of platforms. Overlapping and 2x2 spawning issue.

So in my quest to make a game like the very popular zigzag game, I am stuck at randomly generating platforms. The platforms generate randomly in X,-X,Z,-Z directions. I have written my code where it generates the platforms. I might have taken a very long approach (alternative approaches if any)

              void Start () 
             {
                 lastPos = platform.transform.position;
                 size = platform.transform.localScale.x;

                 InvokeRepeating("SpawnXZ",1f,0.2f);
             }

             void SpawnX()
             {   
                 Vector3 pos = lastPos;
                 pos.x += size;
                 lastPos = pos;
                 Instantiate(platform, pos, Quaternion.identity);    
             }

             void SpawnZ()
             {
                 Vector3 pos = lastPos;
                 pos.z += size;
                 lastPos = pos;
                 Instantiate(platform, pos, Quaternion.identity); 
             }

             void SpawnNegX()
             {
                 Vector3 pos = lastPos;
                 pos.x -= size;
                 lastPos = pos;
                 Instantiate(platform, pos, Quaternion.identity);  
             }

             void SpawnNegZ()
             {
                 Vector3 pos = lastPos;
                 pos.z -= size;
                 lastPos = pos;
                 Instantiate(platform, pos, Quaternion.identity);
             }

             void SpawnXZ()
             {

                     int rand = Random.Range(0, 6);
                     if (rand < 3)
                     {
                             SpawnX();
                     }
                     else if(rand >= 3)
                     {
                             SpawnZ();
                     }

                     if(--counter == 0) { CancelInvoke("SpawnXZ"); };


                 if(counter == 0)
                 {   
                     counter = 25;
                     int r = Random.Range(0,2);
                     if(r == 0)
                     {   
                         InvokeRepeating("SpawnNegXZ",0f,0.2f);
                     }
                     else
                     {       
                         InvokeRepeating("SpawnXNegZ",0f,0.2f);
                     }
                 }

             }

             void SpawnNegXZ()
             {
                 int rand = Random.Range(0, 6);
                     if (rand < 3)
                     {   
                             SpawnNegX();
                     }
                     else if(rand >= 3)
                     {   
                             SpawnZ();
                     }

                 if(--counter == 0) { CancelInvoke("SpawnNegXZ"); };


                 if(counter == 0)
                 {   
                     counter = 25;
                     int r = Random.Range(0,2);
                     if(r == 0)
                     {   
                         InvokeRepeating("SpawnXZ",0f,0.2f);
                     }
                     else
                     {   
                         InvokeRepeating("SpawnNegXNegZ",0f,0.2f);
                     }
                 }
             }

             void SpawnXNegZ()
             {
                     int rand = Random.Range(0, 6);
                     if (rand < 3)
                     {   
                             SpawnX();
                     }
                     else if(rand >= 3)
                     {   
                             SpawnNegZ();
                     }       


                 if(--counter == 0) { CancelInvoke("SpawnXNegZ"); };


                 if(counter == 0)
                 {   
                     counter = 25;
                     int r = Random.Range(0,2);
                     if(r == 0)
                     {   
                         InvokeRepeating("SpawnXZ",0f,0.2f);
                     }
                     else
                     {   
                         InvokeRepeating("SpawnNegXNegZ",0f,0.2f);
                     }
                 }
             }

             void SpawnNegXNegZ()
             {

                     int rand = Random.Range(0, 6);
                     if (rand < 3)
                     {
                             SpawnNegX();    
                     }
                     else if(rand >= 3)
                     {   
                             SpawnNegZ();
                     }

                 if(--counter == 0) { CancelInvoke("SpawnNegXNegZ"); };


                 if(counter == 0)
                 {   
                     counter = 25;
                     int r = Random.Range(0,2);
                     if(r == 0)
                     {   
                         InvokeRepeating("SpawnNegXZ",0f,0.2f);
                     }
                     else
                     {   
                         InvokeRepeating("SpawnXNegZ",0f,0.2f);
                     }
                 }
             }

I have clubbed xz, -xz , x -z and -x -z. I call platform spawning in X and Z direction first, then switching to -XZ or x -Z and so on. But there are 2 main issues.

alt text

PS : those small black squares are nothing but diamonds (ignore them).

They either form a 2X2 block or overlap each other.

How do I avoid these? or is there a simpler way of generating platforms that I am missing.

unity-problem.png (427.1 kB)
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by bitsacpce · Jun 04, 2018 at 03:26 PM

Never mind. I solved it using raycast.

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

Follow this Question

Answers Answers and Comments

84 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

Related Questions

Spawn another platform infront of the original platform 1 Answer

Multiple spawn location with random game object in random time spawn c# 1 Answer

Spawn objects at random position ON a 2D object. 1 Answer

Instantiate random object from Array 2 Answers

Randomly placing an object outside of a box but within a certain range 4 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