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 Citiral · Aug 18, 2013 at 02:28 PM · terraindatasetheights

Unity freezes when calling terrainData.SetHeights

I am trying to use Mathf.perlinnoise to randomly create terrain for my game, but whenever I call terrainData.SetHeights unity just freezes and stops responding. I looked around but didn't find anyone else encountering this problem. Is there something obvious I am overlooking in my code? I am using unity 4.0.0f7

     void generateMap(int seed, int perlinwidthperpixel) {
         Random.seed = seed;
          float[,] heights = new float[(int)terrain.terrainData.heightmapWidth, (int)terrain.terrainData.heightmapHeight];
         Debug.Log(terrain.terrainData.heightmapWidth);
         Debug.Log(terrain.terrainData.heightmapHeight);
         for (int i = 0 ; i < terrain.terrainData.heightmapWidth ; i++) {
             for (int k = 0 ; k < terrain.terrainData.heightmapHeight ; k++) {
                 float perlin = Mathf.PerlinNoise(i*perlinwidthperpixel,k*perlinwidthperpixel);
                 heights[i,k] = perlin;
             }
         }        
         terrain.terrainData.SetHeights(0,0,heights);
     }
Comment
Add comment · Show 2
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 Eric5h5 · Aug 18, 2013 at 03:27 PM 0
Share

You could speed that up a little by assigning terrain.terrainData.heightmapWidth/heightmapHeight to variables, and using the variables in the loops, so heightmapWidth and Height aren't constantly evaluated every loop iteration.

avatar image Citiral · Aug 18, 2013 at 03:36 PM 0
Share

Good idea, I will definately do that. Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Doeko · Aug 18, 2013 at 03:02 PM

SetHeights for larger terrains is very slow and generating the Perlin noise will also take a bit of processing. Most likely it is not crashed but simply executing the function. Wait a while longer and see if it finishes or experiment with a terrain with a very small resolution, such as 128x128.

You could stick it into a coroutine if you don't want the game to freeze while executing the method.

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 Eric5h5 · Aug 18, 2013 at 03:23 PM 0
Share

SetHeights only takes a few seconds for max-size terrains (4097x4097), and a fraction of a second for 1025x1025. It's also an atomic operation, so putting it into a coroutine will do nothing; coroutines aren't threads.

avatar image Citiral · Aug 18, 2013 at 03:32 PM 0
Share

The perlin noise doesn't take up much time, when commenting out the setheights function it only freezes for about a second or less. I have had it running for over half an hour in the background, so if it would ever load, it would have already done so. I also noticed that when I change heights[i,k] = perlin; to heights[i,k] = 1.2f; or any other random float, it does load.

avatar image
0

Answer by Citiral · Aug 18, 2013 at 04:44 PM

I managed to fix the problem partially. If I use terrain.terrainData.heightmapHeight-1 instead of terrain.terrainData.heightmapHeight in my code everything works just fine. This does cause the last vertices of my terrain to not by affected by the generation, which is a problem. Is this a bug in unity?

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 Citiral · Aug 18, 2013 at 04:44 PM 0
Share

Never$$anonymous$$d, I had a really, really stupid bug in another bit of code that caused this problem. I'm sorry for the trouble :)

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

17 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

Related Questions

How can I programmatically stretch a Terrain? 1 Answer

terraindata.setheight as a brush for runtime terrain editing 1 Answer

Can be terrainData.setHeights' values negative? 1 Answer

Problems with Translating Java in C# 1 Answer

Issue copying heightmaps from a LARGE terrain to a new, small, cutout Terrain with GETHEIGHTS and SETHEIGHTS 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