I spend 6 hours trying fix this code
but no result
help pls error:
IndexOutOfRangeException: Index was outside the bounds of the array.
TerrainGeneration.PlaceTile (UnityEngine.Sprite tileSprite, System.Int32 x, System.Int32 y) (at Assets/TerrainGeneration.cs:186)
TerrainGeneration.GenerateTerrain () (at Assets/TerrainGeneration.cs:115)
TerrainGeneration.Start () (at Assets/TerrainGeneration.cs:64)
in this video generating 5 chunks with index 0,1,2,3,4
↓
code from this video:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGeneration : MonoBehaviour
{
[Header(" World Settings")]
[Header(" ")]
[Header(" General World Settings")]
public float seed;
public int worldSize = 100;
public int treeChance = 10;
public bool generateCaves = true;
[Header(" ")]
[Header(" Other World Settings")]
public int minTreeHeight = 5;
public int maxTreeHeight = 7;
public int dirtLayerHeight = 5;
public float surfaceValue = 0.25f;
public float caveFreq = 0.08f;
public float terrainFreq = 0.04f;
public float heightMultiplier = 25f;
public int heightAddition = 25;
public int chunkSize = 16;
[Header(" ")]
[Header(" Noise")]
public Texture2D noiseTexture;
[Header(" ")]
[Header(" Texture Settings")]
[Header(" ")]
public Sprite leaf;
public Sprite log;
public Sprite grass;
public Sprite dirt;
public Sprite stone;
[Header("?")]
public GameObject[] worldChunks;
public List<Vector2> worldTiles = new List<Vector2>();
private void Start()
{
if (seed == 0)
{
seed = Random.Range(-10000, 10000);
GenerateNoiseTexture();
CreateChunks();
GenerateTerrain();
}
else
{
GenerateNoiseTexture();
CreateChunks();
GenerateTerrain();
}
}
public void CreateChunks()
{
int numChunks = worldSize / chunkSize;
worldChunks = new GameObject[numChunks];
for (int i = 0; i < numChunks; i++)
{
GameObject newChunk = new GameObject();
newChunk.name = i.ToString();
newChunk.transform.parent = this.transform;
worldChunks[i] = newChunk;
}
}
public void GenerateTerrain()
{
for (int x = 0; x < worldSize; x++)
{
float height = Mathf.PerlinNoise((x + seed) * terrainFreq, seed * terrainFreq) * heightMultiplier + heightAddition;
for (int y = 0; y < height; y++)
{
Sprite tileSprite;
if (y < height - dirtLayerHeight)
{
tileSprite = stone;
}
else if (y < height - 1)
{
tileSprite = dirt;
}
else
{
tileSprite = grass;
}
if (generateCaves)
{
if (noiseTexture.GetPixel(x, y).r > surfaceValue)
{
PlaceTile(tileSprite, x, y);
}
}
else
{
PlaceTile(tileSprite, x, y);
}
if (y >= height - 1)
{
int t = Random.Range(0, treeChance);
if (t == 1)
{
if (worldTiles.Contains(new Vector2(x, y)))
{
GenerateTree(x, y + 1);
}
}
}
}
}
}
public void GenerateNoiseTexture()
{
noiseTexture = new Texture2D(worldSize, worldSize);
for (int x = 0; x < noiseTexture.width; x++)
{
for (int y = 0; y < noiseTexture.height; y++)
{
float v = Mathf.PerlinNoise((x + seed) * caveFreq, (y + seed) * caveFreq);
noiseTexture.SetPixel(x, y,new Color(v, v, v));
}
}
noiseTexture.Apply();
}
public void GenerateTree(int x, int y)
{
int treeHeight = Random.Range(minTreeHeight, maxTreeHeight);
for (int i = 0; i < treeHeight; i++)
{
PlaceTile(log, x, y + i);
}
//1
PlaceTile(leaf, x - 1, y + treeHeight);
PlaceTile(leaf, x, y + treeHeight);
PlaceTile(leaf, x + 1, y + treeHeight);
//2
PlaceTile(leaf, x - 1, y + treeHeight - 1);
PlaceTile(leaf, x, y + treeHeight - 1);
PlaceTile(leaf, x + 1, y + treeHeight - 1);
//3
PlaceTile(leaf, x - 2, y + treeHeight - 2);
PlaceTile(leaf, x - 1, y + treeHeight - 2);
PlaceTile(leaf, x, y + treeHeight - 2);
PlaceTile(leaf, x + 1, y + treeHeight - 2);
PlaceTile(leaf, x + 2, y + treeHeight - 2);
//4
PlaceTile(leaf, x - 2, y + treeHeight - 3);
PlaceTile(leaf, x - 1, y + treeHeight - 3);
PlaceTile(leaf, x, y + treeHeight - 3);
PlaceTile(leaf, x + 1, y + treeHeight - 3);
PlaceTile(leaf, x + 2, y + treeHeight - 3);
}
public void PlaceTile(Sprite tileSprite, int x, int y)
{
GameObject newTile = new GameObject();
float chunkCoord = (Mathf.Round(x / chunkSize));
Debug.Log(chunkCoord);
newTile.transform.parent = worldChunks[(int)chunkCoord].transform;
newTile.AddComponent<SpriteRenderer>();
newTile.GetComponent<SpriteRenderer>().sprite = tileSprite;
newTile.name = tileSprite.name;
newTile.transform.position = new Vector2(x + 0.5f, y + 0.5f);
worldTiles.Add(newTile.transform.position - (Vector3.one * 0.5f));
}
}