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 awplays49 · Jun 22, 2015 at 10:53 PM · cavecaves

Cave generation code broken. Need some help please!

Hi!

Recently I started making a Starbound type game for experience but I ran into a major problem when some code didn't do anything.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class MapGenerator : MonoBehaviour {
 
     public int width;
     public int height;
     
     private enum Cubes
     {
         Yes,
         No
     };
     private Cubes [,] map;
     
     [Range (0, 100)]
     public int fillPercent;
     
     private int tiles;
     
     private int CalculateWalls (int x, int y) {
         int neighbors = 0;
         for (int x2 = x - 1; x2 < x + 1; x2 ++)
         {
             for (int y2 = y - 1; y2 < y + 1; y2 ++)
             {
                 if (x2 != -1 && x2 != width && y2 != -1 && y2 != height)
                 {
                     if (map [x2, y2] == Cubes.Yes)
                     {
                         neighbors ++;
                     }
                 }
             }
         }
         return neighbors;
     }
     
     void Start () {
         CreateMap ();
     }
     
     void CreateMap () {
         map = new Cubes [width, height];
         FillMap ();
     }
     
     void FillMap () {
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 int randomizer = Random.Range (0, 9);
                 if (randomizer == 0)
                 {
                     if (tiles / (width * height) * 100 < fillPercent)
                     {
                         map [x, y] = Cubes.Yes;
                         tiles ++;
                     }
                     else
                     {
                         Debug.Log ("Over fill percentage.");
                     }
                 }
                 else
                 {
                     map [x, y] = Cubes.No;
                 }
             }
         }
         for (int i = 0; i < 5; i ++)
         {
             SmoothMap ();
         }
     }
     
     void SmoothMap () {
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 int neighbors = CalculateWalls (x, y);
                 Debug.Log (neighbors);
                 if (neighbors > 4)
                 {
                     map [x, y] = Cubes.No;
                 }
             }
         }
     }
     
     void OnDrawGizmos () {
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 Vector2 pos = new Vector2 (x + 0.5f - width / 2, y + 0.5f - height / 2);
                 if (map != null)
                 {
                     if (map [x, y] == Cubes.Yes)
                     {
                         Gizmos.color = Color.red;
                     }
                     else
                     {
                         Gizmos.color = Color.green;
                     }
                     Gizmos.DrawCube (pos, Vector3.one);
                 }
             }
         }
     }
 }
 

I set the fill percent to 75, but it doesn't seem to even consider the fill percentage, and it also freezes (maybe because of a lot of debugging) when adding this line, line 84:

 Debug.Log (neighbors);

It doesn't make sense, because it is only called 5 times so I don't understand what's wrong. It is called 5 times because a for loop executes 5 times in a void called in Start, and after that it calls the void Smooth every loop. Doesn't make any sense.

I set the width and height to 100.

I will greatly appreciate any and all help, Cheers!

Comment
Add comment · Show 3
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 deathhound225 · Jun 23, 2015 at 11:37 AM 0
Share

2 questions for you 1. has this code previously worked? 2. what version of unity are you using?

avatar image meat5000 ♦ deathhound225 · Jun 23, 2015 at 11:34 AM 0
Share

Not an answer.

avatar image awplays49 · Jun 23, 2015 at 11:42 AM 0
Share

No the smoothing never worked. Or it never displayed a difference at least. I am using Unity 5.1 the latest.

I was following this http://youtu.be/v7yyZZjF1z4

and wanted to also do the seed system but I couldn't understand a few lines he wrote so I improvised.

0 Replies

· Add your reply
  • Sort: 

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

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

Making Cave system in Unity 5 0 Answers

Can I make a cave out of two terrain layers? 1 Answer

How to dig a hole 3 Answers

Multiple Monitor Cave Setup with three Cameras 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