How to make a Tower Defense Game (E05 SHOOTING) - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello and welcome to video number five on creating a tower defense game in unity me today we are going to be continuing with our turret while specifically we are going to make it shoot some stuff so that's going to be super super fun so you can see there I'm here in unity and what I want to do is I want to select my turret and open up the charge script this is a pretty good place to add our shooting mechanics because we already have this target variable available to us so let's begin by adding a variable here and this is going to be our fire rate and I'm just going to default this to 1 meaning we will fire one bullet each second then next we need a R that's not right we need this to be a public float there we go next up we need a private variable a private float to be specific and this is going to be our fire countdown and it's basically going to start at zero so we can fire right away and then once we fired it's going to be set equal to one divided by a fire rate so if we can say shoot one time each second that means that our fire countdown will be set to 1 and then it will countdown every time we draw a frame until it reaches zero and then we can shoot again and so it will continue and this just make sure that we have our shooting on some kind of fixed update so inside of our update method here what we're going to do is we're simply going to say if our fire countdown is less than or equal to zero meaning it's time to shoot now well then we want to shoot and we're going to make this method in a second and then we're going to set our 5 countdown equal to 1 divided by our fire rate so you can see here very quickly that if our fire rate is 2 that means we want to fire two bullets each second that means that it should count down from point five okay so cool now we have that set up now we just need to make sure that a fire countdown actually counts down so we want to decrease this by a small amount each each frame and we do that by saying fire countdown - equals time - time so again this will make sure that every second fire countdown will repeat be reduced by one good we've done this before it's very similar to the logic we used in the wave spawning so now we can create our shoot variable and we just say void shoot here and we are going to have some different stuff in here we are going to be instantiated and Boulud and we are going to set it be setting a target for that bullet but for now let's just do a debug that lark statement called shoot just see if things are working so let's jump into unity here you see our variables pop up there is the fire rate and now we can hit play here and we should see that when it locks on to target it says shoot with an interval of 0.5 seconds so that's perfect now what I want to do is just be a bit more organized in the way we have variables set up here more specifically how they look in the inspector because once you get to have more than five or six variables it can be a bit confusing so what we do here is take a fire rate and fire countdown and move them up on to where our range is placed and then let's separate that from our target and what we can do is add a header and this header is going to say attributes so these are the things that we can that can vary from one turret to the to another these are things that maybe can be upgraded or changed and then we're going to make another header down here which is just going to be unity set up fields or something like that just to let people know that this is things that unity requires for us to do this it's not something that should be changed by the user and this should just organize things a bit better here so we have the attributes and we have the unity set up fields in general use header files a lot it it really makes for it very easily makes for a better interface when working with your own scripts great so now we can go ahead and actually add this shoot functionality but we have to have something to instantiate so let's go ahead and make a bullet so let's right-click go on the 3d option and selects fear and this is going to be whoa we don't want this on the chart and this is going to be a very very simple model just reset that focus in on it and move it up a bit and I'm going to make this a size of let's say point five or six point six point six point six something like that just so it's clear when viewed from a distance but so it doesn't like look silly when coming out of a bell that is probably a lot smaller but yeah this is going to be fine actually let's do yeah look here that this great and let's rename this to pull it and this is just going to be a standard bullet we can have other stuff like rockets or something like that later missiles and we can go ahead and remove the collider here we're not going to be using that and we can go and add a custom material so let's go onto our materials folder right-click create and then material and this is going to be out bullet material and we can drag that onto a bullet and adjust it primarily we want the smoothness down a bit more metallic miss and then we want it to be kinda dark almost entirely black cool so that looks a lot better and now we can take our bullet and turn it in to a prefab by simply dragging it in under our prefabs folder and while we added I think we should go ahead and make a scripts folder here as well so I'm just going to make a new folder called it scripts take all of our scripts move it into that folder okay so uh here we are so the next thing that we want to do well actually we can just go ahead and instantiate this now just to see if that part is working so let's delete the prefab here let's go under our turret script and that has of course moved so we not need to open it from the new location there we go and we need to set up a reference to that bullet so let's make a public game object and this is going to be a bullet prefab and we also need a public transform um which is going to be the point at which we want to spawn our bullets so this is going to be a fire point and this is easier you could also use some kind of vector three offs but if we just use the position of the turret it's going to spawn inside the turret and look super weird so instead we just use an empty game object that we reference as a fire point and that just stores a position and rotation and everything's awesome so yeah so that's great and then we can go down here to our shoot and we can instantiate our bullet prefab add fire point dot position oh that's not right fire point dot position and then our fire point dot rotation as well great so now we have spawned this bullet in and it will not move anywhere and it will not do anything but it should be in the scene so if we go ahead and reference this we need to reference the bullet prefab first and then we are going to need to make a fire point so let's focus in on on turret let's go onto a pot to rotate and create a nod EMT reset the transform call this one fire point and let's shift back into the isometric view here the orthographic view just make sure it's centered there I'll shift to the side view here and let's make it sure it's centered here as well and I'm just going to move it out just a bit from the barrel so that it doesn't overlap with our turret model and something like that looks pretty great so let's hit apply to that and that's also dragging the fire point into the slot there and then hit apply again so great say that and we should be ready to go so we should see that when this locks on to an enemy it creates a bullet and that's awesome but currently it's just floating floating in the air and that looks super weird so let's go ahead and change that so let's add a script to a bullet let that will take care of moving the bullet to where it wants to be and take care of hitting the enemy and stuff like that so it will both move the bullet and do some hit detection so in order to do that let's find a bullet prefab hit add component and call this one bullet and let's just go ahead and move this under the scripts folder right away and let's open it up and let's just reload that that's fine remove systems are collections and what do we want to do here well first off what this bullet needs is a target they need something to pursue and what we want to do is pass on the target that is found under our turd to a bullet so we want to transfer that target that information of what to shoot at to the bullet so it can chase it and what how we do this is we make a private variable in here a private transform and we call this one the target and then we made some kind of public function that we can access if we have a reference to the bullet script that will set the target so we are going to have a public void here and I'm just going to call this seek or chase or whatever and this is going to take in a transform and an argument which is underscored target just so we can differentiate between the two and we're simply going to set it equal to underscore target so the cool thing here is we can simply call the seek method and that will set up everything we need to be set up on the bullet this could also do other stuff such as instantiate some kind of um effect right when the bullet is spawned in or set speed on the bullet stuff like that pass on a damage amount to the bullet all that kind of stuff great so we have the seek variable and now what we want to do is we want to call this from a turret so we want to go into a turret here scroll down to our shoot and first off we need a reference to a bullet that is currently instantiated and we do that by making a temporary variable down here that we are going to call the bullet game object and set by setting that equal to the instantiated object and we of course need to cast this into a actual game object so that is called object casting and it's just a necessity it's just something you need to do excuse me when instantiate objects and wanting to and you want to store them so now we have a reference to a bullet then we can go ahead and grab the bullet script off that game object so let's do that let's store the bullet script here and let's just call this deep poet and let's set this equal to bullet geo dot getcomponent of type bullet so we'll go ahead and find the component on that object and then we can just make sure that this is not null that's always a good idea to do that so if bullet is not equal to null if that's actually a component on that object well then we want to say bullet dot and then we want to use our seek method and then we can pass in here the target that is currently here the target from up here what we're currently shooting at so that's basically all we need to do there then inside of a bullet what we want to make sure is that we have a target because if we don't have a target let's say we start shooting and the enemy reaches the end of the level and therefore we lose HP and the enemy dies then this bullet is going to lose its target and it it's not going to want to find a new one in that case I think the best thing to do is either just let the bullet drop to the ground or simply just destroyed and in our case I think we're going to destroy it so inside of our update method we're going to say if target should be equal to null and this might happen then we want to destroy our game object this game object and then we want to return and the reason why we need to return here is that again destroy sometime sometimes takes a bit before it happens or to process and therefore we don't want it to continue down in our code and while it's doing that so now we are much more safe in our code so we should see this working and we could go in and preview it quickly but I think we are going to have to move on here so let's just move on and basically what we want to do now is we want to find the direction that our bullet needs to point in in order to look directly at its target so we've done some of this before it's a vector3 direction and it's going to be equal to our target that position our end location - our current position so our start location and what we then want to do is we want to go ahead and find out how much we are going to move this particular frame and we do that by first making a speed variable and I am just going to set this to something like 70 and then down here we are going to make a float and this is going to be the time all right arm this is going to be the distance this frame the total amount of the the actual distance that we are going to move this particular frame and that's going to be equal to speed multiplied with time Delta time and then what we can do is we can check if our direction dot magnitude if the length of our direction vector is less than or equal to the distance that we're going to move this frame well then we've hit something so why do we do this well the length of our direction vector this here this is our direction vector and this is the length of it and that is basically the current distance to our target so the distance from a bullet to a target is equal to the direction magnitude and if that is less than the distance that we are going to move this frame that means that we've basically already hit the object that means that if we actually do the movement we're going to overshoot and we don't want that we want our object to hit before we move past our target so inside of this if statement we are going to have logic for whenever we hit our target so let's go ahead and make a function called hit target and other than that I don't think we need anything in there I think we just need to call that and then yeah of course we could go ahead and and just return here and then down here that means we haven't yet hit our target and so we actually want to move and we do that using transform dot translate and the amount we want to move here our actual translation is going to be our direction that normalized multiplied with our distance this frame again we normalize it to make sure that however close we are to the object doesn't have an effect on how fast we're moving we want to move at a constant speed and this is going to be our constant speed and we want to move relative to the world space not the local space and that's important or you're going to get these weird circular almost orbital behaviors yeah it's not something you desire trust me cool and I think I think that was pretty much it then the last thing that we need to do in here is add some kind of logic for it whenever we hit something but let's go ahead and do that in just a second so let's just make the hit target down here and let's debug that lock we hit something just to see if it's currently working so let's go ahead and hit play here and let's find out if this is working so let's find out turret all of this is set up we find out bullet of that is set up let's do this so let's hit on play and wait for a enemy to move inside of our range here and you can see that our bullet jumps to that location you can see it keeps following but the shooting itself looks really really nice so and I spelled something wrong but yeah it's working and it's registering that we hit something so what we can do now is we can go ahead and just destroy our bullet whenever we hit something so that just right here just destroy our bullet which is game object there we go and then we can answer instantiate some hit particles so let's go ahead and just very quickly create some cool particles that will spawn and in order to do this that's right-click create a particle system reset the transform on this again reset the transform let's move this over here just so we can see kind of what it looks like in context yeah and then let's set the duration to 2 1 let's do 1 let's keep looping on emission is going to be with the rate of 0 let's do a burst instead here so comes out in bursts like that and it's going to be true between 5 and 10 particles the our renderer is going to be kind of different we're going to make a new material first of all and this is going to be our bullet impact material and just drag it on there there and then drag it into the particle system here there we go and we're going to change this from billboard to mesh and it's going to be a cube mesh so that's already working so that's fine all these settings should just be fine we definitely want some shadows in there they look nice we can go ahead and take a bullet impact and make this a lot darker make it more metallic not as smooth something like that kind of the same style as our bullet itself so it almost looks like our polished bullet shatters and then becomes this but I do want to be able to control it independently so this is fine and we can go back to our particle system we can go in here and set our start speed to a random between two constant and that's going to be between 0 and 5 something like that looks nice then we can take our shape change it into a sphere cool then we can add a gravity modifier which is going to be one and then we can go in here and change the size to maybe point two and we can maybe randomize the size just a bit so between point one and point two five there we go so we have some random size that's maybe a bit two months 0.15 that we go that looks really nice then we're going to add some bounciness to these we are going to actually make them collide with our environment and we do that by going down here enabling collision changing whoops changing our collision to world 3d we want to damin it by something like point eight and you can see currently they're going right through here and the reason why is we need to go into our environments like all of our ground planes and add a box Collider there we go and we deleted those in an earlier episode but I didn't think of we wouldn't think that we would need them for for this but we we would and we can maybe damming them just a bit less something like point six that's a bit more cartoonish I think that's pretty fun we can change the lifetime here to one actually let's do a lifetime of two and they will sink into the ground here that's not much we can do about that I think it's fine let's change the collision quality to medium and yeah I think we're getting there the shape we can just make that a bit smaller something like point two because we want them to just explode from a single point and yeah I think that's pretty much it and then we can maybe make them shrink when they are over with their lifetime so let's go and add a size of a lifetime add a key to this and just add a small fade-out in size at the very end there we go so we can go ahead and rename this to our bullet impact effect and we can make it into a prefab and we can delete it from our scene then we can go into visual studio add a reference to that prefab up here public and this is going to be a game object and this is going to be our impact effect and down here we then want to instantiate our impact effect at our current position and using our current rotation as well as well and that should be it for that we could go ahead and clean this up in the hierarchy let's just do that so game object store a temporary reference and this is going to be our effect instance and we're going to set that equal to that and cast it into a game object and then we can destroy that effect instance after let's say two seconds this way it won't clutter up the hierarchy and now let's try and hit play on this and see if it works whoops I need the thing we need to reference the impact effect of course on a bullet dragging that in there so now let's hit play and hopefully this will work so see and it's working beautifully so I think that's a really nice effect I really like what that does you will notice it just glitches out and that's because I forgot to turn off looping so just very quickly loops and then it gets destroyed and that should fix it right there and what we can also do is we can go ahead and just destroy our enemy for now later we will have just our destroyed target game object later we will have some kind of way of damaging our enemies so they will need some kind of stats or something but for now we can just destroy them so all the turrets will be super powerful so you can see that looks super nice and will definitely do more of this stuff later but for now that was it so I hope you enjoyed this video thanks for watching and I'll see you in the next one 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 mentions and yeah everyone will be happy so just wait for it on the next month and you will see your name here so thanks for watching and I'll see you soon
Info
Channel: Brackeys
Views: 224,151
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, asset, assets, model, texture, models, textures, material, materials, beginner, easy, how, to, howto, learn, course, series, tips, tricks, tutorials, workflow, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, turret, turrets, tower, towers, shoot, shooting, bullet, bullets, impact, hit, effect, particle, system, collision
Id: oqidgRQAMB8
Channel Id: undefined
Length: 24min 24sec (1464 seconds)
Published: Sun Jul 31 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.