Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

PerlinNoise not working

Discussion in 'Scripting' started by Juice-Tin, Apr 14, 2015.

  1. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    234
    Code (CSharp):
    1. for(int x=0; x<width; x++){
    2.        for(int y=0; y<height; y++){
    3.             ar[x,y] = Mathf.PerlinNoise(x, y);
    4.        }
    5. }
    width is 513, height is 513... the Perlinnoise is ALWAYS returning 0.4652731.
    (This is to set terrain data height)

    What's going on? Can it not be used within for loops or something?
     
    vatsalm30 likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,749
    When I googled that particular number, I found this page, which confirms what I suspected would be the answer - you're using whole numbers, and PerlinNoise expects floats.
     
    Juice-Tin likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,072
    try multiplying with some scale value,
    Code (CSharp):
    1.  
    2. float scaler = 0.15f;
    3. ..
    4. ar[x,y] = Mathf.PerlinNoise(x*scaler, y*scaler);
     
    Juice-Tin likes this.
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    234
    Thanks guys, you're completely right.
    Also @StarManta interesting how it always puts out that exact number when putting in an int :O
     
  5. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    It's more like anything over 1 returns the value of 1.
     
  6. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    234
    Nope, try it. You can literally put in any number imaginable, as long as you don't enter 2 ints you'll get good results!
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,749
    Interestingly, while it gives .4652731 for all whole numbers, it's not a repeating pattern. The Perlin results for 0.5, 1.5, 2.5, etc seems to jump randomly between 4 or 5 different particular values (one of which is, in fact, .4652731, out at 12.5 and 13.5...). Odd quirks of Perlin noise I guess....
     
  8. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Random Perlin noise always has repetitive boundary values that by default are at the whole numbers, where the value is always the same. It's kind of like a grid, where the values exactly on the intersections of the grid lines are always the same, but the values within the grid squares can be anything and are different in every square.