How to make a Tower Defense Game (E03 Wave Spawner) - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello and welcome to this third video on creating a tower defense game in unity so today we are going to be creating a wave spawner that will of course spawn in waves of enemies so that's going to be a lot of fun we are going to be creating a very bare-bones version of a wave spawner because we're only going to have one type of enemy that we will spawning in groups and we're only going to have one spawn point so it's it's very simple actually if you want something more configurable that has all of these stuff like you can have different levels with different enemies and different spawn points well then you should check out my two-part series on the channel on creating a wave spawner it's kind of a standalone miniseries and it has all the functionality that you're going to need basically but we are probably going to modify this anyways to to do more stuff later so without further ado I will just show you here before we get started that the project is now in github so if you go to the link in description you can go and download the entire project with everything that I've made and you can just open it up in unity or you can grab scripts from it or whatever you want so if you have a trouble with something I just want to mess around with my project you can go here hit clone or download and download a zip file or you can of course use github to clone it as we go along and if you want an older version then you can go to commits here and you can select whatever version you want and you can see right now there's only video - that's of course going to be video three four and five and so on but video one is not actually here I'm sorry but I forgot to do it with the first video so everything starts at video number two sorry one happen again good so without further ado let's open up unity and get started with the video so let's right-click and hit create empty and let's rename this to game master and this object right here is going to have different scripts that manages the game we could also call it game manager or just manage your scripts or whatever I'm going to call it game master and let's make a new script on this cold wave spawner and then we can have just a series of scripts here so let's double click on that to open it up in Visual Studio and the basics of a wave spawner is in fact well pretty basics we are just going to use the instantiate command pretty excessively and then make sure to time it in some way so what we're going to do is we're going to delete these two methods to begin with and then let's make a public and we're going to make this a transform and this is going to be the prefab of the enemy that we want to spawn so this is just going to be our enemy prefab and this is what we could later turn into some kind of level that will rise and difficulty or some different enemies that we can choose from but for now let's just choose this one single enemy that we've made and then we want of course on some public float which is going to be the time between waves or the countdown timer or whatever you want to call this and this is going to be just for testing purposes I'm going to set this to 5 but in reality you probably want it to be longer then what we want to do is we want to go into the update method and we want to have some kind of way of managing time and what I thought we would do is create a private variable here a private float that we call the countdown and we might want some kind of way of getting this number or using this number to display it on the screen but for now it will just be behind the scenes and we might change this in this video in fact if we are fast and this is going to start off at whatever you want the time however long you want it to take before it spawns the first wave so that might not be 5 or that might exactly be 5 so I'm going to set this to 2 so that it takes the 2 seconds until it spawns the first wave and then what I'm going to do is I'm going to go into the update method here and I'm going to say that if countdown is less than or equal to 0 so if our timer has reached the serial mark well then we want to start spawning some enemies so we are going to make a a method down here that is going to say spawned wave and that's going to be responsible for spawning an entire group of enemies right so right now we can just use a debug that likes statement here that says a wave incoming something like that and then we can close that off and then here we want of course to call the spawn wave method that we just created so that will jump down here and execute this code and then we want to set the countdown timer again because we don't want it to stay at zero and then just keep counting down to negative one negative two no we want this to be set then to the time between waves so um and then we need some kind of way of decreasing this countdown timer we wanted to go down by one every second and we want to do this every frame so we can say that countdown - equals time Delta time and again timed at Delta time is the time amount of time passed since the last time we drew a frame so if we just do this it's going to mean that it will reduce countdown by one every second and we'll do so fluently so what we are doing here is we are creating a countdown float that is going to decrease with time and when it reaches zero we spawn a wave and we reset it to five seconds okay so that's all we're doing and let's try and save this and see if it actually works Oh our spawn wave spawn is here let's make sure to find our enemy prefab and drag it in here let's make sure to delete these enemies and now hit play and we should see that after two seconds we get a wave incoming and then after a following five seconds we should get another wave incoming and that was so that's perfect so whenever we spawn a wave we basically want to spawn a group of enemies and to determine how many enemies that's going to be there's lots of different stuff that we could do we could either make an array of waves with an enemy type and the number of enemies we want to spawn and the delay between them and all that and that's what we do in the wave spawner a short series so you can check that out if that's what you want or if we are lazy we could use the wave number the number of waves that we've been through that we've encountered to tell how many enemies we want to spawn so that the difficulty will increase as we go through another way and you can do this using some kind of function you could use some kind of polynomial that's not like for we will take the wave number and we will square it and that will be how many enemies we will face but we could also just use well the wave number to determine how many enemies it's going to be so the first wave wave will be one enemy and the second to and so on so that's what we're going to do for now and then we can come in and change it later which we will probably do so the spawn wave here is now just going to spawn some kind of wave number and we will loop through this using a for statement but first we need to create this wave number and that's going to be a private integer and we are going to call this the either wave number or wave index I think we will start this at one and therefore I'm going to call this the actual wave number and that's going to be one there we go and then every time we spawn a wave we can take a wave number and increase it when we are done spawning and yeah that all makes sense and but this is actually yeah that's the current wave number everything is good good so on what we can do here now is that we can loop through this wave number so we can use a for statement and if you have hit tab here twice it's going to auto fill this order completed and for I is less than and then we use wave number that's going to be at the amount of enemies and then for each of these we can spawn and enemy and that's another method that we are going to be creating so let's do a void spawn enemy and this is going to be a very short method but it's good to split it up into different methods and then add stuff to those methods that we as we go and we are going to instantiate what are we going to instantiate well we are going to instantiate our enemy prefab and now we need to determine where to instantiate this and therefore I want to go up here I want to create a reference to some kind of spawn location public transform and we are going to call this spawn point and that's again something we'll reference in the inspector and then we can scroll down here again on this spawn enemy so we'll spawn the enemy prefab at spawn point dot position at spawn point dot rotation there we go and now we've spawned this enemy and we don't need to worry about D spawning it or anything that will be handled by our pathfinding system so that's fine we are actually done whoops we are actually done with this script for now so if we go back into unity now and find out game master we should have another empty slot and that's going to be for the spawn point so we can go ahead and create some kind of empty object or we could just use our start node so if we go in the game master find a start node and drag that in there that should actually do it so when now when we hit play we are still gun not going to have any visual representation of what's going on but we can see that we just spawned an enemy and after five seconds it should hopefully spawn two enemies which it did however each of these enemies you can see here that it's spawning lots of enemies but there are multiple enemies on top of each other and that's a problem so how do we space these out well we could do the same have some kind of float variable up here that we reset some kind of countdown separate from the other countdown or we could use what is called a KO routine and this is a super handy super awesome way of kind of creating these subroutines these ways of odd executing code separate from our main piece of code so we have this main countdown that spawns waves and then we have this separate wave spawner that has its own timing and runs in its own separate way and the way to do this is we simply turn this here this method here the spawn wave into a cover routine how do we do this well in a very fusing way so if we go up here we type in using system dark collections the namespace I removed earlier but I wanted to show you that that's why it's there that's primarily what you use it for in unity most of the time so using system dark collections and that allows us to write this weird I in numerator here and the I in numerator instead of void allows us now to pause this piece of code so we can actually wait X amount of seconds before continuing so what we can do here is we can spawn an enemy and then after we've spawned an enemy we can say yield return new wait for seconds and this is a lot of weird syntax and I'm not going to explain it in detail and then we could maybe wait point two seconds so we could maybe wait like half a second or you could turn this up this here into some kind of variable that we can adjust but for now I think I'll just hard-coded in here so that will indeed spawn status spawn wave loop through it spawn an enemy wait point five seconds spawn another enemy wait point five seconds and so on and then it will increase the wave number and I just realized that since we are waiting within this spawn wave it might be a good idea to increase the wave number up here and then have it start at zero instead and actually I'm going to rename this to wave index it's just cleaners just feels more nice okay so I'm the only thing that we need to realize here is whenever we create an eye enumerator we can't just call the method like this this is not the way that you start a corroding instead we simply write start co-routine and then we input the spawn wave in here and remember to open and close the parentheses to indicate that we are talking about a function so that was all we needed to do and now when we go back here we should hopefully see this piece of code working so when we hit play now we should see after two seconds it spawns one enemy then we wait another five seconds and it should spawn too with an interval of point five seconds and that's it's just going to keep doing this as the difficulty increases and you can see we actually already have a pretty nice scaling system for making more and more difficult enemies you will also realize that at some point we are going to start spawning enemies on top of each other because the it's going to take so long to spawn a wave and that there's not going to be enough time to finish spawning it before another wave starts and that's of course a problem with our system but that's not something we'll worry about yet I think we have more important stuff to implement and then later we can look at that kind of stuff so now when we are save this we have that done and let me just check the on time here yeah so I think we just have time to maybe our display this countdown on a timer on the UI so let's just do that really quickly because it makes this system feel a lot nicer so let's right-click over here and create off first UI element so let's go into UI and this is going to be a symbol text object let's go to the canvas now hit F to focus on that and that's giant but that it should be huge and then let's switch to 2d mode and switch our controls here over to the 2d controls let's find our text object and let's Center this on the top so if you hold down alt and click here it's going to snap it anchor it to the top and move it to the top and now we can hit F to focus in on this once more we can then move it down by holding shift to snap it to this axis right here and then we can maybe scale this up to make some more room and I'm using old here to scale in all the axes and we can move it down some more then we can Center it on the X and Center it on the Y and then we can maybe just input some kind of number here so I'm just going to input 20 I'm going to scale this up quite a bit something like I don't know maybe 80 that's very large let's try 70 and then we can also change the color of this change the color of this to probably a Y this one I'm going to want here but we should really mess around with is some more in the future I know this looks kind of weird but we are going to moving and be moving around the camera anyway so it's going to be looking a little weird but what we can do is maybe add some kind of shadow just to make it stand out from the white on our level so let's just write in shadow here and that just helps it pop just a bit and we can maybe increase the shadow here a tiny bit something like that and I'm also going to make this a bold number to make it stand out even more so now we actually have this number that looks fairly decent and yeah and we can of course edit that and again I insist that you spend more time on the UI than I do we'll probably also find some kind of cool font I don't like the Arial default fund but for now this is going to work just fine and this is going to be renamed to the on wave countdown timer and what we can then do is we can jump back into a visual studio here and we can add a reference to this and change it in the update method so that's what we're going to do so we're going to go in here and add a reference and let's actually add that right by the time between waves so this is going to be a public oops a public and then we want a reference to a text object so we are going to be using Unity engine ton UI that will allow us to reference eight texts and we're going to call this the wave countdown text like that and then in the update method as we go through here and we're going to do this after we change the countdown I believe it's really important I don't know sure let's do that and then that set our wave countdown text don't text to be equal to countdown and we want this to be to string to convert it to a string and to avoid that this has all kinds of float numbers all kinds of decimal places is what it's called we are going to actually do something kind of cool here so we're going to say math dot floor and that's going to simply cut off all of the decimal places and only leave the first one and then that is what we want to turn into a string so we're going to math dot Floyd and then do a two string right after that something like that so floor will simply cut off all of the decimal places it's not going to round or anything it's simply going to well always round down then to an integer value and it still is still in a float data type but it is a whole number so that's what that's going to do and then we probably want to since we are flowing it we probably want to add one so if we just if we just add one here I think that's actually let's try this out so let's hit play here and see what happens to this number oh of course we need a reference so in our game master we should drag in the wave countdown text there we go and now we should see it says one zero and you can see it skips the five there and that's kind of what I wanted to avoid but then again it kind of jumps quickly that's annoying so what we can do instead is we could try and round this so if we round this number off we can see now it says two one zero and five four three two one and that's fine so you can see jumps a bit there but that's okay I think we'll live with that if we want to change that what we can do is make the time between waves something like five point five and now that should actually be fixed here so there we go so now it won't jump in time so that's perfect and yeah that's a working countdown timers so that's great that's basically all I wanted to show you for this video I hope you enjoyed it I can't wait for the next one I think this series is really fun to work on so without further adieu thanks for watching and I'll see you in the next video so I'm going to start mentioning patreon supporters the following month after they donate so that there won't be any difference from donating at the beginning of the month to the end of the month this way everyone will get a full month of May and yeah everyone will be happy so just wait for the next month and you will see your name here so thanks for watching and I'll see you soon
Info
Channel: Brackeys
Views: 219,635
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, material, materials, beginner, easy, function, and, settings, how, to, howto, learn, course, series, tips, tricks, tutorials, workflow, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, wave, spawner, spawn, enemy, enemies, level, levels, create, tower defense, defence, minions, minion
Id: n2DXF1ifUbU
Channel Id: undefined
Length: 20min 11sec (1211 seconds)
Published: Sun Jul 17 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.