Procedural Generation: Endless Runner Unity Tutorial (Updated 2023)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's learn how to make endless procedurally generated levels in unity if you're wondering how games like subway surfers make levels that are seemingly infinite this is accomplished by something called procedural generation which I'm going to show you how to do in the previous video I showed you how you can make this cool curvature effect like Subway Surfers has in unity but in this video I'm going to show you how you can give the illusion that your game is never ending to make this happen here's what we need to do while it looks like the player is is running forward the world is actually coming to him as the road comes to the player new roads are being generated in the background without us being able to see if you haven't watched the previous video no worries we won't be doing anything with the curving Shader in this video but I am going to remove the effect just so that it's easier to see we'll want to be able to change the values of that curvature on a whim in the future but we have to have this part set up first for that to make sense so stay tuned I'll be covering that in the next video so the first thing that we need is a signal that tells the game when to generate a new section to do this I'm going to create an invisible wall so that when the player hits the wall this will create a signal to Unity to generate a new section we'll want to know when the player hits the wall and for now the player is just this little ball however we don't actually want the player to collide with the wall we just want it to pass through so we're going to create a box collider but we're going to check is trigger if we want to be able to instantiate this entire section in code we'll want to turn this into a prefab so we can access it so let's just take this whole thing and drag it into our assets folder and we may want to have different types of triggers or spawn points in the future so I'm going to add a tag on this invisible wall as well I probably could name it something better but I'm just settling on the name trigger for now and a quick note here if I I change something in the prefab like this just in the hierarchy view I'm just changing it on this one particular instance not the whole prefab so if I want this to be applied to the entire prefab I can either go to the parent game object and click on that little overrides button and push this to the main prefab or I can click into the prefab itself and change it from there so just a note because you will want this trigger on all of your road sections and now we have our invisible wall we have our tags now we need to actually do something so I'm going to add a script on my player object to check for when the player collides with the wall I'm just going to name it section trigger with Visual Studio open this is actually a very simple script I just need to create a variable to store my prefab which I will plug in on the inspector side and then I'm just going to check if the player collides with the wall the script in this case is on the player so we're just checking for Collision when the other object's tag is trigger we're using on trigger enter here since we want something to happen when the player enters that trigger Zone and we're taking in just the default arguments so we're just going to say that when the other objects tag is trigger instantiate the road prefab that we made with that saved we now see a section on the inspector side or we can drag in our road section prefab and going into play mode you'll see my mistake here hitting play I'm just moving the player through the wall manually so I'm not having to mess with any of the player control mechanics yet we're just testing this functionality and as you can see uh nothing happens and that is because I forgot to add a rigid body to my player we'll have to add a rigid body on our player because checking for Collision needs physics to be there in order for that to happen adding a rigid body adds physics so I'm just going to add a rigid body but I will not be using gravity for the time time being so I'm going to uncheck that hitting play again and as soon as I drag the player through our wall you can see that a new section is instantiated perfect now we're getting somewhere now in a game like Subway Surfers you would probably have several of these sections built out for the first part of the game because we're creating this illusion that this is never ending of course in our little game right here we can still see everything very clearly but with a little curvature a little scenery and a little fog players won't know what hit him but we still have a problem because our platform isn't instantiating in a place that makes sense we want it to instantiate behind our other platforms so luckily if we go back to our script we can specify exactly where we want this to spawn the instantiate Declaration can take in three parameters if we want it to right now we just have it instantiating the one road section but we can also tell it where so we can change this to tell it to to instantiate the road section add in a new Vector 3 I'm just using 0 0 and- 57 because this is where the platform initially overlaps the first one you can play around with this and put in your own values if you want to and then we also need to tell it what rotation the game object needs to spawn in at so we're just saying quater and. identity which means no rotation at all now we're cooking hitting play again and we can see that as the ball passes through the new pre we spawns in the right place to make this work in practice we have to have the platforms actually move on their own instead of me dragging the player through the trigger so we're going to go to our prefab object and add another script that will slowly move the platform towards the player clicking into our script and opening up visual studio we're going to want to put this in the update function so that we're checking the position every frame and adding a small value each time to change the object's position over time we're going to want to access the object's transform component so we're going to say transform. position plus equals because we want to add a little value each time new Vector 3 and I only want to move this along the z-axis so I'm going to put 0 0 and three in there so each frame will be checking the object's current position and adding three along the Z axis and then we're going to multiply it by time. Delta time this essentially equal Iz is the speed and experience the update method checks every frame and depending on your computer your frame rate could be faster or slower so if you have a fast computer this functionality could look very different than if you have a slow computer and time. Delta time kind of equalizes that for everyone now we can see that our platforms are moving and while our script is working it's now spawning farther away because I didn't take into account the moving of the platform when I set my initial spawn point to correct this I'm going to pause my game adjust the platform and take note of where it should overlap and it looks like -35 is a good place to start then I'll update that number in my code as well you could pull this value out if you wanted to but since the trigger is tied to the object it doesn't matter how fast the platform is going the spawn effect will always happen where it needs to so if I speed it up a little uh you can see that this still works as intended you'll need this because if you want to speed up your game as the player progresses to make the game harder this will allow you to do that and now we have working procedural generation but we still have one tiny problem and that's all of these kind of ghost platforms that we don't need anymore behind our player there's no sense in keeping these and they'll weigh down your game because your computer will still have to devote energy to keeping them around so we can do the same thing that we did earlier and create an invisible wall to destroy these platforms this doesn't have to be a prefab since we'll only need one of these but I'll create a new game object add a box collider just like we did earlier again click is trigger and then I will create a tag named destroy clicking into the move script on our road prefab we can write a similar function as we did earlier we'll say void on trigger enter and take in the default arguments this is void because we're not returning any value with this function and then we'll just say if the other game objects tag is destroy destroy the game object that this script is on which is the road so anytime a platform collides with the destroy wall destroy that object if we go back to the inspector view we can see that once again I have forgotten to add a rigid body to my game object so I'm going to go into my prefab at a rigid body and we definitely don't want to use gravity on this one and as soon as we do that now we have some really cool procedural generation the really cool thing about procedural generation is that you can gradually make levels Harder by adding speed increasing the density of objects adding different types of objects and so much more but in the next episode I'll teach you how to change your Shader graph values by incorporating what we just learned today hope to see you there
Info
Channel: Rigor Mortis Tortoise
Views: 9,758
Rating: undefined out of 5
Keywords: unity, unity tutorial, procedural generation, endless runner, subway surfer, game development, gamedev
Id: Ldyw5IFkEUQ
Channel Id: undefined
Length: 9min 27sec (567 seconds)
Published: Thu Oct 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.