Terrain Generation Negative Height?
Hey guys, I was following this video here https://www.youtube.com/watch?v=-5OS1s-NWRw&t=289s&ab_channel=ChronoABI but I've had some strange issues.
The terrain generator is spewing height values that don't make sense for the code that's written, sometimes even returning negative values, like in this screenshot:
At least from what I can tell, the height value should only range from -1 to +2 from the supplied inspector value, which is by default 10. This should mean a height range of only 9 to 12 right? I've also created a method that instantiates a player prefab that has a Cinemachine 2D camera following it. The code for the Generation script is here:
public class Generator : MonoBehaviour
{
[SerializeField] int width, height;
public GameObject dirt, grass;
public GameObject player;
void Start()
{
GeneratorFunction();
SpawnPlayer(player);
}
void SpawnPlayer(GameObject player)
{
player = Instantiate(player, new Vector3(0, height, 0), Quaternion.identity);
}
public void GeneratorFunction()
{
for(int x = 0; x < width; x++)
{
int minHeight = height - 1;
int maxHeight = height + 2;
height = Random.Range(minHeight, maxHeight);
for(int y = 0; y < height; y++)
{
spawnObj(dirt, x, y);
}
spawnObj(grass, x, height);
}
}
void spawnObj(GameObject obj, int width, int height)
{
obj = Instantiate(obj, new Vector2(width, height), Quaternion.identity);
obj.transform.parent = this.transform;
}
}
I'm scratching my head wondering how these values are coming out of this code. Any help would be appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613043630im_/https://answers.unity.com/themes/thub/images/avi.jpg)