Random Map Generator in UNITY! [Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the hardest things to get right in roguelikes is replayability you aren't designing the game to be played once you want it to be played over and over again and one of the best steps in achieving that is through varying level designs making the game feel different enough while still maintaining core elements in the game so in today's video I'm going to show you how to make a procedurally generated map using a random Walker algorithm in Under 12 minutes so let's jump into it first off what is the random Walker algorithm well let me show you let's say you have a grid of X and Y values these can be however big or small you want but keep in mind the bigger the grid the more interesting and different the design there are objects that we will call Walkers when the algorithm starts a single Walker is placed in a certain spot on the grid for this example I will have it in the center now during every iteration there are certain rules that a walker will obey the first being to place a tile down in its current location now if a tile already exists in that location it will skip placing over it to avoid increasing the percentage meaner then there are a few methods that will run that will change its possible Behavior the first being whether the Walker gets removed essentially there will be a chance that the current Walker that is being checked can be destroyed but only if there is at least one other active Walker to ensure that the percentage required to create a map is fulfilled the next is to change directions pretty much think that at all times the Walker's velocity is either X Plus 1 y 0 x minus 1 y 0 x 0 y minus 1 or x0 Y plus one next is to see if the Walker will duplicate this means that there's a chance that another Walker will spawn in its spot as long as the maximum number of Walkers if there is a maximum hasn't been reached and the finalists to update its new position based on its direction or the way that I explained its current velocity during this update as there is a grid we are working with we need to ensure that the value is clamped between the grid so that a walker doesn't go out of bounds of the possible map these steps will Loop continuously until a desired percentage of the grid has been populated we'll then move on to creating the walls the walls is simply created by checking all the position of the floor grid spaces and seeing if there is a neighboring empty tile and if there is then a wall tile can be placed in that position so now we have that covered let's get on to the code with the new Unity project open we will add a new tile map into our hierarchy and include two reference images for this I'm going to use green to represent a floor and blue to represent the walls sort of like an island we will then create two scripts for this walker object and Walker generator inside of our Walker object class we're going to make it a custom class so we can remove the need for the mono Behavior derivation now since it is a custom Clause we won't be able to see the actual Walker object on the screen but we will see where they're placing tiles down at the top of the script we will have these parameters position of the Walker and this handles the direction in which it wants to go we will also have this float value this would be the value that gets compared for each method that the Walker follows now since we are treating this as an object we need to have a Constructor which creates the object and fills its parameters and that's all we're going to need for the Walker object class now opening up our Walker generator class we have quite a few variables at the top which I'm going to explain what they're for first off since we're also using tile Maps we want to make sure that we include the using unityengine.tile maps at the top of the file so that we can access all of the tile map properties inside of our script the first thing that we have is an email which specifies what type of tile is in the current grid position it will also include a reference to this email but since we're working in a grid with X and Y values we want it to be a two-dimensional array will also include a list of our custom Walker class so that we can keep track of all active Walkers and their positions we will also have a reference to the tile map from our project as well as a single reference for two tiles which we can simply call floor and wall here we have an INT for the map width and height for this example I have used 30 and 30. but you can set that to however big or small you would like we also have an INT called Max Walkers which is just a limit to the maximum amount of Walkers that we can have moving around we have an INT for tile count which by default will be set to zero this works with the next float value which we will have is the fill percentage this is used to compare the amount of floor tiles with the desired percentage of the total grid we want covered finally we have a float called wait time this will be used for the generation method so that there is a pause between each successful movement now back inside of our Unity project we will create an empty game object and name it map generator and drag in the Walker generator script into the inspector all of the variables that we have in the script so far are set to public so you can play around with them inside the inspector to fit the type of map you want to make we will also drag in our floor wall and tile map objects into the inspector back inside of the Walker generator script inside of our start method we will create a call to our first method called initialize grid this will be the main setup for the grid so that the grid dimensions and properties are set up but also so that we can create our first Walker object over here we're setting the grid to be equal to the dimensions of the map width and height and then we're going to Loop through the grid and ensure every location is set to empty so that we can start placing our floors down we will then create a new instance of the Walker object list now tile Maps work in Vector 3 values but specifically integers so that all pieces are placed in exact locations and not using floating Point values this line here will create a reference to the exact center piece of the tile map now we will create a new Walker object and like all objects use the Constructor to set the initial values the get direction method is something we will call a lot and it returns a single Vector Direction so either up down left or right then we set the current grid location to be a floor and using our tile map reference set the tile to be our flow tile we will also add the current Walker to the Walkers list and increase our tile count that will be all we need for our setup and we can move on to our main Curry which handles all the methods that the Walkers must follow so in a new career team we'll create a while loop inside the whilst condition it is comparing the tile count as a float with the total size of the grid and will continue looping as long as they are less than the fill percentage value that we just set up earlier now since this is the code routine we will need some yielded return value so we have a condition here which is looking to be satisfied we will then Loop through every Walker in the list and create a reference to its current position and check if that position is an existing floor piece if it's not then we will set a new floor tile in that position increase our tile counter upgrade the grid type and make sure our ball has been set to true I have four methods here the first one is a call which Loops through our Walker list and using a random value we'll compare that to the Walker's chance and if it is less and that the walk account is greater than one then we can remove the walker from the list effectively deactivating the object the chance to redirect is going to do a similar thing but instead of removing the object you'll be a chance to redirect the direction in which the Walker is aiming a it's successful it will call our get direction method and update its direction our third one is the chance to create or duplicate again using a random value it will have a chance to duplicate the object as long as there aren't too many walkers on the screen as well if the check is successful we create a reference to the current position and create a reference to a New Direction so that we can create a new Walker object and fill the Constructor then we will add this to the Walker object list now our final method is to update the actual position of the Walker looping through the Walkers will make sure we add on the value of the direction to its current position now during this check we want to make sure that our values are clamped within the parameters of the grid so that when we create our walls all positions are accounted for and the floors aren't going outside of the grid back in our create flaws core routine just below our method calls we'll also have a check for the has created floorboard if it's true or in other words if the floor was successfully placed during this iteration we will yield return a new wait for seconds call with our wait time reference now outside of this while loop but still within the create floors Co routine we'll create another call to our final curl routine which will create our walls so in our create walls creatine we will Loop through our greens X and Y values but we will minus one from the max value since the flaws have never made it to that limit and we can instead Place walls on the edges if we need to so here we will check each X and Y value of the grid if they're flaws if they are we will continue within the loop since again we are creating a CO routine we want a new condition that needs to be satisfied during this Loop we want to look at all neighboring positions excluding the diagonals and if they are empty pieces then we know that a wall needs to be placed there when setting the walls we want to ensure that we are setting a wall tile in that position updating the grid type to be a wall and then we're updating our condition to be true finally we check again to see if our condition during this part was set to true then we know we need to yield a new wait for seconds call again with our wait time reference now if we go back into our Unity project and we hit play the map is being generated and each time it's going to be different as I mentioned these variables will all alter the map in some way to have fun seeing what different combinations you come up with and let me know down below your discoveries so I hope this video was useful and easy to understand and I hope everyone's had a great start to their year I'm Keen to keep creating content and tutorials for you all so if you have suggestions for another video or tutorial you want to see in the future let me know in the comment section below if you haven't already consider subscribing to the channel to keep up to date with all of the latest videos I have big plans for 2023 and I'm very excited to be able to share them with you all until then I'll catch you in the next video take care
Info
Channel: Garnet
Views: 23,405
Rating: undefined out of 5
Keywords: gamedev, game, development, game development, how to, procedural map generation, procedurally generated maps, tutorial, unity tutorial, unity, procgen, random walker algorithm
Id: 6B7yOnqpK_Y
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Mon Jan 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.