# 1D Perlin Noise Game in Unity

I'm working on a 2D side-scrolling game in the Unity engine. I am using the 1-Dimensional Perlin Noise function and using pseudo-code. I can't use two links so I'll provide a link to imgur with the images and the links in numerical order. First link in imgur links to the pseudo-code/implementation of perlin noise I am using.

My implementation so far is showing good results.

Here is a picture: This image uses the values:

• Persistance = 0.7

• Octaves = 4

From the first article I linked, it states:

Now, you'll want several different random number generators, so I suggest making several copies of the above code, but use slightly different numbers. Those big scarey looking numbers are all prime numbers, so you could just use some other prime numbers of a similar size.

This block of text is referring to this part of code which is an Integer-noise function:

  float Noise (int x)
{
x = (x << 13) ^ x;
return (float)(1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}


Another site I found the author adjusts the Integer-noise function from discrete to continuous (allowing the range not to be limited). I've made this change although I am not sure if my implementation of both links I've provided is entirely correct. The results are promising but when you change these variables values to:

• Persistence = 1

• Octaves = 8

the terrain starts to have little gaps at the bottom like so: I am not quite sure how to fix this, I know lowering the persistence provides smoother results resolving this issue but that makes the terrain's amplitude rather low for a side-scrolling platformer.

Here is the complete code:

    void Start ()
{
objectArray = new GameObject[width, height];

for (int i = 0; i < width; i++) {

float x = PerlinNoise_1D (i, persistances, octaves);

for (int j = 0; j < height; j++){
objectArray[i,j] = (GameObject)Instantiate (gameobject, new Vector3 (i, j + x, 0), Quaternion.identity);
}
}

}

float Noise(int x){
x = (x << 13) ^ x;
return (float)(1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}

float CoherentNoise  (int x)
{
int intX = (int)(Mathf.Floor(x));
float n0 = Noise (intX);
float n1 = Noise (intX + 1);
float weight = x - Mathf.Floor (x);
float noise = Mathf.Lerp (n0, n1, weight);
return noise;
}

float SmoothedNoise (int x)
{
return CoherentNoise (x) / 2 + CoherentNoise (x - 1) / 4 + CoherentNoise (x + 1) / 4;
}

float InterpolatedNoise (float x)
{

int integer_X = (int)x;
float fractional_X = x - integer_X;

float v1 = SmoothedNoise (integer_X);
float v2 = SmoothedNoise (integer_X + 1);

return Cosine_Interpolate (v1, v2, fractional_X);
}

float Cosine_Interpolate (float a, float b, float x)
{
float ft = x * (float)Mathf.PI;
float f = (float) ((1 - Mathf.Cos ((ft))) * .5);

return  a * (1 - f) + b * f;
}

float PerlinNoise_1D (float x, float persistance, int octaves)
{

float total = 0;
float p = persistance;
int n = octaves - 1;

for (int i = 0; i <= n; i++) {

float frequency = (float)Mathf.Pow (2, i);
float amplitude = Mathf.Pow (p, i);

total +=  InterpolatedNoise (x * frequency) * amplitude;
}