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 Ninjaroy · Sep 17, 2016 at 03:18 AM · c#2dterrainperlin noisenoise

[C#] Wondering what is amiss with my 1D Perlin Noise Terrain Generator?

Hello,

To start off I should say that I am still very much a beginner at coding in general, so I must apologize if my incompetence is facepalm-inducing :p

As more or less a learning experience, I've set myself a goal of creating a simple C# script that generates randomized but realistic terrain, consisting of tiled prefabs. To me Perlin Noise seemed to be the way to go considering its relative simplicity (?) and the Mathf.PerlinNoise method.

The code I wrote below works and all, but I'm almost certain that it's just creating random height values rather than the smooth Perlin digits I ventured out for!

 using UnityEngine;
 using System.Collections;
 
 public class terrainGenScript : MonoBehaviour
 {
     public GameObject block;
     public int height;
     public int width;
 
     void RandWorldGen()
     {
         for (int x = -width; x < width; x++)
         {
             float thisRand = Mathf.Floor(Mathf.PerlinNoise(Random.value * x * 100, Random.value * x * 100) * Random.Range(-height, height));
 
             Instantiate(block, new Vector3(x, thisRand, 0), Quaternion.identity);
             for (float y = thisRand - 1; y >= -height; y--)
             {
                 Instantiate(block, new Vector3(x, y, 0), Quaternion.identity);
             }
         }
     }
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             GameObject[] gos = GameObject.FindGameObjectsWithTag("Respawn");
 
             foreach (GameObject go in gos)
             {
                 Destroy(go);
             }
             RandWorldGen();
         }
     }
 }

It would be super amazing if one of you could help me out or point me in the direction that this script should be going :)

Thanks so much for reading!

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

2 Replies

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

Answer by villevli · Sep 17, 2016 at 08:14 AM

Don't use any randoms inside the loop because it will just make the noise random and not perlin. Also the value you multiply x by (scale) must be much lower. I made a fixed version of the script and added more parameters and OnValidate so you can just press play and tweak the parameters in the inspector and see results in realtime.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TerrainGenerator2D : MonoBehaviour
 {
     public GameObject block;
     public int height = 10;
     public int width = 40;
     public float scale = 5f;
     public float offset = 10;
     public float seed;
 
     private List<GameObject> blocks;
 
     // Use this for initialization
     void Start()
     {
         seed = Random.value * 100;
         blocks = new List<GameObject>();
         RandWorldGen();
     }
 
     void RandWorldGen()
     {
         for (int x = -width; x < width; x++) {
             float thisRand = Mathf.Floor((Mathf.PerlinNoise(x * scale * 0.01f + offset, seed) - 0.5f) * height * 2);
 
             blocks.Add(Instantiate(block, new Vector3(x, thisRand, 0), Quaternion.identity) as GameObject);
 
             for (float y = thisRand - 1; y >= -height; y--) {
                 blocks.Add(Instantiate(block, new Vector3(x, y, 0), Quaternion.identity) as GameObject);
             }
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0)) {
             foreach (GameObject go in blocks) {
                 Destroy(go);
             }
             blocks.Clear();
 
             RandWorldGen();
         }
     }
 
     void OnValidate()
     {
         if (!Application.isPlaying || blocks == null) return;
 
         foreach (GameObject go in blocks) {
             Destroy(go);
         }
         blocks.Clear();
 
         RandWorldGen();
     }
 }
 
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 Ninjaroy · Sep 17, 2016 at 01:19 PM 0
Share

That makes a lot of sense, thanks for clearing that up :) Also thanks for the script edit - I'll check it out tomorrow when I have access to a PC

avatar image Ninjaroy Ninjaroy · Sep 18, 2016 at 12:07 AM 0
Share

Yep, looking awesome! Thanks again for the help

avatar image
2

Answer by AurimasBlazulionis · Sep 17, 2016 at 05:50 AM

You are multiplying Perlin noise data with random number. Sure, that is why you get random results. Your line should look like this: float thisRand = Mathf.Floor((Mathf.PerlinNoise(Random.value * x * 100, Random.value * x * 100) - 0.5f) * height * 2); Perlin noise generates data from 0 to 1, so I see you want range from -height to height. You have to subtract 0.5 from the generated data and then multiply by 2 times the height.

Second. Tiles are really expensive. Yes, it is quite hard to create a single mesh, but you will have to do something about that in order to make it really playable (though, separate tiles in 2D are much less expensive than in 3D world, because there are much less of them).

Lastly, you declare thisRand every time. Better store it as a private variable. The problem is that it does GC allocations which degrades performance especially on mobile.

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 Ninjaroy · Sep 17, 2016 at 06:36 AM 0
Share

Hello, and thanks! Trying out your recommendation, I certainly see what you mean, however it seems to yield results that are more or less the same. Like this:

alt text

Rather than something smoother such as this:

alt text

Or am I missing something here and the produced shape is Perlin, but something in its parameters are creating more jagged ground?

Also, I hadn't considered the mesh idea so I might look into that.

Thanks again

jagged.png (68.5 kB)
smooth.png (159.3 kB)

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

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

Distribute terrain in zones 3 Answers

2D Efficient Realtime Terrain Generation 0 Answers

2D Terrain 1 Answer

Procedural Island Terrain Generation 2 Answers

[C#] How to use a Multi Threaded Job Queue for Math function 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