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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with perlin noise plz

Discussion in 'Scripting' started by Ncon123, Aug 24, 2015.

  1. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TerrainMaker : MonoBehaviour
    5. {
    6.    
    7.     public int size = 50;
    8.     public int BlockNumber;
    9.  
    10.     public bool enableHeight = true;
    11.     public bool move = false;
    12.  
    13.     public float scale = 7.00f;
    14.     public float scaleModifier = 5f;
    15.     public int y = 0;
    16.     public float offsetHeight = 1.5f;
    17.  
    18.     public GameObject Grass;
    19.     public GameObject Stone;
    20.     public GameObject Bedrock;
    21.  
    22.     public int x = 0;
    23.     public int z = 0;
    24.  
    25.     void Start ()
    26.     {
    27.         for(x = 0; x < size; x++)
    28.         {
    29.             for(z = 0; z < size; z++)
    30.             {
    31.                 GenerateColumn();
    32.             }
    33.         }
    34.     }
    35.  
    36.     void GenerateColumn()
    37.     {
    38.         BlockNumber = 0;
    39.  
    40.         for (int y = 0; y >= -50; y--)
    41.         {
    42.             if(BlockNumber < 4)
    43.             {
    44.                 GenerateGrass();
    45.             }
    46.             else if(y == -50)
    47.             {
    48.                 GenerateBedrock();
    49.             }
    50.             else
    51.             {
    52.                 GenerateStone();
    53.             }
    54.                
    55.             BlockNumber++;
    56.            
    57.         }
    58.     }
    59.  
    60.     void GenerateStone()
    61.     {
    62.         var c = Instantiate(Stone, new Vector3(x, y, z), Quaternion.identity) as GameObject;
    63.         c.transform.parent = transform;
    64.     }
    65.    
    66.     void GenerateGrass()
    67.     {
    68.         var c = Instantiate(Grass, new Vector3(x, y, z), Quaternion.identity) as GameObject;
    69.         c.transform.parent = transform;
    70.     }
    71.    
    72.     void GenerateBedrock()
    73.     {
    74.         var c = Instantiate(Bedrock, new Vector3(x, y, z), Quaternion.identity) as GameObject;
    75.         c.transform.parent = transform;
    76.     }
    77.  
    78.  
    79.  
    80.     void Update ()
    81.     {
    82.         updateTransform();
    83.     }
    84.    
    85.    
    86.     void updateTransform()
    87.     {
    88.         foreach(Transform child in transform)
    89.         {
    90.             var height = Mathf.PerlinNoise(child.transform.position.x/scale,
    91.                                            child.transform.position.z/scale);  
    92.            
    93.             if(enableHeight)
    94.                 applyHeight(child, height);
    95.         }
    96.     }
    97.    
    98.     void applyHeight(Transform child, float height)
    99.     {
    100.         var yValue = Mathf.RoundToInt(height*scaleModifier);
    101.        
    102.         var newVec3 = new Vector3(child.transform.position.x, yValue, child.transform.position.z);
    103.        
    104.         child.transform.position = newVec3;
    105.     }
    106. }


    I'm trying to modify this so it generates the terrain with blocks under it.
    Voxel like.
    So generate column is ment to enerate the top block when generate a column downwards until it reaches a point.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Create a for loop, that loops from 0 to height, and fill it with block.
     
  3. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    For example? I dont quite understand what you mean.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It looks to me like your code already generates columns of blocks. But then you're mucking with the positions of these in Update... why would you do that? Why are you not generating them where you want them (noise and all) in Start?

    By the way, if you're trying to make a Minecraft-like game, you'll never get good performance out of this approach (generating an object for every voxel in the world). You need to use procedural mesh generation to efficiently render only the exposed surfaces.

    I have code to do this, and could clean it up and put it in the Asset store, but I assumed there were already assets like this by now... have you looked for them?