How to Procedurally Generate a Perfect Maze (Unity Tutorial)

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 มิ.ย. 2024
  • In this tutorial we're going to look at how to procedurally generate a perfect maze.
    The project files are available to our patrons here:
    ► / 84948773
    The final script can be found here
    ► dotnetfiddle.net/krlgdG
    Help support our work:
    ► Patreon: / ketragames
    Follow us:
    ► Ketra Games: www.ketra-games.com
    ► Patreon: / ketragames
    ► Twitter: / ketragames
    ► Facebook: / ketragames
    Introduction - 0:00
    Explanation of the Maze Generator Algorithm - 00:11
    Creating the Project - 01:00
    Creating the Maze Cell Prefab - 02:08
    Implementing the Maze Generator Algorithm - 09:05
    Summary - 22:06
    #KetraGames #LearnUnity #UnityTutorials #UnityTips

ความคิดเห็น • 95

  • @Ma_cieK
    @Ma_cieK 11 หลายเดือนก่อน +10

    This is a fantastic channel, and your talent for explaining how and why things work is truly remarkable. I sincerely hope that you will continue to produce more videos like this one for our community. Your contributions are highly valued and appreciated.

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +2

      Thanks so much for this comment. It really made our day 😊

  • @HandsOnKnowledge
    @HandsOnKnowledge 11 หลายเดือนก่อน +5

    Super excited to see another upload, can't wait to watch the whole thing 😊

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +2

      Thanks for this comment 😊

    • @HandsOnKnowledge
      @HandsOnKnowledge 11 หลายเดือนก่อน +1

      @@KetraGames and thank you for making such quality content 👍

  • @mrB25N
    @mrB25N 11 หลายเดือนก่อน +3

    Wow I needed a way to create an array of buttons, just the way you created the array of Maze cells, this is incredible timing! Thank you so much for this

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Glad it will be helpful, thanks for this comment 😊

  • @5ld734
    @5ld734 10 หลายเดือนก่อน +3

    This is fantastic and your channel is a gem this is exactly what I and many others needed to start to learn procedural generation

    • @KetraGames
      @KetraGames  10 หลายเดือนก่อน +1

      Thanks very much for this great comment 😊

  • @marcoseliasmep
    @marcoseliasmep 11 หลายเดือนก่อน +4

    Wow the best tutorial about this subject! Thanks a lot for this video! It’s better than many complete courses that I’ve seen.

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Thanks so much for this comment 😊

  • @runjimrun
    @runjimrun 11 หลายเดือนก่อน +5

    This is interesting! I'm working on a top down shooter with a random generated city, so this can come in handy

    • @Rahulsingh-theraha
      @Rahulsingh-theraha 11 หลายเดือนก่อน +3

      U might need to use wave function collapse instead of dfs algorithm

    • @runjimrun
      @runjimrun 11 หลายเดือนก่อน +1

      @@Rahulsingh-theraha Awesome, will look into this!

  • @yours_indie_game_dev
    @yours_indie_game_dev 10 หลายเดือนก่อน +3

    to those asking implement entrance/ exit to the maze you can garb all the outer walls as such
    //if x=0 first row get the leftwall
    //if x==row-1 last row get the rytwall
    //if z=0 first colunm get the backwall
    //if z=cols-1 first colunm get the frontkwall
    add them to a list an perhaps find an index and clear 2 walls for entrance n exit
    x,and z being the index of the cell row and colums being the width and the depth as per this tutorial

  • @ChrizSee
    @ChrizSee 11 หลายเดือนก่อน +1

    Great tutorial! Thank you!

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Thanks for this 😊

  • @ItachiX9
    @ItachiX9 11 หลายเดือนก่อน +1

    That's awesome. I wanted to learn about maze algo.

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Glad it was helpful 😊

  • @gazzer4461
    @gazzer4461 11 หลายเดือนก่อน +1

    Thanks Ketra!
    I always wondered how mazes were created in software.

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Glad it was useful 😊

  • @philberex
    @philberex หลายเดือนก่อน

    Thank You for this great video!!

    • @KetraGames
      @KetraGames  27 วันที่ผ่านมา

      👍😊

  • @amypellegrini1732
    @amypellegrini1732 หลายเดือนก่อน

    Great tutorial! 5 stars

  • @zeshufuno9177
    @zeshufuno9177 10 หลายเดือนก่อน +7

    Great tutorial!But I want to ask
    there is way to make a maze entrance and exit?

    • @kettu9943
      @kettu9943 หลายเดือนก่อน

      You can basically put both at opposite ends because every tile can be reached from everywhere

  • @W0rldbuilder
    @W0rldbuilder 11 หลายเดือนก่อน +3

    Wonderfully done! ♥ Might I suggest an addition to this system, wherein we can set a number of larger prefab rooms that get dropped into the maze (for enemy spawns, treasure, etc.)? Or would that be much too complicated?

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +2

      Hi, thanks for the suggestion 😊

  • @CongTuSoi
    @CongTuSoi 9 หลายเดือนก่อน +1

    Hi, great tutorial!
    One question I have is how would you make it so that the maze is spawns at a determined position rather than (0, 0, 0)?

  • @Blossomanukposi
    @Blossomanukposi 9 วันที่ผ่านมา

    This is sooo cool!!!

    • @KetraGames
      @KetraGames  3 วันที่ผ่านมา

      👍😊

  • @weckar
    @weckar 10 หลายเดือนก่อน +3

    This is nice. However, this method tends to create a single long main path with relatively short side branches.
    To prevent that, you can iterate over a shuffled queue of all cells with unvisited neighbors instead.
    In addition; at the end of the process, you can knock down a couple extra walls at random to create loops and overall a less straightforward to solve maze.

    • @gasperkrevs5079
      @gasperkrevs5079 8 หลายเดือนก่อน

      And how would one modify this code to create such effect? If you know by any chance. Thank you in advance.

    • @weckar
      @weckar 8 หลายเดือนก่อน

      @@gasperkrevs5079 I would have to rewatch. I'll get gack to you.

    • @gasperkrevs5079
      @gasperkrevs5079 8 หลายเดือนก่อน

      @@weckar Thabk you very much. If you have any idea how to do it, please let me know otherwise no problem

    • @gasperkrevs5079
      @gasperkrevs5079 8 หลายเดือนก่อน

      I did manage to get a random maze so that is not jsut a single line, but now I want to know if it is possible to create a maze so that when you click on Play, it doesnt say Maze Cell (Clone) but instead the whole maze becomes a game object that I can move around and stuff like that. If you know what I mean?

    • @ButterDog11
      @ButterDog11 6 หลายเดือนก่อน

      @@gasperkrevs5079 Fetch all the MazeClones and assign them a common parent gameobject, now you can move them together

  • @ButterDog11
    @ButterDog11 6 หลายเดือนก่อน

    Great tutorial, any help on finding dead ends in the maze?

  • @_Garm_
    @_Garm_ 11 หลายเดือนก่อน +2

    thank you :D

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      👍😊

  • @mostly4real
    @mostly4real 10 หลายเดือนก่อน +2

    What is this best way to create this maze on a Coordinate that is not 0,0,0?
    as example: I want to have two different mazes in the 3D Platforming Tutorial for the Player to run through.

  • @hamzamehmood9483
    @hamzamehmood9483 11 หลายเดือนก่อน +2

    Thanks

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      👍😊

  • @SyedHammadZada
    @SyedHammadZada 10 หลายเดือนก่อน +1

    How we can spawn collectibles within the maze path at every scene load?
    How about enemy ? Can we do path finding within the maze path?

  • @ButterDog11
    @ButterDog11 6 หลายเดือนก่อน +1

    Never mind I got it.
    For anyone who wants to know:
    Create a variable inside your mazecell script to store the number of active walls of that cell. Now in the maze generator script fetch all the cells who have only 3 active walls, as dead end cells are the only cells with 3 active walls.

    • @KetraGames
      @KetraGames  6 หลายเดือนก่อน +1

      Glad you got it sorted. Thanks for sharing 😊

  • @AlaaMasalmeh
    @AlaaMasalmeh 11 หลายเดือนก่อน +2

    Great tutorial! please upgrade this to First Person or Third Person maze escape with minimap

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +1

      Thanks for the suggestion 😊

    • @Gilgamesh_king_of_heroes
      @Gilgamesh_king_of_heroes 11 หลายเดือนก่อน +1

      ​@@KetraGamesthx for the video
      I'm already thinking 3d maze shooter😮😮

  • @runjimrun
    @runjimrun 11 หลายเดือนก่อน

    Somehow, my sprites (uploaded from sprite pack, containing multiple walking animations) cannot be scaled via PPU. It is sliced via cell size, nut I'm unable to change any properties like PPU and filter mode. Any ideas?

  • @minhkhoa6081
    @minhkhoa6081 5 หลายเดือนก่อน +1

    Sorry i have a question.what algorithicm you used in this generate map.

  • @henrysteven137
    @henrysteven137 11 หลายเดือนก่อน +1

    yo this is so cool

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Thanks for this comment 😊

  • @rainbowslime__
    @rainbowslime__ 11 หลายเดือนก่อน +4

    where u maze quit?

  • @dylanrobinson830
    @dylanrobinson830 5 หลายเดือนก่อน

    How do you fix the z fighting all the walls are too big making the pixels clash

  • @Madkevop7
    @Madkevop7 5 หลายเดือนก่อน

    When the walls overlap, how to fix the corner walls z fighting

  • @ThiagoPrego
    @ThiagoPrego 6 หลายเดือนก่อน

    Thank you so very much. May the Lord bless you always.

  • @bobbydharrell
    @bobbydharrell 4 หลายเดือนก่อน

    This Tutorial was amazing, Thank you. 1 Question though. I wanted to scale up my MazeCell Prefab to be twice as big so it would not be so cramped. But then it broke the Maze Generator .. how do i adapt the Maze Generator to handle any Cell size? Thanks in advance!

    • @magic00squirrel
      @magic00squirrel 3 หลายเดือนก่อน

      Did you figure out how to fix this?

    • @bobbydharrell
      @bobbydharrell 3 หลายเดือนก่อน

      @magic00squirrel yes, I went to the unity forums and another developer helped me out.

    • @aniellorenci7984
      @aniellorenci7984 2 หลายเดือนก่อน

      @@bobbydharrell could you link what helped u or tell me what u did pls

  • @jovonsanon1
    @jovonsanon1 11 หลายเดือนก่อน

    If I wanted to make the physically cells bigger How would I do that? Simply making the prefab parent object larger on the scale is causing problems ?

    • @KetraGames
      @KetraGames  10 หลายเดือนก่อน +1

      Hi, you would need to make some adjustments based on the scale of the prefab. So, for example if your pefab is 2x2 you would need to multiply the x and z positions by 2 when creating the initial grid. Hope that helps 😊

    • @someoneiguess23
      @someoneiguess23 6 หลายเดือนก่อน +1

      @@KetraGames how would you do that?

  • @ricardocreemers
    @ricardocreemers 7 หลายเดือนก่อน +1

    I got an error with the OrderBy methode (row 44). It thinks it's a methode of the IEnumerator. I used using System.Linq; But it doesn't recognize it.

    • @bonaIdeaPro
      @bonaIdeaPro 4 หลายเดือนก่อน +1

      Check if you wrote the right code here(should be IEnumerable, and not IEnumerator) :
      private IEnumerable GetUnvisitedCells(MazeCell currentCell)

    • @kamiii_samaa
      @kamiii_samaa 3 หลายเดือนก่อน

      @@bonaIdeaPro THANK YOU SO MUCH

  • @Sunil_Sable
    @Sunil_Sable 4 หลายเดือนก่อน +1

    I'm here after watching GamesofVab video😂

  • @user-dr5nt2is7f
    @user-dr5nt2is7f 11 หลายเดือนก่อน

    It would be the same concept for the 2D maze generation right?

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +1

      Hi, yes the same concept would work for 2D 😊

  • @sleazybot
    @sleazybot 7 หลายเดือนก่อน

    I'm trying to add to this maze an object player(first person) but it doesn´t appears or fit into the maze, anyone haz any suggestion for me

    • @dannchap9946
      @dannchap9946 5 หลายเดือนก่อน

      Your player has to be smaller than the cell prefab size. Just shrink the player object until it fall into the maze. Hope this helps.

  • @aarondelgado3421
    @aarondelgado3421 11 หลายเดือนก่อน +2

    Great tutorial! You should expand on this tutorial by making into a series where one can add NPCs, doors, pickups, etc.

  • @quetsche
    @quetsche 11 หลายเดือนก่อน +1

    Hi ! What algorithm does it use ?

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +1

      Hi, it uses randomized depth-first search 😊

    • @quetsche
      @quetsche 11 หลายเดือนก่อน +1

      @@KetraGames thanks!

  • @-Toon-
    @-Toon- 10 หลายเดือนก่อน

    What is the best way to scale the size of the cells without messing up the generation?

    • @KetraGames
      @KetraGames  9 หลายเดือนก่อน +2

      Hi, one way you could do it is to add an empty game object that you scale up or down. Then when you Instantiate the maze cells you can specify this as a parent. Hope that helps 😊

    • @-Toon-
      @-Toon- 9 หลายเดือนก่อน

      @@KetraGames this works to scale the cell prefabs, but the maze size/spacing between cells remains the same which results in the cells overlapping

    • @KetraGames
      @KetraGames  9 หลายเดือนก่อน +1

      Hi, this is because when using Instantiate it sets World position rather than the local position. You can fix this by adding the following line
      _mazeGrid[x, z] = Instantiate(_mazeCellPrefab, new Vector3(x, 0, z), Quaternion.identity, _parent);
      _mazeGrid[x, z].transform.localPosition = new Vector3(x, 0, z);
      You'll also need to use local position further down in the algorithm
      private IEnumerable GetUnvisitedCells(MazeCell currentCell)
      {
      int x = (int)currentCell.transform.localPosition.x;
      int z = (int)currentCell.transform.localPosition.z;
      Hopefully this should get it working 😊

  • @johnbai842
    @johnbai842 11 วันที่ผ่านมา

    facing a little issue here 😭My maze cell won't be generated for some reason

  • @SpeedyGamerTV
    @SpeedyGamerTV 10 วันที่ผ่านมา

    Is there a way to make it so the mazecells are 6 meters?

    • @KetraGames
      @KetraGames  7 วันที่ผ่านมา

      Hi, if you watch the next video in the series we show how to increase the size of the cells - th-cam.com/video/dOI-N2QVly8/w-d-xo.html. Hope that helps 😊

  • @PolyVR2
    @PolyVR2 7 หลายเดือนก่อน

    it doesn't let me drag the maze cell into the slot, how do i fix this?

    • @KetraGames
      @KetraGames  6 หลายเดือนก่อน

      Hi, have you made the maze cell a prefab by dragging it into the Project Panel?

    • @PolyVR2
      @PolyVR2 3 หลายเดือนก่อน

      @@KetraGames it works now :D. but one question. how do i change the size of the maze? like can i make the walls in the prefab bigger? or do i have to do something different?

  • @ItsCrucible
    @ItsCrucible 11 หลายเดือนก่อน +2

    This is amazing! But, can you make a tutorial for it to be infinite? Like, Backrooms stuff I guess? I am making a horror game and I need an infinite maze.

  • @raelroque5595
    @raelroque5595 11 หลายเดือนก่อน

    when are you going to do something more advanced? like interaction with or between npcs, quest system, etc.

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Hi, thanks for the suggestions. We'll add them to the list of future videos 😊

  • @Vastlee
    @Vastlee 11 หลายเดือนก่อน

    Is there a reason you're still using Visual Studio 2019? 2022 is really great.

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน +1

      Hi, we stuck with 2019 as that was what was installed with Unity last time I checked. Now Unity 2022 is in LTS we'll probably upgrade to that and Visual Studio at the same time. Thanks 😊

  • @hggyuu
    @hggyuu 8 หลายเดือนก่อน

    I want to code like you

    • @KetraGames
      @KetraGames  8 หลายเดือนก่อน

      👍😊

  • @elisangelacardozo1788
    @elisangelacardozo1788 11 หลายเดือนก่อน +1

    Great tutorial! Thank you!!!

    • @KetraGames
      @KetraGames  11 หลายเดือนก่อน

      Thanks for this 😊