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
1
Question by flygongaby · May 15, 2018 at 08:37 PM · spawnproceduralmaze

Spawning collectibles in a randomly generated maze

So I made a proceduraly generated maze that uses a digging algorithm to create the maze and then sometimes add spike traps on the tiles it digs. I now want to set collectibles to spawn only in dead ends. I tried to detect if a maze position is sou rounded by 3 wall block it would change the the maze position to a different state than the walls and spikes. When I tried, it only generated 1 collectible instead of putting one in each dead end. I would also eventually want to limit the amount of collectibles. Thanks for the help.

Here's the code for the maze generation:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class MazeGenerator : MonoBehaviour
 {
 
     public int mazeHeight = 11;
     public int mazeWidth = 11;
     private int[,] maze;
 
     public MazeCell cellPrefab;
 
     public SpikeCell cellAlt;
 
     private MazeCell[,] cells;
 
     public GameObject wallGo;
 
     public GameObject spikeGo;
 
     public GameObject collectGo;
 
     public GameObject plane;
 
     public double percent = 0.25;
 
     static System.Random _random = new System.Random();
 
 
 
     void Start()
     {
         maze = GenerateMaze(mazeHeight, mazeWidth);
 
         for (int i = 0; i < mazeHeight; i++)
             for (int j = 0; j < mazeWidth; j++)
             {
                 if (maze[i, j] == 1)
                 {
                     Vector3 pos = new Vector3(i, 1, j);
                     GameObject wall = Instantiate(wallGo, transform) as GameObject;
                     if (wall != null)
                         wall.transform.position = pos;
                 }
                 if ((maze[i, j] == 2) && (i != 1) && (j != 1))
                 {
                     Vector3 spikePos = new Vector3(i, 0, j);
                     GameObject spike = Instantiate(spikeGo, transform) as GameObject;
                     if (spike != null)
                         spike.transform.position = spikePos;
                 }
                 if (maze[i, j] == 3)
                 {
                     Vector3 collectPos = new Vector3(i, 1, j);
                     GameObject collect = Instantiate(collectGo, transform) as GameObject;
                     if (collect != null)
                         collect.transform.position = collectPos;
                 }
             }
     }
 
     private int[,] GenerateMaze(int Height, int Width)
     {
         int[,] maze = new int[Height, Width];
         for (int i = 0; i < Height; i++)
             for (int j = 0; j < Width; j++)
                 maze[i, j] = 1;
 
         System.Random rand = new System.Random();
 
         int r = rand.Next(Height);
         while (r % 2 == 0)
             r = rand.Next(Height);
 
         int c = rand.Next(Width);
         while (c % 2 == 0)
             c = rand.Next(Width);
 
         maze[r, c] = 0;
 
         MazeDigger(maze, r, c);
 
         CollectibleGenerator(maze, r, c);
 
         return maze;
     }
 
     private void CollectibleGenerator(int[,] maze, int r, int c)
     {
         for (int i = 0; i < mazeHeight; i++)
             for (int j = 0; j < mazeWidth; j++)
             {
                 if (((maze[r+1, c]) == 1 && (maze[r-1, c] == 1) && (maze[r,c-1]==1)) || ((maze[r + 1, c]) == 1 && (maze[r, c + 1] == 1) && (maze[r, c - 1] == 1)) || ((maze[r + 1, c]) == 1 && (maze[r - 1, c] == 1) && (maze[r, c + 1] == 1)) || ((maze[r - 1, c]) == 1 && (maze[r, c + 1] == 1) && (maze[r, c - 1] == 1)))
                 {
                     maze[r, c] = 3;
                 }
             }
     }
 
 
 
     private void MazeDigger(int[,] maze, int r, int c)
     {
         // 1 - North
         // 2 - East
         // 3 - South
         // 4 - West
 
         int[] directions = new int[] { 1, 2, 3, 4 };
         Shuffle(directions);
 
         for (int i = 0; i < directions.Length; i++)
         {
             switch (directions[i])
             {
                 case 1:
                     if (r - 2 <= 0)
                         continue;
                     if ((maze[r - 2, c] != 0) && (maze[r - 2, c] != 2))
                     {
                         maze[r - 2, c] = 0;
                         maze[r - 1, c] = 0;
                         if(Random.value <= percent)
                         {
                             maze[r - 2, c] = 2;
                         } 
                         MazeDigger(maze, r - 2, c);
                     }
                     break;
                 case 2:
                     if (c + 2 >= mazeWidth - 1)
                         continue;
                     if ((maze[r, c + 2] != 0) && (maze[r, c + 2] != 2))
                     {
                         maze[r, c + 2] = 0;
                         maze[r, c + 1] = 0;
                         if (Random.value <= percent)
                         {
                             maze[r, c + 2] = 2;
                         }
                         MazeDigger(maze, r, c + 2);
                     }
                     break;
                 case 3:
                     if (r + 2 >= mazeHeight - 1)
                         continue;
                     if ((maze[r + 2, c] != 0) && (maze[r + 2, c] != 2))
                     {
                         maze[r + 2, c] = 0;
                         maze[r + 1, c] = 0;
                         if (Random.value <= percent)
                         {
                             maze[r + 2, c] = 2;
                         }
                         MazeDigger(maze, r + 2, c);
                     }
                     break;
                 case 4:
                     if (c - 2 <= 0)
                         continue;
                     if ((maze[r, c - 2] != 0) && (maze[r, c - 2] != 2))
                     {
                         maze[r, c - 2] = 0;
                         maze[r, c - 1] = 0;
                         if (Random.value <= percent)
                         {
                             maze[r, c - 2] = 2;
                         }
                         MazeDigger(maze, r, c - 2);
                     }
                     break;
             }
         }
     }
 
     public static void Shuffle<T>(T[] array)
     {
         var random = _random;
         for (int i = array.Length; i > 1; i--)
         {
             int j = random.Next(i);
             T tmp = array[j];
             array[j] = array[i - 1];
             array[i - 1] = tmp;
         }
     }
 
 
 //    public void Generate()
 //    {
 //        GameObject floor = Instantiate(plane, transform) as GameObject;
 //        floor.transform.localScale = new Vector3(mazeHeight, mazeWidth, 1f);
 //    }
 //}
 
     public void Generate()
    {        cells = new MazeCell[mazeHeight, mazeWidth];
         for (int x = 0; x < mazeHeight; x++)
         {
             for (int z = 0; z < mazeWidth; z++)
             {
                 CreateCell(x, z, maze);
             }
         }
     }
 
     private void CreateCell(int x, int z, int[,] maze)
     {
         
                 MazeCell newCell = Instantiate(cellPrefab) as MazeCell;
                 cells[x, z] = newCell;
                 newCell.name = "Maze Cell " + x + ", " + z;
                 newCell.transform.parent = transform;
                 newCell.transform.localPosition = new Vector3(x - mazeHeight * 0f, 0f, z - mazeWidth * 0f);     
     }
 }

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
Best Answer

