1

So I am trying to get a grasp on unity development with c#, and for my first challenge i decided to make a minecraft clone to understand vertices and meshes better. I have created a chunk system successful and it works great, but my problem is with the random smooth terrain.

My solution was to create a perlin noise table the size of the entire world, then i will get specific pieces of the noise table to put on each chunk. I've tried this but i get an error

IndexOutOfRangeException: Array index is out of range

when i try to access the table to get the heights in the chunk generator.

Here are the two scripts in question:

Script 1 : The Terrain Manager

This script is supposed to generate the chunks next to eachother and it does it quite well.

void Start () {
    chunks = new Chunk[renderArea * renderArea];
    worldHeightmap = PerlinNoise.GenerateHightmap(chunkSize * worldSize + 1, chunkHeight * worldSize + 1, worldSeed, worldSeedIntensity);       
    print(worldHeightmap.Length);

    RVector3 chunkDimensions = new RVector3(chunkSize, chunkHeight, chunkSize);

    for(int x = 1; x < renderArea + 1; x++) {
        for(int z = 1; z < renderArea + 1; z++) {
            Vector3 spawnPos = new Vector3(x * chunkSize, 0, z * chunkSize);
            // Ignore this float [,] heightMapPeice = new float[x * chunkSize, z * chunkSize];

            // Creates Chunk //
            GameObject chunkHolder = Instantiate(block, block.transform.position, Quaternion.identity);
            chunk = chunkHolder.GetComponent<Chunk>();

            chunk.GenerateChunk(chunkDimensions, spawnPos, worldHeightmap, new Vector2(x, z));
            chunks[chunkIndex] = chunk;
            chunkIndex++;
        }
    }

    print(chunks.Length + " Chunks Generated");
}

Script 2 : The Chunk Generator

public void GenerateChunk (RVector3 chunkDimensions, Vector3 spawnPosition, float[,] chunkHeights, Vector2 heightIndex) {
    chunkSize = chunkDimensions;
    chunkBlocks = new Block[chunkSize.x + 1, chunkSize.y + 1, chunkSize.z + 1];

    transform.position = spawnPosition;

    for(int x = 0; x <= chunkSize.x; x++) {
        for(int y = 0; y <= chunkSize.y; y++) {
            for(int z = 0; z <= chunkSize.z; z++) {
                chunkBlocks[x,y,z] = new Block(true);

                //print(x * (int) heightIndex.x);

                if(y < chunkHeights[x * chunkSize, z * chunkSize]) { 
                    chunkBlocks[x,y,z] = new Block(false);
                }
            }
        }
    }

    updateChunk();
}

If i could get some help on this it would be greatly appreciated. Thank you very much <3.

RyanPwn
  • 19
  • 5

1 Answers1

0

It looks to me like your problem stems from the ranges of the following 3 for loops:

for(int x = 0; x <= chunkSize.x; x++) {
        for(int y = 0; y <= chunkSize.y; y++) {
            for(int z = 0; z <= chunkSize.z; z++) {

You're iterating from 0 to chunkSize.<axis>, giving you a total of chunkSize.<axis> + 1 total iterations per axis, when I can only assume you would want only chunkSize.<axis>. You would solve this by using < instead of <=

for(int x = 0; x < chunkSize.x; x++) {
        for(int y = 0; y < chunkSize.y; y++) {
            for(int z = 0; z < chunkSize.z; z++) {
Julxzs
  • 737
  • 1
  • 7
  • 21