ROGUE LIKE RANDOM LEVEL GENERATION - INTERMEDIATE C#/UNITY TUTORIAL - #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to black thorn prod I'm Noah today we're going to dive headfirst into the world of random level generation using as usual unity and the c-sharp programming language I've already made a few tutorials on random level generation both aimed at beginners one on how to spawn random objects at random locations the other and how to make a binding of issac style dungeon in this brand new tutorial series we will create a random platformer level generation system identical to the one found in the amazing game spelunky in this YouTube video Mark Brown explains in detail how this random level generation works our goal will be to code this all out and bring it to life in unity so once you've checked mark Brown's videos I also urge you to read this blog post it will explain how the level generation works in even more detail now of course all games are different and so a level generation system that works from one game almost probably not fit the design needs for your game so the main goal of this new tutorial series is to really flex your programming muscles teach you programming techniques and put you in a procedural level generation state of mind it's also going to be loads of fun so I hope you'll join me on this adventure which should last three to four videos max once you're done you can of course try building a whole game using this level generation system that's completely up to you lastly this tutorial series is aimed at intermediate level game developers basically you should have a solid understanding of the basics of C sharp and unit 8 if we're watching these videos if not you might find some stuff pretty hard and confusing before starting here's also a big thank you to Joseph for his amazing financial support on patreon alright so the first thing we need to do is create the various quotes rooms they'll make up each level we need to create a room with left and right openings one with left right and a down opening another with a left right and top opening and lastly a room with openings in all four directions well then use these rooms to create a path the player can navigate going from the top of the level to the bottom in other words a big challenge of this level generation is to create a clear path that isn't cut off anywhere going from the starts to the exits as you can see here each one of these level layouts has that critical path however in this example there isn't that critical path as you can see the player would get stuck at this point and wouldn't be able to reach the exits that's something we definitely don't want happening anyway once the main path generators random rooms will then spawn to fill up the rest of the level here we don't need to worry about whether some of these random rooms are closed off or not so since they're not part of that critical path from these stars to the exits if the player really wants to access those rooms he have to destroy the terrain using bombs for example so I'm going to create an empty game object I'll reset its transform components and I'll call it room lr4 left right so you guessed it I want this room to have openings on the left and right I'll give this empty game object a little red gizmo so I can more easily see it in the scene view only down control to enables snappy movements I'll snap this room LR right here so it's placed in the middle of this section of the unity grinder this is how big each room will be now of course you can make your rooms as big or small as you want for those who don't know you can tweak your Steffi movement settings under edit snap settings I currently have mine set to 0.5 for the X and y axis meaning that when I hold down control and moving objects around the scene that object will move up little 0.5 increments along the X or Y axis which is really useful when you want to be very precise in your placements that is the case right now it's important this room LR empty game object is right in the middle of this area of the grinder with that Darnell tray another empty game object called spawn points and I'll reset its transform position and then given a yellow gizmo and parented to the room LR empty game objects holding down control worth snappy movements I'll place it at the bottom left corner of my room as you can see this spawn point is placed right in the middle of this little square its job will be to spawn the little tile the player can then walk on I gone ahead and made a simple tile sprites I'll answer it a 2d box Collider and then turn this tile into a prefab and I'll create the very first c-sharp script which I'll simply call spawn object and drag and drop it onto the spawn points opening up inside of Visual Studio I'll create a public in-game object array called objects and in the star function I want to instantiate a random objects from that objects for Radia so I'll create an int variable called rent and set that equal to a random number between 0 and the amount of elements inside of the objects viremia all I need to do is instantiate an object in that objects array with an index equal to R and at the spawn point position and with no rotation back in unit C I'll drag and drop inside of my objects array my tile prefab and hit play and you'll see that these foam points has indeed instantiated the tile if I had multiple tile prefabs each with a different visual and I grind drops doors into that same array you'll see that each time I press play I get a different tile because remember we're choosing to spawn a random object from the objects Iranian allow duplicate this spawn point hold out control for a snappy movement and move it right here and I'll repeat that process of multiple times so as to create a room with left and right openings awesome and hitting play you'll see that IND decayed cool room made up of these tiles with left and right openings if I had a character he could run around and jump inside of this room with ease seeing that the room is made up of tiles it would also be pretty easy to make all this destructible like in spelunky and create a holes in the led using bombs for example anyway I'll now turn this room LR into a prefab and I'm going to go ahead and make some more rooms such as one with left right and a bottom opening we are also turned into a prefab another with left right and the top opening and lastly one with openings in all four directions which leaves me with four room prefabs all right it's now time to trace the path of going from the start of the level to the exits in this tutorial series I'll be making a level Lycan spelunky which is chorim's wide and forums long so I'll use a simple square sprite with a box Collider to define that area and I'll then make an empty game object called level generation now they're making new c-sharp scripts called level generation drag and drop that onto a newly made empty game object and open it up so I want to randomly choose a star C position to spawn the first room of the level so here here here or here to do so I'll make a transformer a called starting positions and in the star function I'll create an int variable called R and starting pose and said that's equal to a random number between 0 and the amount of elements inside of these starting positions Aradia they'll said this level generation game objects position equal to a random starting position a hop back inside of unity and creates an empty game object called pose 1 I'll give it a little blue gizmo and then snap it to the center of this room area in my level and then duplicate sit and move that to the center of this area and I'll repeat that two more times these are my starting positions now select my level generation game object lock the inspector so as to easily drag and drop all of these starting positions inside of that Arabia and hitting play a few times you'll see that each time my level generation starts it's a random position in the level great now I'll create an array of type team objects called rooms will obviously be drag and dropping all our different rooms inside of this Aranea with that done I'll just instantiate the first room of the array and the level generations current position and with no rotation so back instead of unity I'll drag drop all my room prefabs inside of the rooms array and hit play and as expected in room spawns i random starting position in the level now it's time to spawn more rooms in order to create a path of going from the start of the level to the exits so what we first need to do is define a direction in which our level generation is going to move but will it move left and spawn a room to the left of the starting room will new rights or will it move down so I which creates an int variable called direction which are said to be equal to a random number between 1 and 5 in the start function but remember that the last number we type here in this case 6 is included now I'm going to create a new function called move in this function I want my level generation to move in a certain direction depending on the value of my direction variable and spawn in certain room so I'll start by creating an if statesman's checking whether a direction is equal to 1 or 2 if it is I'll decide to get my level generation to move right so I'll create a vector two variable called new pause and set that equal to a new vector 2 that has the level generations car x-coordinates plus a certain amount which are stories I have a float variable called move amounts for example and for the y coordinate I'll just leave that at the players current y position and then set the level generations position equal to that new position so in unity I'll say move amount equal to 10 this way the level generation if Direction is equal to 1 or 2 will move of 10 units to the right and spawning rumor so yeah I'm typing on 10 because each room is separated by 10 units all right I'll now check whether direction was equal to 3 or 4 if it was are copy and paste these three lines of code simply changing this plus into a minus so that the level generation moves left instead of rights and lastly I'll check whether direction was equal to 5 and if that's the case I'll move down by decreasing the level generations Y position with move amount instead of its x value you'll notice that there is a slightly less chance that the level generation moves down instead of right or left of course this is purely up to you you can very well choose to give each direction and equal chance of happening now simply instantiate a room from my rims array after these if statements and say direction equal to a random number between 1 and 5 again so that the level generation continues building our procedural map and doesn't simply move in a straight line rights left or down before winding back into unity to testing now though we first need to call this a move function so I'm going to create a float variable called time between room and another call start time between movement which I'll set equal to 0.25 by default in my update function I'll then check whether a time between room is less or equal to 0 and if it is I'll call my move function so the level generation moves in a random direction and spawns a room I also said time between room equal to start time between room so that I need to a 0.25 million seconds before my level generation moves again and spawns a new rumor of course if Time Machine room isn't less or equal to zero ah slowly decreases value using - equals time aDOT's delta tiny'mon and now our head back into unity make sure start time between room is equal to some small value and hits play and you'll see that indeed the level generation is moving around the scene spawning rooms it's not all working the way we wanted to not only is it spawning rooms on top of one another it's also reaching the limits we established earlier and cherry on the cake there's no end to this level of generation the level generator is going to keep spotting rooms ad infinitum so there's plenty cool things to be done before we can call this random level generation completes for now the great job for having watched the whole video I hope you're as excited as I am to complete this project if you have any problems or don't understand something related to this video don't hesitate asking me a question in the comment section down below or via the btp discord server and me or some other member of the community we'll try helping you out you can expect episode 2 to come out in 2 to 3 days there we will complete the code that gets our level generator spawning a clean path from the start to the exit of the level until then you can support me at my channel by hitting the like and subscribe buttons it's just so motivating and appreciated also consider following me on twitter where lately i'm posting all of my inktober drawings lastly here's a big thank you to my patrons for their amazing financial support you guys are amazing ok stay tunes have a great day Cheers you
Info
Channel: Blackthornprod
Views: 153,495
Rating: undefined out of 5
Keywords: blackthornprod, gameDev, unity, tutorial, noaCalice, c#, programming, art, animation, rogue like, spelunky, random, level, generation, procedural, binding of isaac, dungeon, intermediate, advanced, random level generation, world, procedural level generation, indie game dev, how to, ROGUE LIKE RANDOM LEVEL GENERATION - INTERMEDIATE C#/UNITY TUTORIAL - #1, episode 1, #1, epic, platformer, 2d, 3d, tutorial series, tiles, destructible terrain, derek yu, random map, random world
Id: hk6cUanSfXQ
Channel Id: undefined
Length: 13min 15sec (795 seconds)
Published: Thu Oct 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.