How to make a Wave Spawner in Unity 5 - Part 1/2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello and welcome to this video on creating a wave spawner inside of unity using c-sharp so this video is not part of any particular series because I thought I could describe the process of making a wave spawner in pretty general terms so on the script that we are going to be making today should be fairly generic and therefore very easy for you to modify to suit your project and well before I get started I quickly want to show you just what we are going to be creating and how you can easily download it if you don't want to be creating it with me today so if you simply want to use this in your own project then you can go to brackish comm and you can navigate under the 2d platform of course it's also under projects click on that and I know this is in the 2d assets pack but it will also work just fine for 3d games you don't actually need to change any code in order to make it work for a 3d game but anyway you just go ahead and download that and I will make sure to put the finished product inside of the 2d assets pack once downloaded and then scripts and I'll make a folder here for the wave spawner cool so now that that's all said and done I think I quickly want to jump into unity here and show you what it is we're going to be making so you can see here I have a currently 3 spawn point set up in my level and you can see that I've just make sure that they have this icon so we can easily see them in our scene view and then on the game master object I've been using for the game here we have a script called wave spawner and this is the central component of course and this has a wave class which is then stored in an array called waves and we can easily add more waves by simply changing the size of this to 4 to add another one and each wave currently has a set of properties these are a name the enemy prefab that we want to spawn the number of enemies we want to spawn and the rate that we want them to spawn in and it's very easy for you to add more stuff to this if you want to have more kind of possibilities for each wave so and you can see I've just quickly made three waves here and they are all using the same prefab which is the alien spaceship one and all of this other stuff that I've set up here is part of the 2d platformer course this entire project has been made in a series of videos which you can of course check out so check out the 2d platformer course so um we have these waves we have a list of an array of spawn points which are simply dragged in then we have a time between waves which we can tweak while the game is running too then we have a next wave spawn time or a countdown timer if you will and this shouldn't be touched it's simply there so we can see when the next wave will spawn and this is something you would typically put in the UI some kind of big countdown timer up and there isn't there are no more enemies left and then we have this state and this is simply an enum which shows us whether or not we are currently counting down whether we are spawning the level or whether we're waiting for the player to kill off the enemies cone and that again shouldn't be touched it's just there for display purposes so when I go ahead and hit play now you can see that this goes up to five starts counting down and one once it reaches zero it spawns two enemies and you can see they spawned at the same point and then it starts counting down again once I've killed them and you can also follow what is going on on behind the scene it's down here and you can see that the system keeps going even though I get respond and all that so you can see we've completed it counts down again and here comes the final wave and this is a tough one I'll see if I can do this I could and you can see it says wave completely no more waves and it starts looping so it will simply spawn the next or the first wave and simply keep going that way and one thing you could do here is simply have a game game completed screen if you want to make all the waves yourself or you could also have kind of a stat multiplier that would make the waves harder each time it looped through so that's all something you can add onto the system but this should provide a nice base cool so let's actually take a look at creating this script so let's remove the component here and oops it looks like I have some malware it's currently being deleted and it's a weird thing Windows 10 is doing and I'll quickly remove the wave spawner script here and also will remove the spawn points that I've created and so we should be starting from scratch here so right now when we hit play there is nothing on the GM object and nothing is going on in the console and we actually have nothing set up cool so to start this off let's go ahead and create a wave spawner script and go onto new script and select c-sharp you create an ADD and let's double click this to open it up and I'm actually not using a mono develop here instead I'm using a visual studio and I also have a video on setting that up if you wish to use the same monodevelop should do just fine the namespaces we are going to be using here are unity engine and system dot collections because we are going to be using AI enumerators and well we can go ahead and kill off these methods for now but we are going to be adding them again in a second but first off I simply want to focus on the very first part of creating this wave spawner which is defining what a wave in our game should be so so in order to do that that we need to create a custom class and I'll just put this inside the wave spawner you could also put it outside that's actually completely up to you and I'm simply going to call this wave and then we are going to open up some curly brackets and inside of this we're going to have a a couple of variables the first one is going to be the name of the wave this is cool to have if you want to announce what wave is currently coming as you saw I did that I didn't display it in the UI yet but I had it on displaying in the console then we want a reference to the prefab that we want to instantiate at our spawn point so that the actual enemy game object itself and and for that we could simply do a transform reference and we'll simply call this enemy then we want a public and this is going to be an integer and that's going to store the count simply and the amount we could also call it I'm simply going to call the count here and actually no let's do count I like it better and then finally we'll have a public float which is going to be our rate a spawn rate and we could also do a delay here but I find it a bit more logical to have a spawn rate and then simply do one divided by the rate it's basically the same thing okay so one thing that we will have to make sure that we do with this class I mean you can put all the stuff in here that you want to and the stuff that we are definitely going to be needing in this tutorial is enemy count and rate other than that you can configure this just the way you want to but one thing we will have to do is mark this as serializable so we are going to say system dot serializable up here in square brackets and what this does is simply allows us well basically what it means for us at least is it allows us to change the values of this of instances of this class inside of the unity inspector so when we now go on to making a public wave array and call this waves then while we are including a when when including this tag up here we are actually going to be able to edit it inside of unity so you can see here that we now have this waves array and we can start adding waves and we have all of these properties to edit if we went ahead and deleted this well you can see that that list or array simply disappears so we need this in order to edit it inside of unity cool so that's all fine and dandy next up we are going to create a private integer and this is going to store the index of the wave that we want to be creating next so this is simply going to be called next wave and it's going to be deep is simply going to default to zero cool then we want to have a public float that stores our spawn time between waves and we're going to default that to five seconds and yeah of all of these time measurements are in seconds and next up we want to have a private actually let's just make this public so we can see it in the inspector just to see if everything is working but we will mark this at private when once we're done and this is going to be the wave countdown and that's simply going to default to zero or you could just close it off which I'm going to do cool so now in the start method void start Wow cool oops alright my microphone sometimes gets in the way which makes it kind of hard to type cool so inside of our start method we want to set the wave countdown equal to the time between waves so that's going to start up at 5 seconds then we are also going to have an enumerator or an enum I should say that stores the current state of our spawner but we'll make that in a second first off I want to declare an update method jesuschrist okay an update method and inside of this we are simply going to write that if wave countdown it's less than or equal to zero so if it's time to start spawning waves and then we want to check if we've already started spawning waves so this is where we kind of need that state so because we could do a method here called spawn wave and give it the wave that we wanted to spawn but then we would be spawn start or then we would be starting to spawn a wave every every time the computer to refrain so we have to make sure that we only start spawning a wave one time and in order to do this let's declare an enum here up here and that's going to be a public innum and we are going to call this spawn state and it's going to store on the values spawning waiting and Counting and these do not need to be capitalized then down here we can check that if state o we also need to store this in a variable of course so up here with simply finding the data structure now we also need to actually make that a variable so we'll make a public spawn state called state and default it to spawn state dot counting awesome and I'll change that to private I don't think we need to be viewing that in the inspector so if it's time to start spawning a wave and if state is equal to and if state is not equal to spawn state dot spawning well then we want to start spawning a wave so here we want to start spawning wave then if we haven't reached 0 yet then we simply want to say that wave countdown minus equals time dot Delta time so this will simply make sure that we are go down the appropriate amount of time for each frame and this will make sure that our actual countdown timer will be kind of relevant to time and not to the amount of frames that we drop a second cool so now we could actually go ahead and set up the method for spawning a wave so this is going to be using an I in numerator and the reason for this is that we want to be able to wait a certain amount of seconds inside of the method and in order to do this instead of writing void we write I in numerator and I'm not going to go into exactly what in enumerators actually do and kind of how they work behind the scenes I'm simply going to say that this is the method this is the way of creating methods that will be able to wait a certain amount of seconds continuing and in order to use this you have to also include system data collections so we're going to make an enumerator method and we are going to call it a spawn wave and it's going to take in as an argument a wave which we're going to call underscore wave and eye enumerators basically need to return a value so down here we'll simply do yield break and that will return nothing and that will make sure that we don't get this error that popped up here you can see that not all code paths return a value will simply make sure to end your method with yield break and that should go away cool so basically what do we want to be doing here well first off we want to set our state equal to spawn state don't spawning so now we are actually spawning and when we are done spawning then we want to spawn but a bunch of things and once we are done spawning we want to set state equal to spawn state dot waiting because now we are waiting for our player to kill off all of the enemies then here we want to do a spawning logic but let's first make sure to call this so up here we want to start spawning away well instead of just doing spawn weight and then putting in the wave that we are going currently going to spawn because this is an enumerator we simply need to say start co-routine and then in here we call the method so in here we say spawn wave and we give it the wave that we want to spawn which is currently our waves with an index of next wave like that okay so instead of simply calling this and using a semicolon because this is an enumerator we need to call the start corroding method and that is going to do all of that for us awesome so now in here what we are able to do is we are able to loop through on the we are able to create a full loop that will run the number of enemies that we want to spawn so it will make sure to loop through a count number of times and account this of course equal to the number of enemies that we want to spawn so for I equal zero I is less than wave dot count then we want to increment I and in here we can then say that we want to spawn an enemy so we are going to create a method for that also and we could all do all of this inside of this method but I think it's it's much easier to read if we have a methods for spawning waves for spawning enemies and for searching if checking if enemies are still alive and doing all of that in separate methods just make the code much easier to read so here we're going to have a spawn enemy and that's going to take in as an argument an enemy of some sorts I actually have an enemy class but we declared our allies as a transform and we're going to call it underscore enemy and in here we want to spawn enemy and for now we are simply going to debug deadlock spawning enemy and we are going to print out the name of that enemy so that's going to be fine for now lady will add spawn points and make sure that they are actually instantiating so in here we'll simply call this spawn enemy method and the enemy that we want to be spawning is wave dot enemy and then we want to wait a certain number of seconds so right now if I just go through this will begin at the start method it's going to set our wave countdown equal to the time between waves which is defaulting to five seconds then it's going to go down here and it's going to say well we are not at zero yet so it's simply going to subtract and the number of times or then the amount of time that has passed since the last time we drew a frame meaning that it will simply subtract one of one measurement each second from this wave counter so it's certainly going to count down from five and if it hits zero then we want to check if we've we are already spawning and the first time we of course not going to be spawning so therefore it's going to start spawning which takes us down here and it's going to say well now we are actually spawning then it's going to loop through the amount of enemies that we want to spawn and for each enemy that we want to spawn we are going to call the spawn enemy method which which takes us down here and it's going to instantiate an enemy which we haven't done yet for now it's simply going to write down the in the console that we are spawning an enemy and then we go up here and then we want to wait a certain amount of seconds before looping through again so before continuing to the next iteration of the loop we want to put in a wait four seconds and this is why we put ienumerator here because this allows us to do this yield return new waitforseconds and in here we can simply put 1 / wave dot rate so I know this looks very weird and I know this test you simply ignore it simply know that whenever using an I in numerator we should end with yield break and whenever we want to wait a certain amount of seconds we put in yield return new wait four seconds and then the amount of seconds that we want to wait and in this case that's going to be equal to one divided by our rate or if we had put in a delay we would simply put in wave done delay but that's completely up to you so it's going to wait this certain amount of time and then spawn another your enemy and then wait again and spawn another enemy until we've reached the point where we have as many enemies alive as we want it to spawn and then it's going to set our state equal to spawn state dot waiting you
Info
Channel: Brackeys
Views: 157,417
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, beginner, easy, function, how, to, howto, learn, learning, course, tips, tricks, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, wave, spawner, spawning, spawn, waves, system, generic, asset, Unity (Software), intermediate, brackeys, make, unity 3d tutorials for beginners, unity tutorial, unity3d tutorial, c# tutorial, wave spawner, unity 5 tutorial, part 1, 1/2, enemy, enemies
Id: Vrld13ypX_I
Channel Id: undefined
Length: 22min 49sec (1369 seconds)
Published: Sun Aug 30 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.