Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question how to make a hole to put water with perlin noise

Discussion in 'Editor & General Support' started by GamesStudioDreams, Aug 11, 2023.

  1. GamesStudioDreams

    GamesStudioDreams

    Joined:
    Jan 1, 2023
    Posts:
    15
    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?
     
  2. evyatron

    evyatron

    Joined:
    Jul 20, 2014
    Posts:
    132
    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.
     
  3. GamesStudioDreams

    GamesStudioDreams

    Joined:
    Jan 1, 2023
    Posts:
    15
    I wanted to lower the vertices so I could put the water prefab where it was lowered
     
  4. evyatron

    evyatron

    Joined:
    Jul 20, 2014
    Posts:
    132
    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 :)
     
  5. GamesStudioDreams

    GamesStudioDreams

    Joined:
    Jan 1, 2023
    Posts:
    15
    I'm using custom mesh to make the terrain I'll send some code

    Code (CSharp):
    1.     public void GenerateChunk(Chunk chunk, ChunkData chunkData)
    2.     {
    3.         float xPosition = 0f;
    4.         float zPosition = 0f;
    5.         float yPosition = 0f;
    6.         int xInt = 0;
    7.         int zInt = 0;
    8.         this.Vertices = new Vector3[this.ChunkSize.x, this.ChunkSize.z];
    9.         for (int x = 0; x < this.ChunkSize.x; x++)
    10.         {
    11.             for (int z = 0; z < this.ChunkSize.z; z++)
    12.             {
    13.                 float positionX =  x + this.ChunkStartPosition.x * (this.ChunkSize.x - 1f);
    14.                 float positionZ =  z + this.ChunkStartPosition.z * (this.ChunkSize.z - 1f);
    15.                 xPosition = positionX;
    16.                 zPosition = positionZ;
    17.                 float y = this.GetSurfaceHeight(positionX, positionZ, this.HeightMax);
    18.                 float positionY = y * this.HeightMax;
    19.                 yPosition = positionY;
    20.                 this.Vertices[x, z] = new Vector3(positionX, positionY, positionZ);
    21.                 xInt = x;
    22.                 zInt = z;
    23.             }
    24.         }
    25.         base.StartCoroutine(this.GenerateVegetation(chunkData, chunk, xPosition, yPosition, zPosition));
    26.         for (int x = 0; x < this.ChunkSize.x - 1; x++)
    27.         {
    28.             for (int z = 0; z < this.ChunkSize.z - 1; z++)
    29.             {
    30.                 this.Triangles.Add(this.VerticesList.Count);
    31.                 this.Triangles.Add(this.VerticesList.Count + 2);
    32.                 this.Triangles.Add(this.VerticesList.Count + 1);
    33.                 this.Triangles.Add(this.VerticesList.Count + 2);
    34.                 this.Triangles.Add(this.VerticesList.Count + 3);
    35.                 this.Triangles.Add(this.VerticesList.Count + 1);
    36.                 this.VerticesList.Add(this.Vertices[x, z]);
    37.                 this.VerticesList.Add(this.Vertices[x + 1, z]);
    38.                 this.VerticesList.Add(this.Vertices[x, z + 1]);
    39.                 this.VerticesList.Add(this.Vertices[x + 1, z + 1]);
    40.             }
    41.         }
    42.         chunk.Vertices = this.VerticesList.ToArray();
    43.         chunk.GetComponent<MeshRenderer>().sharedMaterial = this.TerrainMaterial;
    44.         chunk.GetComponent<MeshFilter>().mesh = this.Mesh;
    45.         chunk.GetComponent<MeshCollider>().sharedMesh = this.Mesh;
    46.         this.UpdateMesh();
    47.         this.VerticesList.Clear();
    48.         this.Triangles.Clear();
    49.     }
     
  6. evyatron

    evyatron

    Joined:
    Jul 20, 2014
    Posts:
    132
    So it looks like you're already generating and getting the terrain height:
    Code (CSharp):
    1.                 float y = this.GetSurfaceHeight(positionX, positionZ, this.HeightMax);
    2.                 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).
     
  7. GamesStudioDreams

    GamesStudioDreams

    Joined:
    Jan 1, 2023
    Posts:
    15
    yes my water prefab is a plane but i wanted to lower the vertices to place the water