Hello everyone, I’m making a procedural game that has a chunk system but I want to put water in a certain chunk. How can I do that?
Hello!
This is way too vague and hard to answer without knowing what you currently have, how your game is generated, and what you mean by “putting water”.
If your water is standard “everything below y=0 is water”, one simple trick is to have a plane that actually moves with the player and is always located at y=0. That way if the shader is in world space, you wouldn’t notice it moving and it would automatically cover anything below 0.
I wanted to lower the vertices so I could put the water prefab where it was lowered
Are you using the Unity terrain or a custom mesh?
If you’re using perlin noise, it can be as simple as feeding your Mathf.Perlin output into the terrain height (be it a mesh or the unity terrain), then placing the prefab below a certain threshold.
Again I’m kind of throwing random suggestions here, it’s quite hard to give advice without knowing the full context of your code
I’m using custom mesh to make the terrain I’ll send some code
public void GenerateChunk(Chunk chunk, ChunkData chunkData)
{
float xPosition = 0f;
float zPosition = 0f;
float yPosition = 0f;
int xInt = 0;
int zInt = 0;
this.Vertices = new Vector3[this.ChunkSize.x, this.ChunkSize.z];
for (int x = 0; x < this.ChunkSize.x; x++)
{
for (int z = 0; z < this.ChunkSize.z; z++)
{
float positionX = x + this.ChunkStartPosition.x * (this.ChunkSize.x - 1f);
float positionZ = z + this.ChunkStartPosition.z * (this.ChunkSize.z - 1f);
xPosition = positionX;
zPosition = positionZ;
float y = this.GetSurfaceHeight(positionX, positionZ, this.HeightMax);
float positionY = y * this.HeightMax;
yPosition = positionY;
this.Vertices[x, z] = new Vector3(positionX, positionY, positionZ);
xInt = x;
zInt = z;
}
}
base.StartCoroutine(this.GenerateVegetation(chunkData, chunk, xPosition, yPosition, zPosition));
for (int x = 0; x < this.ChunkSize.x - 1; x++)
{
for (int z = 0; z < this.ChunkSize.z - 1; z++)
{
this.Triangles.Add(this.VerticesList.Count);
this.Triangles.Add(this.VerticesList.Count + 2);
this.Triangles.Add(this.VerticesList.Count + 1);
this.Triangles.Add(this.VerticesList.Count + 2);
this.Triangles.Add(this.VerticesList.Count + 3);
this.Triangles.Add(this.VerticesList.Count + 1);
this.VerticesList.Add(this.Vertices[x, z]);
this.VerticesList.Add(this.Vertices[x + 1, z]);
this.VerticesList.Add(this.Vertices[x, z + 1]);
this.VerticesList.Add(this.Vertices[x + 1, z + 1]);
}
}
chunk.Vertices = this.VerticesList.ToArray();
chunk.GetComponent<MeshRenderer>().sharedMaterial = this.TerrainMaterial;
chunk.GetComponent<MeshFilter>().mesh = this.Mesh;
chunk.GetComponent<MeshCollider>().sharedMesh = this.Mesh;
this.UpdateMesh();
this.VerticesList.Clear();
this.Triangles.Clear();
}
So it looks like you’re already generating and getting the terrain height:
float y = this.GetSurfaceHeight(positionX, positionZ, this.HeightMax);
float positionY = y * this.HeightMax;
If you want the ENTIRE chunk to be water, you would probably do something like replacing that with 0, so positionY = 0, then Instantiate your water prefab and potentially place it at the centre of the Chunk (assuming your water prefab is a plane with its pivot in its centre).
yes my water prefab is a plane but i wanted to lower the vertices to place the water