• 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 BlueSin · Feb 27, 2014 at 09:57 PM · terrainproceduralgenerationblock

2D Procedural Terrain Generation

So I am trying to figure out how to create a procedural terrain generation engine for a 2D game similar to Terraria or Starbound. But I am running into a problem when it comes to generating blocks. Apparently I am generating so many that Unity is having a heart attack and dying. So let's cover what I have so far:

 public int mapWidth, mapHeight;
 GameObject[,] blocks;
 public GameObject baseBlock;

Where baseBlock is a prefab game object with a 2D box collider and spriterenderer attached, with a default sprite. I found that if I place it within the scene but positioned at Y 10,000 so it's out of site it greatly shortens map generation time because the base block has already been instantiated when I start cloning it.

Then I test generation using the Start() method, generating those blocks is also straight forward:

 blocks = new GameObject[mapWidth, mapHeight];
 for(int x = 0; x < blocks.GetLength(0); x++)
   for(int y = 0; y < blocks.GetLength(1); y++)
     blocks[x, y] = (GameObject)Instantiate(baseBlock, Vector2.zero, Quaternian.identity);

That all works just fine save for one problem. If my mapWidth or mapHeight > 256 or so, Unity croaks. Too many Game Objects I guess. I looked into pooling and stuff but that seems unreasonable, requiring the introduction of systems meant for 3D being introduced to your 2D world to create a mesh, etc., etc. My large maps will be 8192x2048 blocks in size, or so I am hoping, so the measly 256x256 blocks I can create right now is a far cry from my objective. What can I do to resolve this?

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 Maui-M · Feb 27, 2014 at 11:58 PM

You should probably set up some dynamic loading/unloading of your gameobjects. So instead of trying to load them all into your scene you are only loading the ones the player can see. You would also want to add an acceptable level of padding to this so you don't run to the right and fall off the world cause nothing is loaded yet.

Below I have done some of it, and commented some logic that needs to be added in. But that should get you started.

 public int loadWidth, loadHeight;
 public float updateTime = 1.0f;
 private float currTime = 0.0f;
 private Vector2 lastPosition;
 
 public int mapWidth, mapHeight;
 GameObject[,] blocks;
 public GameObject baseBlock;
 
 // Initial load
 blocks = new GameObject[mapWidth, mapHeight];
 for(int x = currentPosition-(loadWidth/2); x < currentPosition+(loadWidth/2); x++)
   for(int y = currentPosition-(loadHeight/2); y < currentPosition+(loadHeight/2); y++)
     blocks[x, y] = (GameObject)Instantiate(baseBlock, Vector2.zero, Quaternian.identity);
     
 // Update
 // You will have to change this to fit your code
 currTime += Time.deltaTime;
 if(currTime > updateTime && lastPosition != currentPosition)
 {
     currTime = 0.0f;
     
     // Get min and max values from currentPosition and lastPosition
     int minX, maxX;
     int minY, maxY;
     
     for(int x = minX; x < maxX; x++) {
         for(int y = minY; y < maxY; y++) {
             //Add new objects
         }
     }
     
     // remove old objects
     
     lastPosition = currentPosition;
 }
Comment
Add comment · Show 2 · 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 BlueSin · Feb 28, 2014 at 01:19 AM 0
Share

Thank you much!

avatar image robertbu · Feb 28, 2014 at 01:19 AM 0
Share

If @$$anonymous$$aui $$anonymous$$ answered your question, please click on the checkmark to close the question out. Thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to procedural generation 0 Answers

Procedural Terrain Generation on a Map the Scale of the Milky Way 2 Answers

Diamond Square Algorithm 2 Answers

This script really makes my game lag! 1 Answer

3d Perlin Noise? 3 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