Answer by oStaiko · May 15, 2018 at 09:47 PM

I don't really want to dig through your code... so I'm just gonna give you the steps to do it yourself.

If you want a LIMITED number of RANDOM spawning collectibles, you definitely don't want to set them as you find them in your algorithm. Instead, use an algorithm to find their positions, and save those to a list. Now, you have a list of every possible position to spawn a collectible, and a map that hasn't been changed yet. Randomly pick as many positions as you want from the list and set those to collectibles. This is now truly random, and can be limited.

I did notice your algorithm to check for walls was a little... poor. Here's a sudo version for you to use that is a lot easier to understand.

 for (i=1) // DO NOT START AT 0. 0-1 is -1 and you'll get an error. Run this from 1 to max-1.
    for (k=1)
    {
       if (pos==wall) // Goes to next iteration if this is a wall
          continue;
       int walls = 0;
       if (pos+i==wall)
          walls++;
       if (pos-i==wall)
          walls++;
       if (pos+k==wall)
          walls++;
       if (pos-k==wall)
          walls++;
 
       if (walls==3)
          Debug.Log("Success! Save this position for a collectible:);
    }
 }
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

90 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

Related Questions

Altering a "Drunkard Walk" random level generation algorithm to create an ever-changing, non-euclidian labyrinth 0 Answers

Spawning GameObjects with Hinge Joints 1 Answer

How to overlap tiles in procedural meshes? 1 Answer

Dynamic NavMesh Creation at Runtime? 1 Answer

Is it possible to duplicate an Item that is proceduraly generated with random elements? 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