• 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
Question by awplays49 · Jun 25, 2015 at 12:04 AM · cavecaves

Cave generation code. At low fill percentage, it seems the upper right is empty

Hello!

I have a code that generates cave. I followed Sebastian Lague's tutorial.

Plug this onto something and set the following variables:

Width = 128 Height = 72 Fill Percent = 50 Seed = null Use Random Seed = true Smoothness = 4 Edge Thickness = 2

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class TerrainGeneration : MonoBehaviour {
 
     public int width, height;
     [Range (0, 100)]
     public int fillPercent;
     private enum Cubes
     {
         No,
         Yes
     };
     private Cubes [,] map;
     public string seed;
     public bool useRandomSeed;
     public int smoothness;
     public int edgeThickness;
     
     int GetNeighbors (int x, int y) {
         int neighbors = 0;
         for (int x2 = x - 1; x2 < x + 2; x2 ++)
         {    
             for (int y2 = y - 1; y2 < y + 2; y2 ++)
             {
                 if (x2 != x || y2 != y)
                 {
                     if (x2 > -1 && x2 < width && y2 > -1 && y2 < height)
                     {
                         if (map [x2, y2] == Cubes.Yes)
                         {
                             neighbors ++;
                         }
                     }
                     else
                     {
                         neighbors ++;
                     }
                 }
             }
         }
         return neighbors;
     }
     
     void Update () {
         if (Input.GetKeyDown (KeyCode.Space))
         {
             CreateGrid ();
         }
     }
     
     void CreateGrid () {
         map = new Cubes [width, height];
         PlaceHoles ();
     }
     
     void PlaceHoles () {
         if (useRandomSeed == true)
         {
             seed = Time.time.ToString ();
         }
         System.Random prng = new System.Random (seed.GetHashCode());
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 if (x < edgeThickness || x > width - edgeThickness - 1 || y < edgeThickness || y > height - edgeThickness - 1)
                 {
                     map [x, y] = Cubes.Yes;
                 }
                 else if (prng.Next (0, 100) < fillPercent)
                 {
                     map [x, y] = Cubes.Yes;
                 }
             }
         }            
         for (int s = 0; s < smoothness; s ++) 
         {
             SmoothMap ();
         }
     }
     
     void SmoothMap () {
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {    
                 int neighbors = GetNeighbors (x, y);
                 if (neighbors > 4)
                 {
                     map [x, y] = Cubes.Yes;
                 }
                 else
                 {
                     map [x, y] = Cubes.No;
                 }
             }
         }
     }
     
     void OnDrawGizmos () {
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 if (map != null)
                 {
                     if (map [x, y] == Cubes.No)
                     {
                         Gizmos.color = Color.green;
                     }
                     if (map [x, y] == Cubes.Yes)
                     {
                         Gizmos.color = Color.red;
                     }
                     Vector3 pos = new Vector3 (x - width / 2 + 0.5f, y - height / 2 + 0.5f, 0);
                     Gizmos.DrawCube (pos, Vector3.one);
                 }
             }
         }
     }
 }

At 50%, it is clear that it is only developing at the lower left. EDIT

Since my last question I started over, and I have progressed a lot considering it is working. It just looks a little strange that's all. Like it has a seam.

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Cave generation code broken. Need some help please! 0 Answers

Making Cave system in Unity 5 0 Answers

Is it possible to build caves in the unity terrain? 1 Answer

isuue about multi-camera display in VR cave project. 0 Answers

How do you build cave entries / caves 2 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