How to make a Tower Defense Game (E16 SLOWING) - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is brought to you by our own Metta thank you so much for donating if you want to support breakage yourself you can go to patreon.com/scishow Cheers thanks for tuning in at brac ease hello we want to look into this video in creating a tower defense game in unity today we'll be finishing off our laser beam err but actually making a damage and slow our opponents in the process we'll also clean up our code a bit separating out some of the enemy movement from some of the stats and dying in that kind of stuff so yeah a much needed video and it's just dig right into it so as you can see I'm community and what I want to begin with is just remove this laser beam er from our scene and then we'll only spawn it through our shop because there's really no reason for it to sit there cool and then what we'll do is we'll find the prefab down here and we'll have a look at how it's composed so we have the turret here and this is where we handle all of our shooting so let's double click that to open it up and basically what we want to do is just add another variable on to the part that is exclusive to our laser here so under the hitter use laser this is where we'll go ahead and make a new float and we're going to make it a public float and actually let's make it an integer I don't think we need that much control and let's make this our damage over time and whatever this is set to is the amount of damage that the enemy will receive each second it's hit by our lasers so by default let's just set it to something like I said it's something like 30 and that means that each second our laser will be damaged by 30 hit points or our laser will damage our enemy by 30 hit points great and then we can go down here and inside of our update loop you can see we call a laser each frame and then we can go down here to where we do all that stuff lasering and we could either add it to the top or to the bottom this is all of the graphic stuff and then down here we could damage actually I think I'm going to do this before all of the graphics stuff because it is the most important port important part is actually damaging the enemy so what we could do here is just simply go target and our target has a compare in code enemies so we could go get component of type enemy and then we can do dot take damage and we could input some amount in our case we want to input our damage over time and then multiply that by time.deltatime in order to cancel out any differences that might be are between one computer's frame rate and another computer's frame rate and this will also make sure that this is per second and not per frame so cool so this should actually work however I take damage method currently is only used to receiving an integer and because we are damaging the enemy a tiny bit each frame we can't really deal in integers we might damage him by point zero five one frame or whatever depending on the damage over time and depending on the frame rate so let's go in and just modify our take damage and this is super easy we just need to find our enemy script double click it and instead of having a public integer health here we'll make that into a float gives us more control and then down here we can make this into a float as well so we'll take the amount as a float and that's all we need to do and we should actually see this working out so if we go in here hit play and I'll scroll over and spawn one of these guys in you can see that if we wait I'm a pasta game here and select our enemy you can see he's currently on 14 health here and if we just keep going you can see quickly decreases and then he dies so that is indeed already working however there are a few issues with this the first one is that um currently we are getting the component of our target every single frame and that's a super taxing operation especially if you have a really complex object so what we're going to do instead is cash this into a variable much like we have a variable up here containing our target we'll create another one one which is going to be of type enemy containing our target enemy oil just call it enemy the enemy component of our target and we're not going to default that to anything but we are going to set this instead inside of this if statement here so we update our target search for the nearest enemy we get the nearest enemy and we set target equal to the nearest enemy dot transform and what we then do is simply say that we want our target enemy to be equal to the nearest enemy dot getcomponent of type enemy so basically the same thing that we did down here but we're just making sure that we only do this when we actually update our target and that is a lot less than every frame cool so um then we can just replace this piece of code here with our target enemy and things should still work just fine so the next thing that we want to do is add some slowing to our enemy and before we actually do that I want to separate out some of the code because if we currently have a look at our enemy script I find it's a bit more complicated than it necessarily need to be because we have speed variable we have a health variable then we have some value which is the enemy's worth and I am actually going to go in here and replace that with words and in order to rename something and substitute the name all other places as well you can just to control RR in visual studio and you could also go right click and I believe it's called rename here in some programs it's called refractor or you could of course just write a new thing and then place it all the other places that it appears so now it's called our worth there and um then we have a death effect and that these things all make sense this is stuff central to the enemy it's things that describe the enemy speed health birth and how it looks when it dies and then we get to the target and the wave point index and this is something that's very specific to movement so what I want to do is take stuff like this and move it into a separate script just to kind of make our whole code easily easier to read and more modular so we'll go inside and find our enemy prefab here let's create a new component called well enemy movement and let's make this a new script and it create an ADD and let's double click that to open it up in Visual Studio as well and reload all there and let's just remove all of the default stuff in here and let's paste some stuff from here so let's select the start method and these two variables because that's all movement code let's hit control X to cut it out and then that's paste it inside of our enemy movement and we have more to go then the take damage method again specific to describing the enemy and changing the health around the same thing with our dying and then we have our update and all everything we do inside of the update is movement related we get a direction we translated we also find the distance to the next point and stuff like that and getting the next Waypoint and ending the path all of this is specific to movement so let's take all of this code as well and simply cut that out control the X or command X if you're on the Mac and it's then copy that in here instead and this should work right off the bat except for one thing and that is our speed variable because that speed right here that we're using to determine well how far we want to move each frame um that actually sits still inside of our enemy and we could of course go ahead and move our speed inside of our enemy movement but then we wouldn't have a central place for all of this stuff that we want to change about our enemy and I think it's a good player good to have a place you could call it enemy stats or just enemy um with all of the characteristics of your enemy and then have other components connect to that enemy in order to get information about speed and health and stuff like that so let's just sync up our enemy movement to the enemy our script so in order to do this we'll first required it as a component so we'll say our enemy movement won't work on this we have a component of type enemy that sits on the object that will just ensure that and then we'll also make a reference to this a private variable called enemy and we'll just call this one enemy with a non-capital either and in the start method will set enemy equal to get component of type enemy so now we find the enemy on our objects and we call it enemy and then we can simply instead of going speed we can go enemy speed and it's it's actually that easy to do awesome so I believe that's all we needed to do here so on the next thing that we could do and let's just test out if this is working here so you can see we have enemy movement but it has no attributes we only have all of our attributes in here and they should link up nicely so that when we now hit play and wait for an enemy to spawn it works just as before and what this allows us to do now is if we go inside of our turret we have our target enemy and we can go in here and add a new function say we want to go target enemy dot slow and then we want to give it some slow factors say 0.5 meaning that we will cut the speed in half we could also do 0.3 meaning that it should only have 30% speed or you could turn it around and say that at 30% slow which means it will have 70 points percent speed you can do whatever you want there let's actually do some kind of percentage so the number that we input will be the amount that we want to slow so in order to do this let's make this into a variable first of all so let's make a public float here and this is going to be our slow percent or percentage and we're going to set that equal to something like maybe 0.5 is going to be just fine for now it might be quite a heavy slow but we're not going to have it so that turrets if you have multiple turrets on one target that it will stack we're just going to set this as a limit so even though you have two turrets on the same target it's still just going to be half the normal speed and so let's do that and let's go down here and let's just call the function right away and then we'll go in and create that so we'll have some method called slow and we'll put inside of that our slow percentage or actually I'm going to do slow amount and that is going to be a value between zero and one zero between meaning that we want slow at all and one meaning that we will on completely remove this speed so we'll just make the enemy stop so yeah and then we'll add this function in here and we can use pretty much on this way of writing the method as inspiration so this is also going to be public because we will call it outside the cut class it's going to be a void because we don't want to return anything it's going to be called slow and it's going to take in some kind of amount or percentage or whatever you want to call it I'm just going to call this one percentage and um percentage is a bit misleading because it's a valuable to zero and one and not in hundreds but that's alright we don't need to get too technical with this and then we can say here we simply want to set speed equal to and now comes the problem because what do we really want to write here do we want to say that we want to multiply speed by the one - the percentage here so this would mean that our new speed would be equal to our old speed and then multiplied with and we would have to do this multiplied with one - our percentage so in case we want to slow by 30% we would write point three and that means that we want our speed to be 70 percent of the original speed so one minus 0.3 is equal to 0.7 and speed times 0.7 would be 70 percent of our speed so that this makes sense however if we do this every frame our speed will keep on the our speed will keep decreasing because we'll do that every frame will reduce by thirty percent every single frame and then eventually we are going to gradually go on towards zero it's never going to reach zero but it's going to get really close and slow up really quickly so instead we want this idea of a start speed some base speed that we work off and use as a base line so on this speed here we're actually going to keep that and then we're also going to have a public float which is going to be our start speed and we're going to set this equal to ten by default as well and we can now remove this here because we don't want a speed be equal to anything we want that to start off at the start speed and then be modified as we go and then we can go in here and create a void start and inside of this start method is where we want to set speed equal to start speed so when we start the game or speed variable becomes side speed and then down here we want to set speed equal to start speed and because start speed is never modified we can do this every frame no problem so on there we go so that should actually modify our speed however a few problems again will arise so if we have a look inside of unity here you can see now that our enemy has a start speed but this speed still shows up and it's annoying because we don't want to be able to modify this it's not going to have any effect you can see as soon as I start on the game here and then find an enemy that is actually spawned in here we go so we'll select this enemy here you can see that the speed is just set to the same as the start speed it doesn't matter and so what we'll do about this is um we'll simply remove this start of this speed here from the inspector we still want it to be public because we want it to be accessible from within our enemy movement but we don't want it to show up so we use a tag here called hide in inspector and writing this right before any variable simply means that it will not show up so it says here makes a variable not show up in the inspector but be serialized so there we go cool so that is all the functionality we are going to need here and you will notice that if we go in here now and create one of these and make him shoot you can see that it actually slows the opponent however um however if we were to put in a bit further away here maybe here it slows and you will see that when he gets out of range he's still slowed so we need some way of cancelling this back out and the way that we are going to do this is we're going to utilize on how unity calls update loops ghost currently our turret here is calling the laser function inside of the update loop and the laser function then slows the target enemy and we go in here and that changes our speed then in our enemy movement the update loop is called and that means that we will use that speed to actually move what we could then do is just in here change the speed variable back so we could say enemy dot speed equals enemy enemy dot start speed there we go so we've now reset our speed and that's going to be fine because it means that if our turret is no longer slowing us we've just reset our variable but if it's still slowing us well then it's just going to call this again and slow it again it's going to go in and change the speed and then it's going to use the speed to move and then reset it again so we have this loop here where one update changes the speed one update uses it and then resets it and unless the other one is called again fine it's just reset and if it does well then we just go in a cycle and that's because the way that unity handles these update functions is that it calls all of the awake functions then it calls all of this start functions and there's a whole hierarchy to this and all of the update functions are called on not at the same time but just after one another and you might say well what happens then if our enemy movement is called before our enemy thing here but well that isn't actually necessarily a problem because that just means that we will call this code an enemy speed will be the normal speed and then it's going to reset it and then the other thing here is going to be cold so the next time that this is cold this speed will have changed so that will just offset by a single frame which is definitely not noticeable but if we wanted to get rid of that I will show you how you can do that in just a second but for now let me just demonstrate that this should in fact be working so if we now go in here and place a guy and that is not going to work because he is going to kill the enemy before we can do anything about it so that just place one here you can see it slows the enemy and then he speeds but right up again so it already works just the way we want it to and this is going to work with all of the different towards no many no matter how many torch we have so we could go ahead here and place multiple of these in there and let's just do that by allowing ourselves to start with a bit more money so let's just start with a lot of money here hit play and we can go in here and we can put some of these guys down and you can see right now they will damage the enemies a lot but as and the amount of enemies spawn increases you will see how they easily just switch between targets so you can see that's the first one then it's those the second one and and that this should just be working so what we can then do is easing a is maybe a better practice than just having the movement be offset by a single frame would be to go in and ensure in some way that on the enemy script here is called before the enemy movement and a way we can do this and of course unity supports this is we go under edit then project settings and go under what is called the script execution order and normally I tell beginners not to mess around with this because normally you can get around this by writing different code and in this case you could do that as well you could have some kind of for example a list of all of the different debuffs that you want to put on your chart and then or on your enemy and turrets can then register a debuff and then unregister it when it stops shooting and then on the enemy will just be responsible but for going through all of the deep buffs and adding them on each frame and that's maybe a more solid way to do it but I think it's definitely overkill for what we're doing because we only want one type of debuff but on what you can do here is you can go ahead and add a new script so we're going to add our enemy movement and icloud still demands of me to purchase something and you can see here it add scripts to the custom order and drag them to reorder so in our case here the default time is when all of the different scripts will be are executed and we can go ahead and put a small delay on our enemy movement and that means that all of our scripts will execute along with the enemy script that changes and our turret script that changes our speed and then when they're done exactly 100 milliseconds after we are going to execute the enemy movement so this just allows us to put an order if we wanted this to execute before we could simply drag it up there and it's now executed negative a hundred on so that means 100 milliseconds before the default time and we could actually go in here and be explicit about it as well so we could go in here and put our enemy before there and do that but I think this is going to work just fine so if you hit a plan that you won't see any real noticeable difference but the scripts are now executing in the proper manner here so you can just see that working they're awesome so I'm actually really happy with what we've done with the lasers here and they are just so much fun to play around with and this was pretty much all I wanted to show for this video if you have suggestions as to what you want to see next definitely leave them in a comment I want to do some something about ending the game we also have to look at game ballots at some point and I also kind of want to expand on the different types of enemies that we're able to spawn and how they increase in our difficulty as the game goes on so thanks for watching that was all for this video and I will see you in the next one thank you so much to all of the awesome people who donated in September and especially thanks to our own Metta Robert Rhodes James Calhoun Kim Rose cuccos Bao and Damned Wong and vixen pea thank you so much for donating you guys are awesome if you want to support brackets yourself you could go to patreon.com/scishow
Info
Channel: Brackeys
Views: 54,727
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, beginner, easy, how, to, learn, learning, course, series, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, turret, tower, defense, laser, beamer, slow, slowing, debuff, damage, over, time, dot
Id: UOYBr9vFqYI
Channel Id: undefined
Length: 21min 45sec (1305 seconds)
Published: Wed Oct 19 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.