How to make a Tower Defense Game (E25 WAVE SPAWNER 2.0) - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello animal consist video including a tower defense game in unity today we are going to be doing something really exciting which is making a wave spawner 2.0 so today we're going to expand the functionality of our wave spawn and quite a bit make it a lot more customizable we're going to introduce this concept of waves where we can edit properties for each individual wave so we will be able to have different enemies for different waves just drag in the prefab so we can change their look and their health their speed stuff like that I will also be able to control the amount of enemies we spawn and the rate they spawn at for each individual wave so really exciting video and let's just jump right into it so I'm here in unity and I want to begin by just getting an overview over how we currently have things configured so let's look under your game master and go to the wave spanish script and you can see here that we currently have a few variables the main problem with this is that we only have this central enemy prefab and that means that we will be spawning the same enemy for each individual wave which quickly becomes pretty boring for our user so let's not now just double click on our wave Spanish script and have a look at how this actually works so you can see again we have this enemy prefab we also have a reference to a spawn point and our wave countdown text that stuff we still need in here then we have this countdown on float and this is probably the most important variable because it's what keep track of when when we need to spawn the next wave and basically we set this equal to the time between waves and then we just slowly reduce it and make sure to display it on all that until it reaches zero in which case we spawn a new wave and then again set it back to the time between waves which is currently just set to 10 so every 10 seconds no matter if we actually kill off the wave or not that's going to spawn a new one and I kind of want to change how that works instead what I wanted to do is only spawn a new wave in case we killed off the last one and actually I want it to be in such a way that the countdown doesn't start until the previous wave is dead and then we can have a very tiny countdown say three or five Ekans until the next wave so I think that just helps separate things that make things easier both for the user and also kinda to separate things out for ourselves so in order to do this what I want to do is keep track of how many enemies we have in the scene because we need to know if we've actually killed off the wave and in order to do this let's just create a new variable let's just make this at the very top and it's going to be public because we want to be able to access it from without the script and we also want it to be static again because we want our enemies whenever they die to be able to change this value so we want a public static int and we'll call this our enemies alive and we want to default that to serum then what we do is each time we spawn an enemy and we have a handy method for that down here we've course instantiate the enemy prefab and then we can go enemies alive plus plus so we simply add r1 onto the current on the existing value so each time we spawn an enemy we now increase this value value now we need to also decrease it when an enemy D spawns and in order to do that we simply need to find our enemy script in here just double click on that and you can see that we have a method here for killing off the enemy called die so each time an enemy dies we want to go in here we want to say wave spawner and again because this is a static variable we don't need a reference to the instance of the class we can simply use the type so we'd simply go wave spawner dot enemies alive and then - - we simply want to subtract one from the current amount of enemies alive so that's really really easy except there's actually one case where our enemy our gets removed from the scene but the die method isn't cold and that's the case where our enemy actually slips through our defenses and gets to the end of our level in that case inside of our enemy movement it reaches the end of its path and then we subtract some lives and we destroy the object and here it doesn't call the any die method so in here we also need to go spawner dot enemies alive - - so there we go so now we actually keep track of the current amount of enemies in our scene and we can use this in here all we need to do is kind of say if and we can do that up here if our so if enemies alive is greater than zero well then we can simply return out of our update function then we don't want to do any of this stuff happening down here and I actually believe that all the code that we need to add so if we just go through this so it's the countdowns going to start at - it's going to go through here it's going to go down it's it's not no enemies are alive and it's not reached 0 yet so it's just going to subtract a bit every frame and it's going to keep doing that until it reaches 0 then we spawn a wave and you can see down here we start spawning that way we wait a little bit between each enemy and each time we spawn an enemy enemies alive is going to increase and so this here is going to be true so it's not going to do anything else and just spawn the wave and set the count down equal to the time between waves it's not going to go down here and draw any of this out on the screen and it's not going to subtract from our countdown until the enemy here dies and we subtracted and or they reach the end of our level until all of the enemies have gone through one of those two things in which case our enemies alive will again be 0 and it will continue down down here again and start counting down so that is kind of on the base of what's happening and it might be a bit confusing because we're kind of juggling both time and amount of enemies and a co-routine and so that stuff can be confusing but of course you can always use the link in the description to get the code of github and see for yourself on your own system so that should actually work now so all we need to do is simply hit play and we should see that for our first enemy is going to reach 0 here and actually didn't reach 0 immediately it actually went a bit for and that's because after we start the curio team here we set the countdown equal to the time between waves we actually continue down and subtract for just a single frame until we reach the top again and all we need to do is just hit return here in order to make sure that we don't pass through all of this other logic when we begin spawning in your wave and that should actually take care of that bug so now when we have play we should see that it reaches zero it stops a new enemy spawns and the second that we kill off that enemy the countdown begins again and when it reaches zero it's going to spawn the next wave let's just wait for that to happen very quickly and we can see that the next wave is now incoming and it should feature two enemies awesome and you can also see that the next wave won't start until both of these enemies are killed off which is now cool so I think with this new system we can easily decrease our time between waves actually I wanted to set it to only like 2 seconds as long as we're just prototyping and then we can always bump it up at a later point so the next thing is our sum allowing for more configuration when it comes to how many enemies we want to spawn each wave and what enemy prefab we want to spawn and in order to do this it's actually quite simple what we need to do is go under our wave spawner and we need to kind of have some list in here more specifically an array which is just a list of items that where each element in the list has some specifications about the wave so um in order to do this let's start by creating a class that will act as our wave all of the data for our wave that we can then have a list of so let's start by creating a new class and we do that by just going on the scripts hidden create c-sharp script and let's call this one wave and double click that to open it up and of course we have to think about well what do we want each wave to kind of have in terms of information what do we want each wave to know and we also have to remove monobehaviour here because we're just using this class to store a bit of information some different variables so first of all we want this wave to have a reference to a prefab for the enemy that we want to spawn that wave so we don't just want to go public game object and we want this to be enemy you could also go enemy prefab will just say enemy we also want an amount of enemies to spawn so we go public integer and we'll do count and finally we want an amount of time that should are kind of a spawn raid actually I think that's better than a time between enemies I think we should have a rate so let's just do public float and then rate and we don't need to input any default values that's totally fine and one thing that we do need to do because we want this to be able to show up in the inspector so that we could edit these these variables inside of unity is we want to mark this as system dot serializable and that just means that we will notify unity this is something that you should save a load and this is something that you should display in the inspector once we actually turn it into an array so when we then go into our wave spawner instead of having our enemy prefab we can then create a public wave and then the array sign here so again a list of items and we want this to be our waves and when we now just save this and hit into unity it's not gonna work just yet and again we are referencing the enemy prefab so I just want to get rid of that so I can show you what's going on okay so let's just finish the logic here and I'll show you what it looks like in the inspector in just a second so now each time we spawn a wave we don't want to spawn an amount of enemies equivalent to our wave index and we don't want to increase our wave index in the beginning we want to do that after we've spawned the wave because the wave index should start at zero and then go one two three four and so on we don't want it to start at one so let's take a wave index here and move that down so that we increment it after our loop and by the way the way that moving lines up and down is by holding down alt and then pressing the up and down arrow keys it's a really handy thing inside Visual Studio and a lot of code editors offer that functionality so we want to increase our Wave index down here and first off let's get the current wave that we want to spawn so simply the way that we want to spawn is of type wave and we can just call it wave and it will be equal to our waves at the wave index is precision so again if it's the first wave we are going to take the first element which has an index of zero so our wave index will be zero and we're going to store all that information in wave if it's the second wave the wave index is going to be one and we're going to grab the second element and store it inside a wave then what we can do is go in here and say that we want to spawn a bunch of enemies and the amount of enemies we want to spawn is wave dot count and so for I is equal to zero as long as I is less than wave count we are going to keep on iterating through this loop and we don't want to wait a half a second between each enemy we want to wait 1 divided by wave dot rate so again we could just have put wave dot time between enemies and then we wouldn't have to do any kind of division we could just input the number but I think using a rate it's just a bit more clean so what we can then do is of course we also need to specify the exact enemy that we want to spawn and so we'll just pass this bat through as an argument so again we'll create a game object here called the enemy or enemy prefab let's just call it enemy and that's what we want to use here for our spawning and we just need to pass that into a function so in here we'll just go wave dot and you guessed it we just go waved our enemy so this is just a much cleaner way of doing things it's not as automated because we have to make each individual wave or level or round or whatever you want to call it but it gives us so much more configuration possibilities and really to create something that's much more fun for the end-user having this idea of a wave that is kind of an object that we've created on our own with these different characteristics then storing a bunch of these waves in the list and then just going through them one by one and using some of the properties in there to create different behaviors is kind of the core of object-oriented programming so if you're able to understand this you're going to be able to do so much with programming and even if you're lagging behind a bit and think this is really hard just right after me no don't worry about it you will definitely get this it was something I thought was really hard to wrap my head around but once you master this you're going to feel like you can do pretty much anything so really great if you're getting what's going on here so I'm just gonna have a sip of water and and we can continue cool so this should actually work and this should allow us now to go inside of unity and see that there's no errors awesome and we can now go into our game master and the wave spawner and you can see that there's now a list in here and we can increase the size of this list that's just import say five and have five different elements here and we can collapse these and expand them and each element again has three properties our enemy the enemy count and the rate they spawn at so now we can populate these and this is really just about you going knots with your animation imagination and just doing as much as possible of course we'll have to kind of balance it in terms of not making each level two easier too hard or having a really big gap between one level and another but it's really just about coming up with a bunch of things that you think could be really fun and varied for all your players so um in order to do this we have to go under our prefabs folder and maybe organize this a tiny bit actually I want to create a totally new folder called enemies and I believe ya actually created this earlier so I have this enemies folder here and I had this idea that I wanted to create at least three different types of enemies I wanted to have a very simple one which is much like the one we have now where the speed is kind of in the middle and he has an average amount of health and stuff like that he's not that special then we can create a really tough one with a lot of health but who might be a little slower and we can create some fast enemies who are have almost zero health and don't give that much money but we can create a lot of them and they'll be moving through the level at a rapid speed so let's take our enemy and enemy death effect and use that as a base for our simple enemies and we can go in here and maybe just add symbol to the end of the names of these objects here just to make it very clear what's going on and we can also find under our materials or enemy material and drag that in as well and add again on the square symbol to that so now we have everything grouped in that folder and I don't really want to change anything about these enemies I think they are working just fine as is so let's go and make a few waves using these this enemy type this dragon enemy symbol let's increase the count to say one for the first one and the rape two to that so that means two enemies a second but again we're only spawning once or really doesn't matter it's just to satisfy my OCD then for the second wave I think we should still keep it really simple and easy so let's do a count of two at a rate of two and for our third element which is of course index two we can maybe create a new type of enemy or we could just kind of bump up the count here let's just get as much as we can out of this enemy type until we are exploring new ones just to let our use to kind of figure things out on its own so five enemies at a rate of two I think that's going to be fine and now for our fourth wave I definitely want to do some variation so let's take our symbol folder here and that's actually just duplicated let's remove the tough folder and let's just rename this to tough that's just easier to have this to work off you could of course create a totally new object but it's much much easier to just duplicate things and then I just modified the things that you need to modify so we're going to rename all of this to tough enemy tough boobs not tough simple enemy tough and let's do the same thing here enemy top and enemy death effect tough cool and then we can take our enemy tough and we can pump up his health let's do something like 300 maybe even 350 I think actually 350 could be pretty cool and we can take our start speed and set it to maybe 7 so he moves a lot slower and also definitely bump up his worth to something like maybe even 140 to really make him much more valuable to to kill and let's now take our enemy tough material and drag it under the materials here so so we make sure that it doesn't use the symbols material and let's do the same with our death effect so if we go onto our renderer on our death affect we have to drag in enemy tough for that death effect and we also have to go into our enemy tough and drag in the correct death effect so that's a bit of that's a few things that you need to make sure that you link up probably but once you do that it's really really easy then next up you could of course increase the scale here to make him a bit bigger and I definitely recommend that you do so if you think it would be cool but what I'll do instead is actually going and edit our material here you can do both but I'll just edit the material and I want to make this a tiny bit more kind of red and maybe even add a bit of metallic nurse just to make it darker and kind of more Grinchy cool so now if we just drag in our enemy tough I think it looks pretty cool and I definitely think it's acceptable so let's just use that as a base and you can always spend more time playing around with this stuff than I do so now under our game master and our wave spawner we can just take our enemy tuff script here and drag it in there and set the count to something like 3 at a spawn rate of 1 so um and for our fifth and final element I'm again going to duplicate the symbol folder let's remove our fast folder here and let's rename this one too fast and again I just want to link up everything so let's start by renaming this stuff so this is going to be enemy fast again enemy fast for the material and enemy death effect fast then we can take our enemy fast here drag in the correct material drag in the correct test effect let's take the death that's affected rag in the correct material there as well we can then change the material to something I think a yellow would be pretty cool definitely wanted to to stand out a bit something like that looks pretty cool I think and I definitely wanted to be bright maybe even brighter than this something like that looks pretty cool and then we can go in here and simply edit some of its stats here so let's change the start speed to something like 20 and I start health to something really low let's just do 15 and we can also maybe set its worth to something a bit less let's say 25 and that should work just fine so again let's go into a game master let's drag in the prefab let's set the count to something much higher let's say on 10 and set the raid also to something really fast like 3 awesome so that should actually already work and if we just go in here now and hit play we should see that on these waves are indeed spawning so there's the first guy and let me just show you that the second ones are working as well indeed they are but before we go through this we have to fix one bargain I'm just going to go out and say that there is a bug so that we don't have to go through the entire level only to find out that something isn't working and that is when we reach the end of our levels so when we don't have any more waves prepared meaning that we've gone through all 5 waves it's going to increment our wave index so the last level has an index of 4 and so the next level which doesn't exist is going to have a wave index of 5 and when it then goes through here spawns the wave and tries to get the wave here it's going to be unsuccessful because if we try and get the fifth element of our wave array it's going or the sixth element with an index of five of our wave array it's going to give us a null reference exception because that doesn't exist so what we need to do is add just a tiny bit of functionality basically just an if statement here is saying that if wave index is equal to armed waves dot length so if it's equal to the amount of total waves that we've created well then we want to end our level we could just say you won the game or we could transition to a new level and you guys been requesting me to do multiple levels for a long time and it's definitely something that I would like to do the only thing that I'm kind of waiting on is a bit of graphical updates I want to create some more interesting graphical pieces before we go on to creating new levels but I definitely want to leave some code here that allows us to easily transition to a new one so in order to do that all we kind of want to do here is just throw a debug deadlock statement with something like wave or a level one and of course we want to call something on the game manager or something like that but for now we'll just say that in here and then we want to disable this script so that we don't keep on spawning new waves and to do that we simply go this dot enabled equals false and we haven't had a look at using this stud enabled really but always saying is that this script we want to disable it cool so we can save that now and things should work without any kind of weird errors so let's just hit play here and enjoy what we've been creating so that's C here so let's create a standard turd first I think that's kind of a good base and one thing that I know is that our missile launcher works pretty well against the standard unit so I'll go ahead and create one of those if you'll shoot awesome and I know that this laser here works really really well against um kind of a really really quick units because it shoots continuously but they don't have a lot of health it it's really really a great against fast opponents who might just escape the bullets of some of the other turrets so I think we have a pretty good defense setup here and of course right now we you can see there how effective the laser turret is on trying to create some or kill some of these enemies and now that we have through all five levels you can work all five waves you can see down here that it says level one and that's awesome and we can transition to the new one so it's just gonna be stuck here and wait till we do something with it you can display a victory screen or you can make your own new level and transition to it that's totally up to you but at least I think this was a really really important upgrade so that's pretty much it for this video I hope you enjoyed it and you find a lot of fun ways to play around with this really it's only your imagination that should stop you at this point and yeah thanks for watching and I will see you in the next video thanks to of the awesome patreon supporters who donated in November and a special thanks to sultan al sharif faisal arrow fire james Calhoun and Robert Bonham become a patron yourself at patreon.com slash practice
Info
Channel: Brackeys
Views: 54,469
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, material, materials, beginner, easy, settings, how, to, howto, learn, course, series, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, tower, defense, td, defence, wave, waves, spawner, spawning, spawn, customizable, different, levels, editor
Id: VecEiv7e6PM
Channel Id: undefined
Length: 25min 41sec (1541 seconds)
Published: Wed Dec 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.