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 NovaDrox · May 11, 2018 at 02:41 PM · generationmaze

Fill in the small empty gaps (Maze Generator)

So I created a generator that is able to generate different mazes using random seeds. Now the problem I am running into is how to fill in the tiny gaps that it creates when doing different maps. I want to leave the bigger area for a player to move around.

Code:

 public float cellSize = 1;
 public int XSize = 20;
 public int ZSize = 20;

 [Range(0,100)]
 public int randomFillPercent;

 public GameObject Cell;

 public string seed;
 public bool useRandomSeed;

 public Transform MazeHolder;
 private GameObject[,] Maze;

 private void Start()
 {
     GenerateMaze();
 }

 private void GenerateMaze()
 {
     Maze = new GameObject[XSize, ZSize];
     CreateMaze();
 }

 private void CreateMaze()
 {
     if (useRandomSeed)
     {
         seed = Random.Range(0,999999999).ToString("0");
     }

     System.Random pseudoRandom = new System.Random(seed.GetHashCode());

     for (int x = 0; x < XSize; x++)
     {
         for (int z = 0; z < ZSize; z++)
         {
             if (x == 0 || x == XSize - 1 || z == 0 || z == ZSize - 1)
             {
                 Maze[x, z] = (GameObject)SpawnCell(x, z);
             }
             else if (pseudoRandom.Next(0, 100) < randomFillPercent)
             {                   
                 Maze[x, z] = (GameObject)SpawnCell(x, z);                  
             }
         }
     }
 }

 private GameObject SpawnCell(int X, int Z)
 {
     float newX = cellSize * X;
     float newZ = cellSize * Z;

     GameObject newCell = Instantiate(Cell, new Vector3(newX, 0, newZ), Quaternion.identity);

     newCell.transform.SetParent(MazeHolder);

     return newCell;
 }
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 Glurth · May 11, 2018 at 02:56 PM

An image of the "tiny spaces" would help confirm, but I suspect this issue is due to the fact that the Mesh you use for each cell is NOT precisely cellSize, in world space. From the description I'd guess it is slightly smaller that cellSize, or maybe slightly offset from model-space center.

You could use the [bounds] dox link member of MeshRenderer (assuming the cell has one) e.g. cell.GetComponent<MeshRenderer>().bounds to get the actual, rendered size of the cell, in worldspace. You can then use this value, instead of the hard-coded '20'.

Comment
Add comment · Show 3 · 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 NovaDrox · May 11, 2018 at 04:40 PM 0
Share

Its these areas that I highlighted with red. As for the cellSize, it should be distance between each cell, which I am just using the standard cube from Unity. As for the mesh, its multiply cubes, not 1 huge mesh.

[1]: /storage/temp/116700-highlighted.png

highlighted.png (33.4 kB)
avatar image Glurth NovaDrox · May 11, 2018 at 04:54 PM 0
Share

Ah, so you want an algorithm to fill an area. $$anonymous$$y answer above does NOT apply.

Here is a link to "flood-fill", which is a way to fill in those tiles. The trick is going to be knowing if a given area SHOULD be filled or not. You may want to make it TWO passes, where the first one only COUNTS the empty tiles to see if they are above or below your "fillSizeThreshold", then on the second pass, do the actual fill only on areas below the fillSizeThreshold.

avatar image NovaDrox Glurth · May 11, 2018 at 05:34 PM 0
Share

I get what your saying, but would you know how to integrated it into the code I created?

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

115 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

Related Questions

Generating 2D collisions for maze 0 Answers

[Help]How can I make my maze makes steps as it generate ? 2 Answers

how one would go about using Triangulator in javascript? 0 Answers

Procedural terrain generation ? 0 Answers

Dyanmic Platforms and Erasing Geometry? 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