Search code examples
unity-game-enginegame-developmentmath-functions

unity Mathf.PerlinNoise not between 0 and 1


Does anybody know why this:

    Debug.Log(Mathf.PerlinNoise(190911.45f, 2290937.40f));  

gives me: 1.044323 It should have been between 0 and 1 isn't it?

And if it can get bigger than 1 can it get smaller than 0? I'm making a map with sprites and everything works :) except that i get empty spaces if the value is bigger than 1.

I use a random seed, that's why the numbers are so big, if you wondering.

I hope someone can help me out, Thanks :)


Solution

  • From the Unity Documentation,

    Note: It is possible for the return value to slightly exceed 1.0f. You may need to clamp the return value if the 0.0 to 1.0 range is important to you.

    So you need to use float normalized = Mathf.Clamp(Mathf.PerlinNoise(190911.45f, 2290937.40f),0,1f)

    Where second argument is minimum value while third argument is maximum value.