Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 alexanderameye · May 16, 2014 at 04:33 PM · c#programmingminecraft

Question about script for minecraft-like terrain generation

So I have this script for terrain generation like minecraft, but when I attach the script to a cube and press generate when running, it takes a couple of seconds for the terrain to load in. That is fine for me, but it creates the whole terrain at the same time. How can I code it so then it like creates line per line or block per block, but at a constant speed, because I don't want to wait like 30 seconds for a 500 by 500 terrain to generate, I want to see it creating the terrain piece by piece. I know I need to change the for loop in the script somehow. Here is the script, TYVM!

 using UnityEngine;
 using System.Collections;
 
 public class Blocks : MonoBehaviour
 {
     
     //Public variable for the size of the terrain, width and heigth
     public Vector2 TerrainSize = new Vector2( 50 , 50 );
     
     //Height multiplies the final noise output
     public float HeightOfTerrain = 10.0f;
     
     //This divides the noise frequency
     //Higher is Flatter
     public float NoiseSize = 10.0f;
     
     private GameObject root;
     
     void OnGUI ()
     {
         
         //Make a button that calls generate function when you press it
         //(left, top, width, height), "name"
         if(GUI.Button( new Rect( 10, 10, 100, 30 ), "Generate" ))
         {
             //Call the generate function
             Generate();
         }
         
     }
     
     //Function that inputs the position and spits out a float value based on the perlin noise
     public float PerlinNoise(float x, float y)
     {
         //Generate a value from the given position, position is divided to make the noise more frequent.
         float noise = Mathf.PerlinNoise( x / NoiseSize, y / NoiseSize );
         
         //Return the noise value
         return noise * HeightOfTerrain;;
         
     }
     
     //Generates the terrain
     void Generate ()
     {
         
         //Destroy the current terrain if there is one
         Destroy(GameObject.Find("Terrain"));
 
         //Create a new empty gameobject that will store all the objects spawned for extra neatness
         root = new GameObject("Terrain");
 
         //Put the root object at the center of the boxes
         root.transform.position = new Vector3( TerrainSize.x/2, 0, TerrainSize.y/2 );
 
 
 
         
         //For loop for x-axis (width)
         for(int i = 0; i <= TerrainSize.x; i++)
         {
             
             //For loop for z-axis (length)
             for(int p = 0; p <= TerrainSize.y; p++)
             {
                 
                 GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 
                 box.transform.position = new Vector3( i, PerlinNoise( i, p ), p);
                 box.transform.parent = root.transform;
                 
             }
 
                 
         }
 
         
         //Move the root at the origin.
         root.transform.position = Vector3.zero;
         
     }
 }
Comment
Add comment · Show 1
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 njpatter · May 16, 2014 at 06:10 PM 1
Share

If you use a cube primitive for every cube/voxel then it is going to go incredibly slow.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · May 16, 2014 at 09:25 PM

The way Minecraft-like terrain generation works is as follows:

  • MC-like terrain is made up of individual chunks, for example MC uses chunks that are 16x128x16 (with each chunk also divided into sub-chunks)

  • each chunk's mesh can have up to ~65k vertices

  • A three-dimensional array is filled with data concerning the type of block at this xyz position

  • for each coordinate in that array, up to six faces are generated depending on wether there are neighboring blocks

  • each face is made up of four certices

  • once all faces, and thus blocks, are creates, the resulting mesh is applied to the chunk's mesh component, and often to it's meshcollider component as well.

There a loads of exmaple scripts and tutorials available, and I also recommend digging through the treasure trove that is the After playing minecraft... thread on the Unity forums.

Comment
Add comment · 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
0

Answer by zGiMbOz · Aug 26, 2016 at 04:47 AM

How do i change the cube color?

Comment
Add comment · 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

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

22 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to learn c sharp fast? 0 Answers

Adding a player to a list 0 Answers

Parkour Help Please !!??! 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