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 Jackop222 · Jan 03, 2014 at 10:56 PM · procedural-generationgeneratingterrain gen

Help with randomly generated terrain!

 using UnityEngine;
 using System.Collections;
 
 public class Generation : MonoBehaviour {
 
 
 
     void Start () {
 
         for (int z = 1; z < 20; z++) {
             for (int x = 1; x < 20; x++) {
                 //create cube
                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 //add rigid body
                 cube.AddComponent<Rigidbody>();
                 //freze the y axis
                 cube.rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
                 // create random color
                 Color newColor = new Color( Random.value, Random.value, Random.value, 1.0f );
                 //color cube
                 cube.renderer.material.color = newColor;  
                 //create random number to add to y
                 int RandomAdd = Random.Range(1,6);
                 //create y
                 int y = 0;
                 //add number to y
                 y += RandomAdd;
                 //offset
                 cube.transform.position = new Vector3(x, y, z);
 
             }
         }


 
     }
     
 
     
 
     
 
     void Update () {
 
 
     }
 }



I am trying to create a game where the cubes randomly generate at random heights, so i can later on place procedural generated building on them, my blocks now do this : http://gyazo.com/eed286ac4c82791fcd6c15edd2b9a46d

I would like them to not have the gaps so it is all connected, how can i do that ?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Memige · Jan 03, 2014 at 11:24 PM

You're off to a decent start. At the moment what you are doing is generating a cube at every X/Z position on your grid, and then randomly changing the height. The way i'm envisioning it, you have two options.

1: If you do not want caves to be possible, after you discover the height of your random cube, place an additional cube under it for each step, so at height 3, add another cube at height 0, 1, and 2 at the same X/Z coordinate.

2: If you do want caves, instead of randomly choosing a height, create a third nested loop to cycle through each y location as well. Then randomly determine if you should place a cube at all (you can fiddle with the exact percentage to alter how dense your world will be). To ensure you have no floating cubes, simply keep a list of every place you have placed a cube, then when you are creating a new cube, check to see if there is a cube next to it, and if not and it is not on the lowest level, cancel the placement and skip to the next spot.

Comment
Add comment · Show 1 · 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 Jackop222 · Jan 04, 2014 at 10:33 AM 0
Share

How would do number 1, i don't need caves in my game?

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

19 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

Related Questions

Program that generate a mesh from 3 images. What was its name? 0 Answers

Why is StaticBatchingUtility not completely combining all of my meshes? 1 Answer

Terrain generation problem. 1 Answer

Get Terrain material / Set splat maps 1 Answer

Terrain Generation 1 Answer


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