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 /
avatar image
0
Question by ParagonTime · Aug 08, 2016 at 04:37 PM · c#bugmapprocedural generationprocedural-terrain

Having some issues with procedurally generating a map

I'm building a very simple 2d tile map. It just spawns a few different grass tiles in random locations here's what it looks like:

 public int tileHeight; //y 
     public int tileWidth; //x 
     int tileHeightCounter;
     int tileWidthCounter;
 
     public GameObject[] floorTiles; 
 
 
 
 
 
     void Start(){
          
         tileHeightCounter = 0; 
         tileWidthCounter = 0; 
 
         while(tileHeightCounter < tileHeight){ 
 
             Vector3 tileSpawnPoint = new Vector3 (tileWidthCounter, tileHeightCounter, 0); 
 
             GameObject groundTiles = (GameObject)Instantiate (floorTiles[Random.Range(0, floorTiles.Length)], tileSpawnPoint, Quaternion.identity); 
             groundTiles.transform.parent = transform; 
 
 
 
 
                 
                 if (tileWidthCounter == tileWidth) {
                     tileWidthCounter = 0;
                     tileHeightCounter++;
             }
 
             tileWidthCounter++;
 
         }
 
 
 
     
     }


I've run into two problems- say your tileHeight is 10 and your tileWidth is also 10 then the map it generates should be a 10x10 with 100 total tiles randomly distributed.

Instead two weird things are occurring the first is that if your map is 10x10 it actually generates a 10x9 meaning it stops one layer short on the y axis. The second is a grid(grass tile) is being created at 0,0 but the rest of the map is being created with at least x being 1 meaning that the map has a single tile attached to the bottom left sticking out.

I'm a bit confused as to whats going on here or how to fix it. Any help is much appreciated.

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

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

Answer by Rob2309 · Aug 09, 2016 at 01:09 AM

it would be way easier and prettier if you used two nested for-loops like this: public int tileHeight; public int tileWidth;

     public GameObject[] floorTiles;
 
     void Start()
     {
         for(int x = 0; x < tileWidth; x++)
         {
             for(int y = 0; y < tileHeight; y++)
             {
                 Vector3 tileSpawnPoint = new Vector3(x, y, 0);
 
                 var groundTiles = (GameObject)Instantiate(floorTiles[Random.Range(0, floorTiles.Length)], tileSpawnPoint, Quaternion.identity);
                 groundTiles.transform.SetParent(transform, true); //2nd parameter makes sure the tile stays at its global position
             }
         }
     }

In the code you posted, the loop runs once more than you want for each row, meaning that the tile field will be one tile too large in the width everytime, while being correctly sized in the y direction... so theoretically your code would produce a 11x10 field if I'm correct :)

Comment
Add comment · Show 1 · 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 ParagonTime · Aug 09, 2016 at 03:33 PM 0
Share

This was exactly what I was looking for never thought about using nested for loops (still new to this). Thank you for the help!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Items not destroying. 1 Answer

Symmetrical Map Design 0 Answers

Google Map Geocoding API gives different results on different platforms 0 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