Search Unity

Resolved Perlin Noise Generation Script Holes in Terrain (2d Game)

Discussion in 'Scripting' started by EnderSkelly34, Aug 16, 2021.

  1. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    Hello, I am making a new unity 2d game. I am using perlin noise with random seeds in order to generate terrain and plants. Unfortunately, there are holes in my terrain when it is generated. Any suggestions? Pictures and script down below.


    What it looks like (Unwanted Holes):
    Captur1e.PNG
    Public Variables I assigned in Inspector:
    Capture.PNG

    My World Generation Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using UnityEditor;
    6.  
    7. public class WorldGeneration : MonoBehaviour
    8. {
    9.  
    10.     public Tilemap tileMap;
    11.     public TileBase[] forestTiles;
    12.     public TileBase[] desertTiles;
    13.     public GameObject[] forestVegetation;
    14.     public GameObject[] desertVegetation;
    15.  
    16.     public float perlin;
    17.  
    18.     public int xOffset;
    19.     public int yOffset;
    20.  
    21.     public float width;
    22.     public float height;
    23.  
    24.     public float vegetationChance;
    25.  
    26.     private void Start()
    27.     {
    28.  
    29.         generateWorld();
    30.  
    31.     }
    32.  
    33.     public void generateWorld()
    34.     {
    35.        
    36.         for (int x = xOffset; x <= width / 2; x++)
    37.         {
    38.  
    39.             for (int y = yOffset; y <= height / 2; y++)
    40.             {
    41.  
    42.                 float random = Random.Range(0f, 1f);
    43.                 float rainFall = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    44.                 float temperature = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    45.  
    46.                 if (vegetationChance >= random)
    47.                 {
    48.  
    49.                     generateWithVegetation(x, y, temperature, rainFall);
    50.  
    51.                 } else {
    52.  
    53.                     generateWithoutVegetation(x, y, temperature, rainFall);
    54.  
    55.                 }
    56.  
    57.             }
    58.  
    59.         }
    60.  
    61.     }
    62.  
    63.     void generateWithoutVegetation(int x, int y, float temperature, float rainFall)
    64.     {
    65.  
    66.         perlin = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    67.  
    68.         if (temperature <= 0.75f && rainFall >= 0.15f) {
    69.  
    70.             perlin *= forestTiles.Length - 1;
    71.             int tileNum = Mathf.RoundToInt(perlin);
    72.  
    73.             tileMap.SetTile(new Vector3Int(x, y, 0), forestTiles[tileNum]);
    74.  
    75.         } else if (temperature > 0.75f && rainFall < 0.15f) {
    76.  
    77.             perlin *= desertTiles.Length - 1;
    78.             int tileNum = Mathf.RoundToInt(perlin);
    79.  
    80.             tileMap.SetTile(new Vector3Int(x, y, 0), desertTiles[tileNum]);
    81.  
    82.         }
    83.  
    84.     }
    85.  
    86.     void generateWithVegetation(int x, int y, float temperature, float rainFall)
    87.     {
    88.  
    89.         perlin = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    90.  
    91.         if (temperature <= 0.75f && rainFall >= 0.15f) {
    92.  
    93.             perlin *= forestTiles.Length - 1;
    94.             int tileNum = Mathf.RoundToInt(perlin);
    95.  
    96.             tileMap.SetTile(new Vector3Int(x, y, 0), forestTiles[tileNum]);
    97.             GameObject plant = Instantiate(forestVegetation[Random.Range(0, forestVegetation.Length)], new Vector3(x + 0.5f, y + 5.15f, 0), Quaternion.identity);
    98.             plant.transform.parent = gameObject.transform.GetChild(0);
    99.  
    100.         } else if (temperature > 0.75f && rainFall < 0.15f) {
    101.  
    102.             perlin *= desertTiles.Length - 1;
    103.             int tileNum = Mathf.RoundToInt(perlin);
    104.  
    105.             tileMap.SetTile(new Vector3Int(x, y, 0), desertTiles[tileNum]);
    106.             GameObject plant = Instantiate(desertVegetation[Random.Range(0, desertVegetation.Length)], new Vector3(x, y, 0), Quaternion.identity);
    107.             plant.transform.parent = gameObject.transform.GetChild(0);
    108.  
    109.         }
    110.  
    111.     }
    112.  
    113. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    Doing what you did, It is running it as many times as I want it. The only problem is that desert happens rarely. About 2 times, but I still do not know why it has holes.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    At line 109 above, put an else and throw an error. Is that your hole?

    You have to do something to find out what the hole is. Either fix your inputs so you can recreate the same stage again and again and then start debugging it at that specific x/y location where the hole is, or else start putting extra error handling for ALL the different ways through the code that might not create something.
     
  5. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    Simplified my code But still has holes. Hmmmmm

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using UnityEditor;
    6.  
    7. public class WorldGeneration : MonoBehaviour
    8. {
    9.  
    10.     public Tilemap tileMap;
    11.     public TileBase[] forestTiles;
    12.     public TileBase[] desertTiles;
    13.     public GameObject[] forestVegetation;
    14.     public GameObject[] desertVegetation;
    15.  
    16.     public float perlin;
    17.  
    18.     public int xOffset;
    19.     public int yOffset;
    20.  
    21.     public float width;
    22.     public float height;
    23.  
    24.     public float vegetationChance;
    25.  
    26.     private void Start()
    27.     {
    28.  
    29.         generateWorld();
    30.  
    31.     }
    32.  
    33.     public void generateWorld()
    34.     {
    35.        
    36.         for (int x = xOffset; x < width / 2; x++)
    37.         {
    38.  
    39.             for (int y = yOffset; y < height / 2; y++)
    40.             {
    41.  
    42.                
    43.                 float rainFall = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    44.                 float temperature = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    45.  
    46.                 generateTile(x, y, temperature, rainFall);
    47.  
    48.             }
    49.  
    50.         }
    51.  
    52.     }
    53.  
    54.     void generateTile(int x, int y, float temperature, float rainFall)
    55.     {
    56.  
    57.         Debug.Log("Generating Tile");
    58.  
    59.         perlin = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    60.  
    61.         float random = Random.Range(0f, 1f);
    62.  
    63.         if (temperature <= 0.75f && rainFall >= 0.15f) {
    64.  
    65.             if(random <= vegetationChance)
    66.             {
    67.  
    68.                 perlin *= forestTiles.Length - 1;
    69.                 int tileNum = Mathf.RoundToInt(perlin);
    70.  
    71.                 tileMap.SetTile(new Vector3Int(x, y, 0), forestTiles[tileNum]);
    72.                 GameObject plant = Instantiate(forestVegetation[Random.Range(0, forestVegetation.Length)], new Vector3(x + 0.5f, y + 5.15f, 0), Quaternion.identity);
    73.                 plant.transform.parent = gameObject.transform.GetChild(0);
    74.  
    75.             } else {
    76.  
    77.                 perlin *= forestTiles.Length - 1;
    78.                 int tileNum = Mathf.RoundToInt(perlin);
    79.  
    80.                 tileMap.SetTile(new Vector3Int(x, y, 0), forestTiles[tileNum]);
    81.  
    82.             }
    83.  
    84.         } else if (temperature > 0.75f && rainFall < 0.15f) {
    85.  
    86.  
    87.             if (random <= vegetationChance)
    88.             {
    89.  
    90.                 perlin *= desertTiles.Length - 1;
    91.                 int tileNum = Mathf.RoundToInt(perlin);
    92.  
    93.                 tileMap.SetTile(new Vector3Int(x, y, 0), desertTiles[tileNum]);
    94.                 GameObject plant = Instantiate(desertVegetation[Random.Range(0, desertVegetation.Length)], new Vector3(x, y, 0), Quaternion.identity);
    95.                 plant.transform.parent = gameObject.transform.GetChild(0);
    96.  
    97.             } else {
    98.  
    99.                 perlin *= desertTiles.Length - 1;
    100.                 int tileNum = Mathf.RoundToInt(perlin);
    101.  
    102.                 tileMap.SetTile(new Vector3Int(x, y, 0), desertTiles[tileNum]);
    103.  
    104.             }
    105.  
    106.         }
    107.  
    108.     }
    109.  
    110. }
    111.  
     
  6. EnderSkelly34

    EnderSkelly34

    Joined:
    Jun 13, 2020
    Posts:
    27
    Heyyyy! Using your ideas I fixed it!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using UnityEditor;
    6.  
    7. public class WorldGeneration : MonoBehaviour
    8. {
    9.  
    10.     public Tilemap tileMap;
    11.     public TileBase[] forestTiles;
    12.     public TileBase[] desertTiles;
    13.     public GameObject[] forestVegetation;
    14.     public GameObject[] desertVegetation;
    15.  
    16.     public float perlin;
    17.  
    18.     public int xOffset;
    19.     public int yOffset;
    20.  
    21.     public float width;
    22.     public float height;
    23.  
    24.     public float vegetationChance;
    25.  
    26.     private void Start()
    27.     {
    28.  
    29.         generateWorld();
    30.  
    31.     }
    32.  
    33.     public void generateWorld()
    34.     {
    35.        
    36.         for (int x = xOffset; x < width / 2; x++)
    37.         {
    38.  
    39.             for (int y = yOffset; y < height / 2; y++)
    40.             {
    41.  
    42.                
    43.                 float rainFall = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    44.                 float temperature = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    45.  
    46.                 generateTile(x, y, temperature, rainFall);
    47.  
    48.             }
    49.  
    50.         }
    51.  
    52.     }
    53.  
    54.     void generateTile(int x, int y, float temperature, float rainFall)
    55.     {
    56.  
    57.         Debug.Log("Generating Tile");
    58.  
    59.         perlin = Mathf.PerlinNoise(x + Random.value, y + Random.value);
    60.  
    61.         float random = Random.Range(0f, 1f);
    62.  
    63.         if (temperature <= 0.75f && rainFall >= 0.15f || temperature > 0.75f && rainFall >= 0.15f) {
    64.  
    65.             if(random <= vegetationChance)
    66.             {
    67.  
    68.                 perlin *= forestTiles.Length - 1;
    69.                 int tileNum = Mathf.RoundToInt(perlin);
    70.  
    71.                 tileMap.SetTile(new Vector3Int(x, y, 0), forestTiles[tileNum]);
    72.                 GameObject plant = Instantiate(forestVegetation[Random.Range(0, forestVegetation.Length)], new Vector3(x + 0.5f, y + 5.15f, 0), Quaternion.identity);
    73.                 plant.transform.parent = gameObject.transform.GetChild(0);
    74.  
    75.             } else {
    76.  
    77.                 perlin *= forestTiles.Length - 1;
    78.                 int tileNum = Mathf.RoundToInt(perlin);
    79.  
    80.                 tileMap.SetTile(new Vector3Int(x, y, 0), forestTiles[tileNum]);
    81.  
    82.             }
    83.  
    84.         } else if (temperature > 0.75f && rainFall < 0.15f || temperature <= 0.75f && rainFall < 0.15f) {
    85.  
    86.  
    87.             if (random <= vegetationChance)
    88.             {
    89.  
    90.                 perlin *= desertTiles.Length - 1;
    91.                 int tileNum = Mathf.RoundToInt(perlin);
    92.  
    93.                 tileMap.SetTile(new Vector3Int(x, y, 0), desertTiles[tileNum]);
    94.                 GameObject plant = Instantiate(desertVegetation[Random.Range(0, desertVegetation.Length)], new Vector3(x, y, 0), Quaternion.identity);
    95.                 plant.transform.parent = gameObject.transform.GetChild(0);
    96.  
    97.             } else {
    98.  
    99.                 perlin *= desertTiles.Length - 1;
    100.                 int tileNum = Mathf.RoundToInt(perlin);
    101.  
    102.                 tileMap.SetTile(new Vector3Int(x, y, 0), desertTiles[tileNum]);
    103.  
    104.             }
    105.  
    106.         }
    107.  
    108.     }
    109.  
    110. }
    111.  
    Thanks!
     
    Kurt-Dekker likes this.