Question by
Marotinnn · Oct 02, 2017 at 03:00 AM ·
scripting problem
Logic problem with my Random Terrain Generator Script
Hi, i am studying how to make a randomized terrains and load them by using chunk logic, like in minecraft. I got stuck with an array out of index error. Here is my code:
## Map Generator Class ##
public class MapGenerator : MonoBehaviour {
public Transform tilePrefab;
public int mapWidth;
public int mapHeight;
public int seed;
public int chunkSize;
public int renderDistance;
// Use this for initialization
void Start () {
GenerateMap();
}
public void GenerateMap()
{
string holderName = "Generated Terrain";
if (transform.FindChild(holderName))
{
DestroyImmediate(transform.FindChild(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
Chunk[,] renderMap = Chunk.GenerateRenderMap(mapWidth, mapHeight, seed, renderDistance, chunkSize);
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
for(int i = 0; i < renderMap[x, y].tiles.Length; i++)
{
Transform newTile = Instantiate(tilePrefab, renderMap[x,y].tiles[0].position, Quaternion.Euler(Vector3.right * 90)).transform;
newTile.localScale = Vector3.one;
newTile.GetComponent<Renderer>().sharedMaterial.mainTexture = renderMap[x, y].tiles[0].texture;
newTile.GetComponent<Renderer>().transform.localScale = new Vector3(renderMap[x, y].tiles[0].texture.width, renderMap[x, y].tiles[0].texture.height);
newTile.parent = mapHolder;
}
}
}
}
}
## Chunk Class##
public class Chunk{
public Tile[] tiles;
public Chunk(Tile[] tiles)
{
this.tiles = tiles;
}
public static Chunk[,] GenerateRenderMap(int mapWidth, int mapHeight, int seed, int renderDistance, int chunkSize)
{
int arrayDSize = renderDistance ^ 2;
Chunk[,] renderMap = new Chunk[arrayDSize,arrayDSize];
for(int y = 0; y < arrayDSize; y++)
{
for(int x = 0; x < arrayDSize; x++)
{
renderMap[x, y] = new Chunk(Tile.CreateChunk((mapWidth / 2) + (chunkSize * x), (mapHeight / 2) + (chunkSize * y), mapWidth, mapHeight, seed, chunkSize));
}
}
return renderMap;
}
}
## Tile Class ##
public class Tile {
public Vector3 position;
public Texture2D texture;
public Tile(Vector3 position, Texture2D texture)
{
this.position = position;
this.texture = texture;
}
public static Tile[] CreateChunk(int cordX, int cordY, int mapWidth, int mapHeight, int seed, int chunkSize)
{
ushort[,] tileMap = World.GenerateWorld(mapWidth, mapHeight, seed);
Tile[] chunks = new Tile[chunkSize];
for(int y = cordY - chunkSize, x = cordX - chunkSize; y < chunkSize; y++, x++)
{
Vector3 tilePosition = new Vector3(cordX, 0, cordY);
Texture2D texture = new Texture2D(1, 1);
Color[] color = new Color[1];
texture.SetPixels(color);
if(tileMap[cordX,cordY] == 0)
{
color[0] = Color.blue;
}else
{
color[0] = Color.green;
}
texture.Apply();
chunks[x] = new Tile(tilePosition, texture);
}
return chunks;
}
}
## World Data Class##
public static class World{
public static ushort[,] GenerateWorld(int mapWidth, int mapHeight, int seed)
{
ushort[,] tileMap = new ushort[mapWidth, mapHeight];
System.Random prng = new System.Random(seed); //Pseudo Random Number Generator
for(int y = 0; y < mapHeight; y++)
{
for(int x = 0; x < mapWidth; x++)
{
tileMap[x, y] = (ushort)prng.Next(4);
}
}
return tileMap;
}
}
What is wrong with my logic? how can i fix my code?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612140108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED]My OnMuseDown execute only once 1 Answer
First person shooter : Leaning! 2 Answers
NavMesh of an automatically generated maze 0 Answers
example of new way to load levels 1 Answer
Menu object not responding 0 Answers