I’m making a map out of blocks using perlin noise, the only problem is that the perlin noise doesn’t change, so the map always stays the same, and I’d like to know how to fix that, heres the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateInfiniteWorld : MonoBehaviour
{
public GameObject player;
public GameObject blockGameObject;
public int worldSizeX;
public int worldSizeZ;
public float noiseHeight;
public float gridOffset;
private Vector3 startPosition;
private Hashtable blockContainer = new Hashtable();
private List<Vector3> blockPositions = new List<Vector3>();
void Start()
{
for (int x = -worldSizeX - 25; x < worldSizeX + 25; x++)
{
for(int z = -worldSizeZ - 25; z < worldSizeZ + 25; z++)
{
Vector3 pos = new Vector3(x * 1 + startPosition.x, generateNoise(x + xPlayerLocation, z + zPlayerLocation, 8f) * noiseHeight, z * 1 + startPosition.z);
GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;
blockContainer.Add(pos, block);
blockPositions.Add(block.transform.position);
block.transform.SetParent(this.transform);
}
}
}
private void Update()
{
if(Mathf.Abs(xPlayerMove) >= 1 || Mathf.Abs(zPlayerMove) >= 1)
{
for (int x = -worldSizeX; x < worldSizeX; x++)
{
for (int z = -worldSizeZ; z < worldSizeZ; z++)
{
Vector3 pos = new Vector3(x * 1 + xPlayerLocation, generateNoise(x + xPlayerLocation, z + zPlayerLocation, 8f) * noiseHeight, z * 1 + zPlayerLocation);
if (!blockContainer.ContainsKey(pos))
{
GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;
blockContainer.Add(pos, block);
blockPositions.Add(block.transform.position);
block.transform.SetParent(this.transform);
}
}
}
}
}
public int xPlayerMove
{
get
{
return (int)(player.transform.position.x - startPosition.x);
}
}
public int zPlayerMove
{
get
{
return (int)(player.transform.position.z - startPosition.z);
}
}
public int xPlayerLocation
{
get
{
return (int)Mathf.Floor(player.transform.position.x);
}
}
public int zPlayerLocation
{
get
{
return (int)Mathf.Floor(player.transform.position.z);
}
}
private Vector3 ObjectSpawnLocation()
{
int rndIndex = Random.Range(0, blockPositions.Count);
Vector3 newPos = new Vector3(
blockPositions[rndIndex].x,
blockPositions[rndIndex].y + 0.5f,
blockPositions[rndIndex].z
);
blockPositions.RemoveAt(rndIndex);
return newPos;
}
private Vector3 ObjectSpawnLocation2()
{
int rndIndex = blockPositions.Count / 2;
Vector3 newPos = new Vector3(
blockPositions[rndIndex].x,
blockPositions[rndIndex].y,
blockPositions[worldSizeX / 2].z
);
blockPositions.RemoveAt(rndIndex);
return newPos;
}
private Vector3 ObjectSpawnLocation3()
{
int rndIndex = Random.Range(0, blockPositions.Count);
Vector3 newPos = new Vector3(
blockPositions[rndIndex].x,
blockPositions[rndIndex].y + 0.5f,
blockPositions[rndIndex].z
) ;
blockPositions.RemoveAt(rndIndex);
return newPos;
}
private float generateNoise(int x, int z, float detailScale)
{
float xNoise = (x + this.transform.position.x) / detailScale;
float zNoise = (z + this.transform.position.y) / detailScale;
return Mathf.PerlinNoise(xNoise, zNoise);
}
}