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 thomfur · Apr 17, 2016 at 07:25 PM · 2dperlin noiseperlin

Perlin noise not being changed runtime

I have written some code to generate some terrain. It uses perlin noise to decide what type of tile to spawn. I am happy with the results of using perlin noise, but there is a problem. I would assume that with this code a different terrain would be generated, because the perlin noise generated would be different. However this doesn't work, so is there a problem in the code? Or is the perlin noise cached and I have to generate a new one at runtime? Here is my code:

     using UnityEngine;
     using System.Collections;
     
     public class TerrainGeneration : MonoBehaviour {
         public float tileWidth;
         public GameObject grass;
         public GameObject dirt;
         public GameObject water;
         private float[,] terrainArray;
         // Use this for initialization
         void Start () {
             Vector2 currentPos = (Vector2)transform.position;
             for (float x = 0; x < 100; x++) {
                 for (float y = 0; y < 100; y++) {
                     float noise = Mathf.PerlinNoise (x / 10f, y / 10f);
                     if (noise < 0.2) {
                         Instantiate (water, currentPos, transform.rotation);
                     } else if (noise > 0.2 && noise < 0.8) {
                         Instantiate (grass, currentPos, transform.rotation);
                     } else {
                         Instantiate (dirt, currentPos, transform.rotation);
                     }
                     currentPos = currentPos + new Vector2 (tileWidth, 0);
                 }
                 currentPos = new Vector2 (0, currentPos.y + tileWidth);
             }
         }
         
         // Update is called once per frame
         void Update () {
         
         }
     }
     
 
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 Eno-Khaon · Apr 17, 2016 at 08:13 PM

Part of the intent of the Perlin Noise algorithm is that, given the same inputs, you will see the same outputs every time. For example, this allows you to use the algorithm to generate an infinite environment without having to save infinite data to the hard drive.

Your inputs for the noise are two floating point numbers, x and y. They effectively range from 0 to 9.9, by your implementation.

Because the noise algorithm will give you consistent outputs for consistent inputs, you can expect to see the same results every time right now.

What this means is that you can modify your scale for your for loops and see different outputs as a result.

An example of this could look something like:

 public float xOffset;
 public float yOffset;
 
 // ...

 for(float x = xOffset; x < 100 + xOffset; x++)
 {
     for(float y = yOffset; y < 100 + yOffset; y++)
     {
         // ...
     }
 }

In this form, your scale would remain unchanged from its current state, so all changes would come from the offsets alone. That said, scaling could also be modified with further tweaks, such as:

 for(float x = xOffset; x < (100 * increment) + xOffset; x+=increment)
Comment
Add comment · 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
1

Answer by tanoshimi · Apr 17, 2016 at 08:07 PM

If you always sample the same co-ordinates, you'll always get the same result. You need to apply a random offset, something like:

 Vector2 offset = new Vector2(Random.Range(0,100f),Random.Range(0,100f));
 float noise = Mathf.PerlinNoise (x / 10f + offset.x, y / 10f + offset.y);
 ...
Comment
Add comment · 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

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

65 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

Related Questions

Adding rivers to procedural generated 2D map 1 Answer

Perlin Noise world generator, instantiating smaller tiles together. 2 Answers

2D Animation does not start 1 Answer

Perlin Noise Implementation 2 Answers

Always same result from Mathf.PerlinNoise() 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