Simple Wave spawner in Unity 2D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll make a simple wave spawning system and well that is it let's get started so hey this is chrome av and this channel is here to make you just a little bit more better in game development all the links mentioned in the video will be in the description box down below and if you have not subscribed then do so by clicking the subscribe button and pressing the bell icon to never miss the latest videos so with that said we'll make a wave spawning system in this video and here is how we will do that so we'll have a spawner system in which we can have multiple waves and in each wave we can set up how many enemies we want to spawn what kind of enemies we want to spawn that means what type of enemies in a particular wave and how fast we want those enemies to spawn maybe the first way we want the enemies to come slowly and then in the second wave maybe you want it to be a little bit harder or more tough and make it spawn a little bit more faster so we will do all that and after a wave spawning is completed we will wait until the player kills off all the enemies after the player kills off all the enemies an animation will play which will just tell that wave is complete then give the name of the next wave then go 3 2 1 go and then we will start spawning the next wave after you finish spawning all the wave and killing all the enemies everything is done your game will be finished now with that said let's go to unity and start making the script so i have a simple scene here where we have a wave spawner and the wave sonar has a wave spawner script we will set this script up in this video basically and inside the wave spawner what we have is we have some spawn point all of this are some spawn point and we will spawn our enemies randomly into these random spawn points so that's why we have our random spawn point first of all let me show you the enemies so these three are the enemy this is a bullet these three are the enemy and all of them are in the enemy tank all of them are in the enemy tag after that everything else is just normal so let's go to our script make a script alright called wave spawner or anything whatever you like so let me open it up in visual studio now inside visual studio first of all what we will do is just above our main class we will make our own custom class and we will call it wave and after that write public string wave name so w-a-v-e and a-m-e wave name this will be the name of a certain wave right so uh for now after just doing this let's just call it in our main class by writing basically public or maybe serialized field so serialize field wave and let's just call it wave once i do this let's just hit save and go back to unity let it load but as you can see we do not see that from our inspector right we don't see it in the inspector and the way we can make it see in the inspector is by just going up here and in a big bracket write system dot serializable after writing that hit save go back to unity and now you can see a wave and we have a wave name now we don't want just one wave we want multiple waves and to have that we just need to make it a wave array after doing that again go back okay now we have multiple waves for this video we'll only have three different waves so let's give all of them name i'm gonna give my first one wave one wave two and the last one will be called final wave right now these names just survey purpose to just give this a name if you don't keep anything here it'll say element 1 element 2 and so on right but if you do this it just does this and it looks more easy to organize for now it works like this but afterwards it will have some use as well now we know the name of our wave but we need to know how many enemies do i want to spawn in a certain way so public integer and let's call it number of enemies so let's set that up as well let's go back in unity here all of them will have the their own number of enemies so let's say the first wave we will have only three kind of enemies in the second wave we will have five different kind of enemies and in the third wave we'll only have one enemy but it will be the boss enemy now we want to know what different kind of enemies do we want to spawn for that write public game object and make it an array of game object and call it type of enemies after that we don't want to spawn all the enemies at once we want to spawn them in a certain spawn interval so here right public float and let's call it spawn interval so after doing that let's just set it up in visual studio now all of them will have its own different type of enemies and spawn intervals so in the type of enemies for the first one let's go to prefab right and let me just click on here and click here so that this won't go away now for the first one for now because it's it for testing right the first one will have all the enemies so let me just drag and drop all three types of enemies i have two normal type of enemy and a boss got it so we'll randomly pick between these three type of enemies right let me just close that and the spawn interval let's say we will spawn one enemy after every two second for the first one for the second one we will only put two so let me just drag to and it will be spawned a bit faster so with the interval of one second the third one will be our boss so only one object and it does not need a spawn interval because we only have like one object to spawn so after hitting that hit save now down here we don't need to work on this anymore we can even close this that looks kind of clean so after that down here what we need to do is we need a serialized field meaning public you can write public let's let's just write public so that will be easy public here as well public we need a just like we have array of waves right we need an array of our spawn point we'll randomly choose which spawn point we want to spawn the enemy in so here right public transform and let's say spawn points and by the way it will be a attic so do something like this now if i go back into unity for our wave we have already set up all the waves so let me just close this off as well it looks more cleaner now and in our spawn point we have it locked so let's go to our spawn point click on spawn point i'm going to copy and drag and drop it until i see that cursor you won't see it anywhere else once you reach that place spawn point you'll see a cursor be like that and leave it you'll have all of them dragged and dropped basically so we have that done as well now we want to know which way we are in meaning we this is the like all the waves we are we will ever have but we want to know every individual wave are we in the first wave or the second wave or maybe even the in the last wave we will not know that so for that we need a private and let's call it wave and let's just say current wave and we need one more thing private integer and current wave number now when this is 0 we will know that it's the first way when this is 1 we'll know that it's the second wave and so on this will help us know which way we are in right these two basically will help us know which way we are in after that in our update function let's make another function called spawn wave so void now in this spawn wave so whenever we spawn a wave what will happen is first of all we will take a random enemy from this type of enemies right then we will take a random position and then we will spawn an enemy so first of all let's take a random enemy from our current wave so before that we need to set up our current wave so how we can do that is by writing current wave is equal to waves and inside the big bracket we will write current wave number so that is going to be the current wave whenever this is zero current wave will be the first wave whenever this is one current wave will be the second wave and so on that is how it work after that write game object and let's call it random random enemy is equal to current wave dot type of enemy and because even that type of enemy is an array we need to give specific type of enemy and that will be a random dot range and the range starts from zero to our current waves type of enemy dot length now we need to spawn enemy in the random position so for that right transform random point let's call it that and that will be our spawn point and inside the big bracket a random dot range starting from zero and ending on spawn point dot length after that what we need to do is we will now instantiate our basically random enemy on a random point dot position and for the rotation we are not going to do anything so quaternion dot identity now this should basically infinitely keep on spawning our enemies but it will be a random enemy at a random point basically so if this works we'll go to the next stage so let's just call it so now what we have call it let's go back to unity and here let me show you so it will spawn basically uh okay these three it will spawn all of these three but randomly so let's see if it works or not and as you can see we are spawning so many enemies but randomly okay after that now we only want to spawn three enemies so what we will do is first of all we will have a boolean called uh can spawn maybe yeah so let's just call it can spawn and whenever we start the game can spawn will be true so down here in our spawn wave right if we can spawn then we will spawn these enemies so now while can spawn is true we will spawn these enemies right and every time we spawn these enemies what will happen is current wave dot number of enemies will be minused by one meaning minus minus and now if meaning when the number of enemies in our current wave will be equal to zero our can spawn will be false basically we will be able to spawn our enemy until our current waves number of enemies will be zero in our first wave so once we spawn three enemies this spawning will stop so let's see if it happens or not as you can see we spawned three enemy and it stopped let me show you again three random enemies just here as you can see now we don't want all of them to spawn at once so we want a time interval so for that we need a private float first of all private float called next spawn time and now here if we can spawn basically in here if we can spawn and our next spawn time is actually less than time dot time so if both of these conditions are met then we will spawn an enemy and after spawning every enemy what's gonna happen is we will basically give it a timer now how we will do that is by writing next spawn time is equal to time dot time plus our current waves uh spawn interval let's go back click on play and if you see now the first enemy takes two seconds the second enemy takes another two seconds the third enemy and all of them get spawned in random positions which is great so now after doing that we want to now start spawning the next wave but we only want to spawn the next wave after we kill off all the enemies from the first wave so how we are gonna do that is first of all we need to know how many enemies are there in the first wave right how many enemies did we spawn so to know that how we can do that is by writing game object and let's call it total enemies after that write game object dot find and the find will be find game objects with tag not game object with tag because we want multiple different objects so find game object with tag let's click on all of them and we will find those game object which has the tag called enemy right now it's saying something is wrong the reason why is this has to be an array for that to work so now it will take in all the list of the enemies that have been spawned first of all and are in the enemy attack so this will just keep a track of all those enemies after that what we can do is we will write if if the total enemies dot length meaning the total number of total enemies is equal to zero right and we cannot spawn meaning can spawn is false right because can spawn will be only false after we finish spawning one wave of enemy because of this so if the total enemies is zero that means we killed off all the enemies and we cannot spawn the next enemy then what will happen is your current wave will be added by one and then oh no your current wave number will be added by one current wave number will be added by one not like that actually just do this yeah this will work and then we will basically say can spawn is again true now after doing that every time we kill off all the enemies it will start the next wave so let's hit save let's go back to unity now let's hit play so we will spawn the first wave i'll just kill all of them one two both of them killed one last enemy left and after that we'll have to spawn five enemies because that's the second wave so we spawned five enemies it has stopped let me kill off all of them and finally the last wave was the boss so it spawned the boss as well now what is happening here is that we just went above our total number of waves basically and it's giving us this error so let's fix that as well so if we have killed off all the enemies right and we cannot spawn and the current wave number plus one basically current wave number plus one is not equal to is not equal to wave dot length meaning the total wave until your current wave number right will not be equal to the total wave meaning until we have not reached the final wave right we have not finished all of the waves this will keep on spawning if this will be equal to length then we won't spawn anything now if i just do that again and by the way i'll just delete it from here by the way it'll be more easier yeah done three enemies gone now the second wave three four and five yeah so that is also gone and we have the boss once i kill it we don't see any kind of problem here which is great after that now let's set up an animation that will help us basically do the same thing so but before we do that let's just put these two in a function because we will call this function that will do these two things from our animation so let's make a function called void spawn next wave basically and i will just copy this and then paste it here now back in unity click on your wave spawner and make those animation in your wave spawner that'll be way more easier so to make our animation first of all we need a canvas and some text you can make it however you like by the way just follow along for now so i'm going to put in ui and a text right we have a text ui now i'm just going to press f click on it and just remove this lock so that we can see that now here i'm just going to write a wave name we will set this up so that it will show us different waves according to which wave we are in after that let me just rename this to wavename as well i'll just put it as 164 and i will turn on overlap on both horizontal and vertical overflow overflow basically okay i'll have a let's see which one looks good okay this one looks good yeah so let me just put it on the center as well okay now this is too big the reason why is because of we are putting constant pixel size in our canvas scaler put it as scale width screen size so now it will scale with the screen size so we need to put our screen size -16 is to 9 that means 1600 by 900 that will give us a perfect size as you can see now our first one is done by the way i need to change the color to kind of white yeah okay now the first one is done so i will just make a duplicate of this and close the first one so the duplicate will be called wave complete and we'll write wave complete here as well everything else is already set up so we don't have to worry about that after that i'm going to make a empty folder and just call it three comma two comma one and comma go so let's make a duplicate of this wave complete and let's just close this off as well so we've completed let's put it inside here and call it basically three write three here as well after that two and two here as well after that one and one here as well and lastly we need a go go [Music] go okay now after we do that i'm just gonna close off all of them so let's see how it looks in our inspector i mean in our game view so it looks kind of fine all right after that go here and in our animation panel let's click on animation create animation and i'm going to put it inside a folder that i made for animation so the first animation will be our idle animation so idle anim let's just call that and the idle animation is just for us to transition from one animation to another one so the idle animation is done after that we need another one called wave complete that's the second animation first of all let me just set it up so in our wave complete what's going to happen is we will show this basically in our scene meaning just turn it on for a while and then turn it off just record this click here then turn on at the start and in about i don't know one second of time turn it off when you turn it off you can also do one more thing you can just make it small as well make the size 0 as well what's going to happen is it will look something like this right we don't want it to be something like this we just want it to be this size till here so what we will do is we will just click here copy that ctrl c and you have this white line here on 40 ctrl v and now it will look something like this and that is exactly what i want so our wave complete animation is done let's go to our animator and let's just make a transition from our idle to wave complete now let's make another one and this will be called three comma 2 comma 1 and go now here what we will do is first of all we will say the next wave name so we will turn that on so let me just record it turn it on and so the next wave name and it will go on for like a second basically right then turn it off next wave name ah let me do something like i don't know let's make it small as well just like the before one okay wave complete after we have closed this turn on three basically turn on three and go to another two second mark then turn off three turn on two go to three second mark turn it off turn two off then turn on one go to about i don't know where is it four second mark turn this guy off then turn go on and finally in about a thirty second timer turn this off as well so your animation will look something like this wave name three two one go it'll look something like this so now in the animator let's just make a transition from this and then after we finish this animation we'll basically go back to normal now we need a something that will help us transition from this to this animation so for that we need a parameter let's call it trigger and let's just call it wave complete so whenever the wave complete is triggered we will go from idle to wave complete now for going to idle to wave complete we don't need any any exit time and we don't need any exit duration as well but for here from going from wave complete to three to one go we need an exit time which will be we'll play the whole animation so one but we don't need an exit duration so zero do the same here as well and now our animation is done so what will happen when you're idle your idle once you kill all of the enemy it will say wave complete then we'll complete turns off and three two one go now finally what we will do is in here in our three to one go at the last point just make an event add an event and this event will find all the functions in our script basically so in this event you will see your spawn next wave function click on that so what's going to happen is after we end this animation we will start spawning the next wave now let's just go here and first of all make a reference to the animator which is inside here now once we make the animation it will have an animator inside our wave spawner so let's make a reference to the animator here down here just write public let's do it here so public animator and let's just call it animator after that we want to show which way we are going to go to right so for that we will basically make a reference to this wave name that we made right here so for that let's just make a public and we need for this to work we need a reference to the unity engine dot ui in visual studio it does it automatically once i just call this as you can see i have unity engine dot ui but if you don't have that you need to write something like this all right and let's make a text and let's call it wave name let's just set it up in unity let's go back let's go here wave spawner and right now let me just close off all of this okay right now we have two places one has the animator so drag and drop your animator which will be just below your script basically and then we have wave name which will be our wave name now once we kill off all the enemies and we have already spawned all the enemies and still some more waves are left then first of all we will just name the next wave give a next wave a name right which will be equal to our wave name of that next wave so that is how we're going to do that and how we are going to write that is by writing wavename dot text is equal to waves and inside we are going to write currentwave and do a plus one and dot wave name sorry not current wave i just mistakenly wrote that we need to write current wave number so current wave number plus one dot wave name what's going to happen is it will just give us the next waves wave name and put it inside our own wave name this this guy right here in the wave name so after that we will call the animation so animator dot set trigger and the trigger we are going to set is going to be a wave complete now after doing that if we just hit play here so we will spawn the first wave of enemies as you can see spawning the first wave of enemies which is awesome let me just kill off all of them and we'll say wave complete wave 2 3 2 1 go but this happens but it is still working the reason why that is happening is a small problem and because of that all of these get spawned all at once but as you can see it is working as well as it showed us the next wave name right how we did that is just it took the this value and added it into this value basically so if you want to see that work again uh let me just change this to something else which will be maybe sk do do so if i just hit play kill off all the enemies from the first wave so yeah i just kill off all of them wave complete skid doodle three two one and go it will say that again yeah there's still a problem now the problem is that once we actually do this there is no way of stopping this this if statement keeps on running until you go to the next wave right but until then this function has already been called about two times giving us that so that is why it's not working so instead of this can spawn right let's just remove it for now we need another boolean called private bool and let's say can animate should can can animate and from the start it will be false we will only be able to animate once we first spawn the first wave so here can animate will be true now if the total enemy is 0 and we still have more waves to spawn as well as can animate is equal to true then we will spawn the enemy and once we do that after that we will say can animate is equal to fonts now we are almost done by the way everything is almost done because if you see here mostly all of them is done you will see so first of all let me just kill it off from here one two and three wave complete ski doodle okay i have to change that by the way three to one go and we spawn the next wave let me just kill off them as well and okay okay wave complete final wave three two one go and we will spawn the boss where the hell is he okay the boss so once we kill off all of that now finally we want something to happen after we kill off all of the enemies that means all the waves are finished we want to say something live wave complete something like that right so let's go back in unity and let's set it up here so what we need to do is if i just write here else so else if else if these conditions are not met we can write debug.log and let's just say game finish now if i just do this what's gonna happen is the moment any one of this will not be true right any one of them will any one of these conditions will not be true what's gonna happen is it will say game has finished and we'll start saying from the start see it keeps on saying it so what we need to do is we need to first of all write first of all if we kill off all the enemies we have to kill of all the enemies to trigger this as well as this so let's put it inside here so let's make another if statement and let me just drag and drop these guys here and inside here let's just copy this cut this and paste it here and inside here cut this then paste it here but still it will be a problem because if either of these will not be true then it will directly go here right so one last thing i have to do is now write another if statement and just copy this guy down here and after that let's just cut this and passed it here okay now what's gonna happen is if we have killed all the enemies and we don't have any more waves to spawn right then game is over game finish you should put something here like some kind of a ui that will help us go to the next level or maybe your whole game is finished something like that but for now i'm just going to print game finish and with that done our whole wave spawning system is done which is awesome so one last time i'm just gonna play with this so we have our first three enemy i'm gonna kill off all of them two and three wave complete i have to change that by the way three two one go the second way which will be five enemies four and five one two three four and five wave complete final wave three two one go and we have our boss boss okay once i kill that as you can see here game is finished which is awesome so thank you for watching i hope you liked the video if this video was too long i'm really sorry my explaining i think it went really long but hey if you like the video please hit the like button subscribe can we reach 1000 subscribers that would be awesome with that said you can download this this whole this project basically i'm just gonna put this all in my h.i.o pace and please follow me there as well i keep on updating these asset bundles every time i make a new video so please do follow me there as well and with that said again thank you for watching i hope you like the video like share and subscribe have a great day bye baby [Music] you
Info
Channel: ChronoABI
Views: 26,305
Rating: undefined out of 5
Keywords: Simple Wave spawner in Unity 2D, Simple Wave Spawner, enemy Wave Spawner Unity 2D, Wave Spawner, wave spawner unity, enemy wave spawner unity, Wave spawning system, Game, Dev, Enemy wave, enemy waves unity, enemy wave Spawning system, game development, unity (software), unity tutorial, unity wave spawner, unity wave spawner script, unity endless wave spawner, unity 2d wave spawner, Unity 2D Wave spawning system, Unity 2D simple wave spawner
Id: pKN8jVnSKyM
Channel Id: undefined
Length: 35min 8sec (2108 seconds)
Published: Wed Jul 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.