Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_Vpi53N_fb99F6g · Oct 03, 2019 at 06:31 PM · terrainvoxelminecraftperlin noise

Minecraft-like ore generation

The project is a simple voxel world made with perlin noise for the y making something similar to a minecraft chunk

 //Function that creates the terrain
     void  CreateTerrain(int xRange, int yRange, int zRange)
     {
         //If random seed is checked, choose a random seed between negative max seed and positive max seed
         if (randomSeed)
         {
             seed = Random.Range(-maxSeed, maxSeed);
         }
 
         //Nested for loops to create the terrain
         for (int x = 0; x < xRange; x++)
         {
             for (int z = 0; z < zRange; z++)
             {
                 //perlinY is used to make a wave-like terrain
                 float perlinY = (int)( Mathf.PerlinNoise(seed + x / detail, seed + z / detail) * scale );
                 //We add the ground level to the perlinY so it generates on top of all of the stone and dirt
 
                 if (groundLevel >= 0)
                 {
                     perlinY += groundLevel;
                 }
                 
                 GameObject grass;
 
                 //Creates the grass cubes on the x, y, and z
                 grass = Instantiate(cubeTypes[0], new Vector3(x,perlinY,z), Quaternion.identity);
                 grass.transform.SetParent(GameObject.FindGameObjectWithTag("Terrain").transform);
 
                 //Creates the player at the middle of the terrain
                 if((x == xCubes/2 && z == zCubes / 2) && !created)
                 {
                     GameObject cam = Instantiate(player, new Vector3(x, perlinY + 1, z), Quaternion.identity);
                     created = true;
                 }
 
                 //Creates stone and dirt up from 0 to the y of each cube
 
                 if (groundLevel >= 0)
                 {
                     for (int y = 0; y < perlinY; y++)
                     {
                         //Used to randomize where dirt appears
                         int dirtLayers = Random.Range(1, 5);
 
                         //This is used to optimize the game, making it so it only draws the outside walls
                         if ((x == xRange - 1 || x == 0) || (z == zRange - 1 || z == 0))
                         {
                             //This is used to make dirt only appear past a certain y
                             if (y >= perlinY - dirtLayers)
                             {
 
                                 //Creates the dirt cube
                                 GameObject dirt = Instantiate(cubeTypes[1], new Vector3(x, y, z), Quaternion.identity);
                                 //Parents the dirt cube to the terrain game object
                                 dirt.transform.SetParent(GameObject.FindGameObjectWithTag("Terrain").transform);
 
                             }
                             else
                             {
                                 //This is used to optimize the game, making it so it only draws the outside walls
                                 if ((x == xRange - 1 || x == 0) || (z == zRange - 1 || z == 0))
                                 {
                                     //Creates the stone cube
                                     GameObject stone = Instantiate(cubeTypes[2], new Vector3(x, y, z), Quaternion.identity);
                                     //Parents the stone cube to the terrain game object
                                     stone.transform.SetParent(GameObject.FindGameObjectWithTag("Terrain").transform);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }

What I want to add now is some ore generation, but instead of just doing it with random numbers (which works fine, but is too simple) I would like it to be based on the seed, so the same seed results in the same world. That's where I'm stuck, I've tried perlin noise but it just does some weird things. If anyone knows how to do it please help, I'm absolutely lost. Thanks.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gonzales6 · Oct 16, 2019 at 12:22 PM 0
Share

Is it possible to make such a world generator and if so, can I get some pointers to how it can be done and what topics to research? If somebody wrote a tutorial about doing that it would be perfect UPSers website. I hope this isn't a duplicate question because I am specifically interested in generating each block completely independent from its surroundings.

avatar image lgarczyn Gonzales6 · Oct 16, 2019 at 08:17 PM 0
Share

There is a tutorial on this exact subject: https://youtu.be/chbIgz5H0lA

There are plenty of other tutorials and videos of you Google "voxel procedural world"

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by lgarczyn · Oct 04, 2019 at 09:12 PM

Since the other answer avoided the question, you can easily set the seed of the Random class. Random would have predictable results.

That taken care of, I wouldn't suggest using Mathf.PerlinNoise, as it doesn't allow real seeds, and only two dimensions.

The seed here mostly randomizes your starting location, not the actual world.

There are C# libraries for 3d noise generators, fractal noise, and well as the C# random class which allows you to have multiple generators and seeds.


Now as for a better answer, you could also do something like

     float perlinOre = Mathf.PerlinNoise(seed + y / oreDetail, seed + oreFlatness * Mathf.PerlinNoise(seed + z / detail, seed + x / oreDetail) / oreDetail);

    if (perlinOre > oreThreshold)
        //blablabla

If possible, you should use a different seed for ores. Maybe use a string as your seed, and do this

       int seed = inputString.GetHashCode();
       int seedTerrain = seed & 0xFFFF;
       int seedOre = seed >> 16;

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_Vpi53N_fb99F6g · Oct 05, 2019 at 09:42 PM 0
Share

Thanks a lot for the answer, but I'm really a noob, and can't quite get what this would do, forgive me, probably should have said it from the begging.

avatar image lgarczyn unity_Vpi53N_fb99F6g · Oct 06, 2019 at 01:38 AM 0
Share

Can you be more precise about what you don't understand? The last part is simply a way of storing two 32bit ints into a 64bit long. It would allow you to have a single large number as a seed, and using it for multiple different seeds.

avatar image unity_Vpi53N_fb99F6g lgarczyn · Oct 06, 2019 at 10:46 AM 0
Share

The first part I do understand, but the first line of the second code I don't. What I'm guessing it's doing is shifting it, as the second line is doing, but I'm not sure. Thanks.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Perlin noise for (sort of Minecraft) terrain generator 2 Answers

Anyone got a Voxel generator I can use? 0 Answers

Cube style terrain 1 Answer

How to load stacking chunks on the fly? 1 Answer

Add perlin noise to blocky terrain 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges