Hello everyone, i am trying to make a multiplayer adventure game and want to have procedural terrain generation. The script I created for this is capable to create a single chunk, the broblem comes when i try to create side by side chunks that dont match. here is the code im using:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Create_Mesh : MonoBehaviour
{
public int width;
public int height;
public int xPos;
public int yPos;
public float scale;
public float multiplier;
public float yOffset;
[HideInInspector]
public float xOffset;
[HideInInspector]
public float zOffset;
public bool roundHeight;
public int chunkAmount;
// Create a plane;
public List verts = new List();
public List tris = new List();
Mesh mesh;
MeshFilter mF;
void Start ()
{
mF = gameObject.AddComponent();
gameObject.AddComponent();
mesh = new Mesh();
mF.mesh = mesh;
gameObject.AddComponent();
CalculateTerrainAtPos(Vector3.zero);
CalculateTerrainAtPos(Vector3.left * 16);
CalculateTerrainAtPos(Vector3.left * 32);
}
void CalculateTerrainAtPos (Vector3 pos)
{
xOffset = width / 2;
zOffset = height / 2;
for (int y = -height/2; y < height/2; y++)
{
for (int x = -width/2; x < width/2; x++)
{
float xCoord = xPos + x;
float yCoord = yPos + y;
float sample = Mathf.PerlinNoise(pos.x + x / scale, pos.z + y / scale) * multiplier;
GameObject clone = GameObject.CreatePrimitive(PrimitiveType.Cube);
clone.name = "Cube_" + (y * width + x);
clone.transform.position = new Vector3(xCoord - xOffset + pos.x, Mathf.RoundToInt(sample) - yOffset + pos.y, yCoord - zOffset+ pos.z);
}
}
}
}
I know the script has some useless variables but im just trying to make it work before i clean it up a little. I think the solution is prety simple but i just cant find it. So if you have any suggestion or solutions pls tell me, this is driving me nuts. thanks a lot to everyone.
↧