ROGUE LIKE RANDOM LEVEL GENERATION - INTERMEDIATE C#/UNITY TUTORIAL - #3 [END]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to black thorn prod I'm Noah and this is the third and final episode of the how to make a cool random level generation tutorial series inspired by Derek Hughes masterpiece spelunky using of course unity and c-sharp so in the previous video we almost completed the levels critical path to generation the only problem was that if the level generator went down twice in a row sometimes a nasty dead end would appear blocking the player from advancing to the game's exits the reason this is happening is because the level generator is getting ready to move down so it spawns a room with either openings in all four directions or a room with openings at the left right and bottom then is founds a room with a top opening but then it prepares to move down again so it destroys the current room if it doesn't have a bottom opening and replaces it with one that does the thing is that room could be of type too in other words a room with just left right and a bottom opening sword or top openings which obviously creates a dead end so what we're going to do to fix this is calculate if the level generator has moved down twice in a row if it has we'll make sure to spawn a room with openings in all four directions not a room with all the a bottom and letter right openings this way we are guaranteed no dead ends so inside of my level generation scripts I'll create an int variable called down counter and then simply increase down counter of one when the L generator moves down then if the level donor HR wants to move down and realizes that the roommate just spawn doesn't have a bottom opening then we want to make an if statements if down counter is greater or equal to two that will spawn a room with openings in all four directions because that means the level generator has moved down twice in a row even three times in a row and of course I'll make sure to destroy the other room we had spawned and that didn't have any bottom opening if not then we'll simply spawn either a room with openings in all four directions or a room with left-right and a bottom opening since there's no risk of dead ends now just make sure to reset down counter equal to zero when the level generator left or right because that means the level generator is about to move down twice in a row anymore awesome and now I'll hit play and you can be sure that you won't find any more dead ends in your games level all right it's now time to fill up all this empty space with other random rooms once the main path has been generators now of course there are plenty ways of doing this and I really urge you to test things out by yourself and only then take a look at how I'll do it even if you end up with loads of red error messages or bugs you'll have learnt a lot simply by experimenting on your own with that saying I'm going to duplicate this paused empty game object and using my snappy movements I'll move it down one room and I'll duplicate that empty game object multiple times so has to have a paused empty game objects placed at the center of each room area and I'll create a new C sharp script called something like spawn room with an add that to all my pause empty game objects inside the script I'll be checking whether my paused game object detects a room nearby and once the main path generation completes if it does detect a route that it won't do anything if not then it will simply spawn a random room from the level Generations room ray again like it's Valon key we don't need to worry about whether or not these side rooms are walled off they won't stop the player from advancing to the exits and if he really wants to access one of these closed rooms he'll is destroy the terrain using bombs for example so in my update function I'll create a Collider to the variable called room detection and set that equal to physics 2d overlapped circle and in the parenthesis Alice has a position for the invisible circle so the game object composition they'll type out a value for its radius one will do and lastly I can plug in a layer mask so I'll create a public layer mask variable called Wives room and copy/paste that right there we're basically doing the same thing as we did in the last episode in other words creating an invisible circle that will detect if there is any room nearby this time though we won't be destroying the room we simply want to see if there is a room if there isn't any room nearby so if room detection is equal to null then we want to spawn in random room so create a public variable of level generation called level gen now I can make an int variable called Rams and said that equal to a random number between zero and the amount of elements inside of my level James ruse array and now I can simply instantiate a random room from that rooms array using the rand variable before starting a random room though I'll make sure that not only is room detection equal to now but the main path has also been completely generators so I'll head back into my level generation script and turn these stop generation private bool variable into a public variable this way I can access it via another scripts such as these spawn room scripts and now I'll just check whether a stop generation is indeed equal to true now if I leave the code like this store random rooms will endlessly spawn so I'll make sure to destroy this pause empty game objects was the random room instantiate errs there's no need for any more in unity I'll make sure to set the boys room lair mass equal to room this way we're sure that the invisible circle will only detect and game objects with the room layer also drag and drop the level generation inside of that empty slot in the inspector lastly double check that each one of your rooms has a 2d box Collider attached to it and that they also have the room layer or the circle won't be able to detect if there's already a room or not and now hitting play you'll see that once the main path has been generated more rooms spawn to fill up the entire level you'll notice how extra rooms don't spawn on top of the main path because the invisible circle had detected that room was already placed there great we're almost done with this level donation system and I will show you how to add a random exciting elements to each room for example I'll drag and drop the room with left and right openings into the Seaview and take a look at it for now this room will always be the same each one of these four points will always spawn a tile periods let me now create a new empty game object called nothing and turn that into a prefab now for example I could grab one of these slow points and duplicate says a couple times two creates this extra row of titles I'll select all spawn points making up this new row and drag off inside of the objects array into nothing game objects this way there's 50 and chance that each spawn point spawn the tile and has much chance that is found this empty game objects in other words nothing at all so as you can see simply using the objects arranged you can create a lot of randomness in a single room you can create spawn points that randomly instantiate spikes goals even a monster you can also make something called random chunks which are basically a group of spawn points that one spawn point instantiates for example I'll duplicate a spawn points a couple times I'm making sort of a little platform shape but the player can jump on their group all these spawn points inside of an empty game object called chunk one and I'll turn that into a prefab and delete it from the scene view and now I can get this single spawn point spawning either nothing just one tile or that chunk obviously the more chunks you have the more randomness and excitements there will be of course it's also a good idea to create a couple room templates for each room type to create a few rooms for the left and right opening room type right left and bottom room type and so on just play spawn points differently in each room templates and get each to spawn different things for example one left and right room templates can be filled with spawn points that have a high chance of spotting platforms and loops whereas another room template can be riddled with spawn points that will more than likely spawn traps and monsters so once you've gone ahead and made a couple room templates for each room type you can first of all create an empty game object called length rank room templates and to that the spawn object script and drag drop inside of the array all of your various selector rights room templates now simply turn that into a prefab and inside of the level generation script replace the left/right room with the newly made left/right room templates prefab this way the level generator will always spawn the same room with left and right opening this full spawn a random one and yeah do the same for all the other room types and what you end up with is a wonderfully different level each time you hit play so as you can see not only can this system generates an enormous amount of unique but you has the designer also have a lot of control or the layout and content of each room it's a beautiful mix between random and handcrafted and that marked the end of this tutorial series I hope you had a good time and learned new things from here you can take this project even further and build an entire game around this level generation system or simply stop here and try building your own unique level generator using some tools and techniques taught in this series anyway with that said remember to hit the like and subscribe buttons if you enjoyed this video each new subscriber and each like is so appreciated and very very encouraging we also consider supporting me financially by a patreon like these amazing people alright I'll see you very soon for a lots more exciting game creation contents Cheers you
Info
Channel: Blackthornprod
Views: 43,181
Rating: undefined out of 5
Keywords: blackthornprod, gameDev, unity, tutorial, noaCalice, c#, programming, art, animation, spelunky, random level generation, ROGUE LIKE RANDOM LEVEL GENERATION - INTERMEDIATE C#/UNITY TUTORIAL - #3 [END], intermediate, advanced, game creation, map, world, how to make spelunky's level generation, episode 3, #3, dungeon generation, indie game dev, how to, coding, procedural, procedural level generation, 2d, 3d, platformer, random, fun, unity tutorial, 2018
Id: G9Wa0XZ2a2o
Channel Id: undefined
Length: 9min 44sec (584 seconds)
Published: Tue Oct 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.