- Home /
Question by
beau101023 · May 21, 2015 at 06:17 AM ·
c#terraingenerationgeneratorflat
Terrain Generator Producing Completely Flat Results.
My terrain generator is failing to produce any variation in height whatsoever. The logic seems to work out fine on paper. The system is supposed to work by alternating between a system of deriving rows of cube heights progressively, taking the height of the previous cube and adjusting the next up, down, or not at all, and a system of deriving the next row's height map from the previous row.
using UnityEngine;
using System.Collections;
public class Terrain_Generator : MonoBehaviour
{
public GameObject Target_Obj;
int width = 5;
int iteration;
int length = 5;
public int height;
int prevPos;
int height0;
int height1;
int height2;
int height3;
int height4;
int height5;
int horizPos;
void Start ()
{
//j controls the x coord.
for (int j=0; j<width; j = j + 1)
{
prevPos = 0;
//i controls the z coord.
for (int i=0; i<length; i = i + 1)
{
/*Sets the horizPos to generate cubes 1 unit below or above the previous cube's
position, as well as in the same position height-wise.*/
if(j == 0 && j == 2 && j == 4)
{
horizPos = Random.Range (prevPos + height, prevPos - height * 2);
}
//Generates the cube's height based on the corresponding cube in the previous row.
if(j == 1 && j == 3 && j == 5)
{
if(i == 0)
{
horizPos = Random.Range (height0 + height, height0 - height * 2);
}
if(i == 1)
{
horizPos = Random.Range (height1 + height, height1 - height * 2);
}
if(i == 2)
{
horizPos = Random.Range (height2 + height, height2 - height * 2);
}
if(i == 3)
{
horizPos = Random.Range (height3 + height, height3 - height * 2);
}
if(i == 4)
{
horizPos = Random.Range (height4 + height, height4 - height * 2);
}
if(i == 5)
{
horizPos = Random.Range (height5 + height, height5 - height * 2);
}
}
Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
prevPos = horizPos;
//Stores y coord of previous j iteration's cubes in order.
if(i == 0)
{
height0 = horizPos;
}
if(i == 1)
{
height1 = horizPos;
}
if(i == 2)
{
height2 = horizPos;
}
if(i == 3)
{
height3 = horizPos;
}
if(i == 4)
{
height4 = horizPos;
}
if(i == 5)
{
height5 = horizPos;
}
}
}
}
void Update ()
{
}
}
Comment
Best Answer
Answer by tanoshimi · May 21, 2015 at 06:54 AM
The parameters for Random Random.Range are (min, max).
It looks like you're supplying (max, min) every time.
D'oh. Thanks for pointing this out. I feel silly now. :P