Hey guys. I have this error with my perlin noise scripts. Where either the game objects overlap
or they multiply he distance between the game objects by 2.
Here is the code for the perlin noise script:
public class PrefabNoise : MonoBehaviour
{
public int mapSize = 50;
public GameObject grass, water, tree;
// public float threshold = 0.5f, scalepublic, frequency = 0.05f, amplitude = 1f, treeFrequency, treeAmplitude;
public int seed;
public int chunkSize = 64;
public float chunkDistance = 32f;
private float offsetX, offsetY;
private GameObject player;
private List<Vector2> GrassVected = new List<Vector2>();
private Dictionary<Vector2, Chunk> chunks = new Dictionary<Vector2, Chunk>();
private float x = 0, y = 0;
void Awake()
{
Random.InitState(seed);
}
void Start()
{
player = GameObject.Find("Player");
}
private void Update()
{
Vector3 playerPos = player.transform.position;
int currentChunkX = Mathf.CeilToInt(playerPos.x / chunkSize);
int currentChunkY = Mathf.CeilToInt(playerPos.y / chunkSize);
for (int x = currentChunkX - 1; x <= currentChunkX + 1; x++)
{
for (int y = currentChunkY - 1; y <= currentChunkY + 1; y++)
{
Vector2 chunkCoord = new Vector2(x, y);
if (!chunks.ContainsKey(chunkCoord))
{
float[,] noiseMap = new float[chunkSize, chunkSize];
int mapPosY = 0;
int mapPosX = 0;
for (int i = 0; i < chunkSize; i++)
{
for (int j = 0; j < chunkSize; j++)
{
float xCoord = (float)(i + chunkCoord.x * chunkSize) / (float)chunkSize + offsetX; // added offsetX
float yCoord = (float)(j + chunkCoord.y * chunkSize) / (float)chunkSize + offsetY; // added offsetY
noiseMap[i, j] = Mathf.PerlinNoise(xCoord, yCoord);
mapPosX = (int)xCoord;
mapPosY = (int)yCoord;
}
}
GameObject chunkObject = new GameObject("Chunk (" + x + ", " + y + ")");
chunkObject.transform.position = new Vector3(x, y, 0f);
Chunk chunk = chunkObject.AddComponent<Chunk>();
chunk.noiseMap = noiseMap;
chunk.transform.parent = transform;
chunk.mapPosX = chunkCoord.x;
chunk.mapPosY = chunkCoord.y;
chunks.Add(chunkCoord, chunk);
}
}
}
}
}
And here is the code for the chunk script:
public class Chunk : MonoBehaviour
{
public float mapSize = 50, mapPosX, mapPosY;
public GameObject grass, water, tree;
public float threshold = 0.5f, scalepublic, frequency = 0.05f, amplitude = 1f, treeFrequency, treeAmplitude;
public int seed;
private List<Vector2> GrassVected = new List<Vector2>();
public float[,] noiseMap;
void Start()
{
water = GameObject.Find("Water");
tree = GameObject.Find("Tree");
grass = GameObject.Find("Grass");
GenerateTerrain();
}
private void GenerateTerrain()
{
for (int x = 0; x < noiseMap.GetLength(0); x++)
{
for (int y = 0; y < noiseMap.GetLength(1); y++)
{
float noiseValue = noiseMap [x,y];
if (noiseValue > threshold)
{
GrassVected.Add(new Vector2(x, y));
Vector3 position = new Vector3(x * mapPosX, y * mapPosY, 0f);
GameObject b = Instantiate(grass, position, Quaternion.identity);
b.transform.parent = transform;
}
else
{
Vector3 position = new Vector3(x *mapPosX, y * mapPosY, 0f);
GameObject b = Instantiate(water, position, Quaternion.identity);
b.transform.parent = transform;
}
}
}
}
}
I cant figure out how to add images from my computer to show you guys what it looks like. But I hope you can still help me with this.
Thanks in advance