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.

Question Perlin Noise is not Perling Noising

Discussion in 'Scripting' started by Jaumetv, Sep 9, 2023.

  1. Jaumetv

    Jaumetv

    Joined:
    Mar 8, 2022
    Posts:
    2
    Hey! I am using Perlin noise to generate a terrain procedurally. This includes the procedural placement of the grass and rocks.
    To put you in context, this is how my terrain should look always:
    upload_2023-9-9_13-24-0.png
    As you can see, the grass makes a nice pattern following elevation.

    When my character moves, the terrain loads and unloads with it, if I load terrain to the left or to the right (x axis) everything looks fine as you can see, the pattern is still here:
    upload_2023-9-9_13-24-15.png

    The problem comes when I move up and down (along the z axis). As you can see, the pattern disappears and all of a suddent I get stripes of grass, as if somebody mowed my terrain XD:
    upload_2023-9-9_13-24-21.png


    I think I know where the issue may reside. As you will see in my code, at first I look for movement in the x axis and then I do the same for the z axis. x axis doesn't have any problems so the isseua may be in the z axis block of code. Here is all the code I think you will need:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (Mathf.Abs(CalculateMovementX()) < tileSize && Mathf.Abs(CalculateMovementZ()) < tileSize)
    4.             return; //IF THE PLAYER HASN'T MOVED 1 FULL TILE YET, RETURN
    5.         GenerateTerrain();
    6.     }
    7.  
    8.     private int CalculateMovementX()
    9.     {
    10.         return (int)(Mathf.Floor(character.transform.position.x) - previousPosition.x);
    11.     }
    12.  
    13.     private int CalculateMovementZ()
    14.     {
    15.         return (int)(Mathf.Floor(character.transform.position.z) - previousPosition.z);
    16.     }
    17.  
    18.     private void GenerateTerrain()
    19.     {
    20.         if (Mathf.Abs(CalculateMovementX()) > 0)    //IF PLAYER MOVES HORIZONTALLY
    21.         {
    22.             if (CalculateMovementX() < 0)
    23.                 selectedTile.x = (int)(Mathf.Floor(character.transform.position.x) * tileSize - ((gridWidth / 2) + 1) * -1);
    24.             else
    25.                 selectedTile.x = (int)(Mathf.Floor(character.transform.position.x) * tileSize - ((gridWidth / 2) + 1));
    26.          
    27.             //LOOP TO DELETE OLD TILES
    28.             for (j = -1; j < gridHeight + 1; j++)
    29.             {
    30.                 selectedTile.y = (int)(Mathf.Floor(character.transform.position.z) - gridHeight / 4 + j * tileSize);     //OFFSET THE Z AXIS SO THE TERRAIN FIST IN THE SCREEN NICELY
    31.              
    32.                 DeleteTile();
    33.             }
    34.  
    35.             if (CalculateMovementX() < 0)
    36.                 selectedTile.x -= gridWidth;
    37.             else
    38.                 selectedTile.x += gridWidth;
    39.          
    40.             //LOOP TO SPAWN NEW TILES
    41.             for (j = 0; j < gridHeight; j++)
    42.             {
    43.                 selectedTile.y = (int)(Mathf.Floor(character.transform.position.z) - gridHeight / 4 + j * tileSize);    //OFFSET THE Z AXIS SO THE TERRAIN FIST IN THE SCREEN NICELY
    44.              
    45.                 i = CalculateMovementX() > 0 ? gridWidth : 0;
    46.                 GenerateTile();
    47.             }
    48.         }
    49.  
    50.         if (Mathf.Abs(CalculateMovementZ()) > 0) //IF PLAYER MOVES VERTICALLY
    51.         {
    52.             if (CalculateMovementZ() < 0)
    53.                 selectedTile.y = (int)(Mathf.Floor(character.transform.position.z) * tileSize - ((gridHeight / 4 * 3) + 1) * -1);  // 3/4 TIMES TERRAIN SIZE;
    54.             else
    55.                 selectedTile.y = (int)(Mathf.Floor(character.transform.position.z) * tileSize - (gridHeight / 4) - 1);  // 1/4 TIMES TERRAIN SIZE
    56.          
    57.             //LOOP TO DELETE OLD TILES
    58.             for (i = -1; i < gridWidth + 1; i++)
    59.             {
    60.                 selectedTile.x = (int)(Mathf.Floor(character.transform.position.x) - gridWidth / 2 + i * tileSize);
    61.              
    62.                 DeleteTile();
    63.             }
    64.  
    65.             if (CalculateMovementZ() < 0)
    66.                 selectedTile.y -= gridHeight;
    67.             else
    68.                 selectedTile.y += gridHeight;
    69.          
    70.             //LOOP TO SPAWN NEW TILES
    71.             for (i = 0; i < gridWidth; i++)
    72.             {
    73.                 selectedTile.x = (int)(Mathf.Floor(character.transform.position.x) - gridWidth / 2 + i * tileSize);    //OFFSET THE Z AXIS SO THE TERRAIN FIST IN THE SCREEN NICELY
    74.              
    75.                 j = CalculateMovementZ() > 0 ? gridHeight : 0;
    76.                 GenerateTile();
    77.             }
    78.         }
    79.         previousPosition.x = Mathf.Floor(character.transform.position.x);
    80.         previousPosition.z = Mathf.Floor(character.transform.position.z);
    81.     }
    82.  
    83.  
    84.     private void DeleteTile()
    85.     {
    86.         if (!placedTiles.ContainsKey(selectedTile)) return;
    87.         tilePool.Release(placedTiles[selectedTile]);   //DESTROYS A TILE ONLY IF IT EXISTS
    88.         placedTiles.Remove(selectedTile);
    89.  
    90.         if (!placedAssets.ContainsKey(selectedTile)) return;    //DESTROYS AN ASSET IF IT EXISTS IN THAT POSITION
    91.         newAsset = placedAssets[selectedTile];
    92.         if(newAsset.CompareTag("SeaWeed"))
    93.             seaWeedPool.Release(placedAssets[selectedTile]);
    94.         else if(newAsset.CompareTag("Rock"))
    95.             rockPool.Release(placedAssets[selectedTile]);
    96.         placedAssets.Remove(selectedTile);
    97.     }
    98.  
    99.  
    100.     private void GenerateTile()
    101.     {
    102.         if (placedTiles.ContainsKey(selectedTile)) return; //IF THE TILE ALREADY EXISTS, MOVE ON
    103.      
    104.         //PERLIN NOISE WITH SPLINE POINTS
    105.         placementCoord.x = selectedTile.x;
    106.         placementCoord.y = SplinePoints(Mathf.PerlinNoise((i + (int)Mathf.Floor(character.transform.position.x) + seed) / amplitude, (j + (int)Mathf.Floor(character.transform.position.z) + seed) / amplitude));
    107.         placementCoord.z = selectedTile.y;
    108.      
    109.         GenerateRock();
    110.         GenerateGrass();
    111.              
    112.         newTerrainTile = tilePool.Get();
    113.         newTerrainTile.transform.position = placementCoord;
    114.              
    115.         newTerrainTile.transform.SetParent(transform.GetChild(0));
    116.         placedTiles.Add(selectedTile, newTerrainTile);
    117.     }
    118.  
    119.  
    120.     private void GenerateGrass()
    121.     {
    122.      
    123.         if (placedAssets.ContainsKey(selectedTile)) return;    //ONLY PLACE AN ASSET IF THE LOCATION IS FREE
    124.      
    125.         aux = Mathf.PerlinNoise(i + placementCoord.x + seed, j + placementCoord.y + seed);
    126.         if (aux > 0.62)
    127.         {
    128.             newAsset = seaWeedPool.Get();
    129.             randomRotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
    130.             newAsset.transform.position = placementCoord + (Vector3.up / 4);    //PLACE THE ASSET JUST BELOW THE TOP OF THE TILE
    131.             newAsset.transform.SetParent(transform.GetChild(1));
    132.             placedAssets.Add(selectedTile, newAsset);
    133.         }
    134.     }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,245
    We don't need the code, you need the code! You're gonna debug this after all.

    First, simplify the problem. Make it like a 2x2 grid of cells, trivially micro-small, whatever the smallest thing you can do and still see the error.

    Then it is...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    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
    - you're getting an error or warning and you haven't noticed it in the console window

    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 names of the GameObjects or Components involved?
    - 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 supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

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

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    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

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. Jaumetv

    Jaumetv

    Joined:
    Mar 8, 2022
    Posts:
    2
    Dude I think this is the best reply I will ever get from a forum lol.
    Thank you so much, I will try to debug it following your suggestions.