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
1
Question by Max 4 · Jul 21, 2011 at 08:50 AM · randomproceduralgenerate

How do I generate believable terrain for a 2D game using cubes?

Ok, I have a fairly simple code that spawns a moving block. It spawns them in a range.

 Instantiate (defaultground, Vector3 (transform.position.x, Random.Range(0,2.5), transform.position.z), transform.rotation);

What I want to do with this snippet code is to replace it with code that can set a "trend", ie. instead of randomly making blocks, making it go up and down in hills. How should I approach 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

Answer by Herman-Tulleken · Jul 21, 2011 at 10:14 AM

Check out this:

http://devmag.org.za/2009/04/25/perlin-noise/

Basically, you can generate Perlin noise, and use that to drive the vertical position of your cubes.

I am not sure what you want to do---is using Unity's terrain system an option? There are also plug-ins for generating procedural landscape.

Edit: Here is an version from the DevMag article translated to Unity to get you started. It is 1D only... read the article for an explanation.

Here is how it looks:

alt text

...Sorry, QATO's image thing does not want to work... here is the link:

http://www.devmag.org.za/examples/perlin_noise/terrain.png

 public class TerrainSpawn : MonoBehaviour 
 {
     public GameObject cubePrefab;
     public int cubesToGenerate = 20;
 
     float[] GetEmptyArray(int width)
     {
         return new float[width];
     }
 
     float[] GenerateWhiteNoise(int width)
     {
         float[] noise = GetEmptyArray(width);
 
         for (int i = 0; i < width; i++)
         {
             noise[i] = (float)Random.value;        
         }
 
         return noise;
     }
 
     float Interpolate(float x0, float x1, float alpha)
     {
        return x0 * (1 - alpha) + alpha * x1;
     }
 
 
     float[] GenerateSmoothNoise(float[] baseNoise, int octave)
     {
        int width = baseNoise.Length;     
        float[] smoothNoise = GetEmptyArray(width);     
        int samplePeriod = 1 << octave; // calculates 2 ^ k
        float sampleFrequency = 1.0f / samplePeriod;
      
        for (int i = 0; i < width; i++)
        {
           //calculate the horizontal sampling indices
           int sample_i0 = (i / samplePeriod) * samplePeriod;
           int sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around
           float horizontal_blend = (i - sample_i0) * sampleFrequency;
           
           smoothNoise[i] = Interpolate(baseNoise[sample_i0],
                 baseNoise[sample_i1], horizontal_blend);          
        }
      
        return smoothNoise;
     }
 
     float[] GeneratePerlinNoise(float[] baseNoise, int octaveCount)
     {
         int width = baseNoise.Length;
         float[][] smoothNoise = new float[octaveCount][]; //an array of 2D arrays containing
         float persistance = 0.5f;
 
         //generate smooth noise
         for (int i = 0; i < octaveCount; i++)
         {
             smoothNoise[i] = GenerateSmoothNoise(baseNoise, i);
         }
 
         float[] perlinNoise = GetEmptyArray(width);
         float amplitude = 1.0f;
         float totalAmplitude = 0.0f;
 
         //blend noise together
         for (int octave = octaveCount - 1; octave >= 0; octave--)
         {
             amplitude *= persistance;
             totalAmplitude += amplitude;
 
             for (int i = 0; i < width; i++)
             {
                 perlinNoise[i] += smoothNoise[octave][i] * amplitude;
             }
         }
 
         //normalisation
         for (int i = 0; i < width; i++)
         {
             perlinNoise[i] /= totalAmplitude;
         }
 
         return perlinNoise;
     }
 
     public void Start()
     {
         float[] perlinNoise = GeneratePerlinNoise(GenerateWhiteNoise(cubesToGenerate), 5);
 
         for (int i = 0; i < cubesToGenerate; i++)
         {
             GameObject gObject = (GameObject) Instantiate(cubePrefab);
 
             gObject.transform.position = new Vector3(i, 4*perlinNoise[i], 0);
         }
     }        
 }

Comment
Add comment · Show 8 · 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 Max 4 · Jul 21, 2011 at 11:00 AM 0
Share

It's a side scrolling game, using a prefab as ground.

avatar image Herman-Tulleken · Jul 21, 2011 at 11:28 AM 1
Share

O$$anonymous$$. So using Perlin noise should be fine; you only need to generate 1D Perlin noise though. Here is a better article for the 1D case:

http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

avatar image Max 4 · Jul 21, 2011 at 11:31 AM 0
Share

I'm not quite sure on how to make perlin noise in Unity. I only really know how to program things that directly relate to Unity.

avatar image Herman-Tulleken · Jul 21, 2011 at 02:40 PM 0
Share

O$$anonymous$$, I did a quick version for you. Play with the constants for different effects.

avatar image Max 4 · Jul 22, 2011 at 06:51 AM 0
Share

What do I change to make different ranges? I usually code in JavaScript so...

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to instantiate prefabs between 2 objects like a path 0 Answers

Procedural Terrain? 3 Answers

Generating objects randomly, only one will Instantiate 0 Answers

Procedurally generating terrain? 0 Answers

Game Map Boundaries/Respawn somewhere else when you walk too far off course 1 Answer


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