Animated Tower Attacks - Tower Defense Tutorial #7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my fellow gnomes how are you all doing i'm known code and today on episode 7 of our tower defense series we're going to be working a little bit more on our towers funnily enough now previously we've had our placement system working and we also already created this tower with this tower testing script in it so we're going to need to integrate these two scripts together so we can actually place these towers and have them attack and also we need to get some animations working because they're a bit still and stationary at the moment so the first thing i want to do is work on these animations so i'll hit stop and i'll show you what i've done so if i drag this guy well i'm just going to create a copy and i'll drag him into the workspace because this guy's all ready to go but i'll show you what i've done if you're already familiar with the whole rigging process then you might want to skip this part but we'll talk you through it all anyway so you see i've got this character and if you want to add a weapon to your model uh then i've just gone to the the toolbox and i searched for a slingshot and then i got i got this one right here you can add it in put it into the workspace and there you go now when because it's a tool it's got well first of all it's inside a tool object and it's got all of these scripts which we don't really want so i just went ahead and deleted all of the scripts and then inside the handle there's a sound some other things you don't want and we're going to rename it from handle just to slingshot and we don't need to be inside this tool object anymore so you can click drag it just into the workspace delete the tool and then you want to rig it to your model now what i'm going to be using is something called rig edit so if i click the model and i go to my plugins and select edit rig you'll see then i get this skeleton up here of the character and you can see it's all rigged all these joints are connected i've got this sort of little skeleton thing going on and what we need to do is we need to connect the existing rig to this slingshot weapon that we've just created so let me just delete the the one i've already added in we'll delete that and then what we're going to do is we sort of rotate it round it way to where we want it so we'd obviously we'd move it along here somewhere to roughly be in his hand okay something something like that will kind of will do for this demonstration and then you need to drag it into the model so it's not just in workspace it's inside the model and now everything moves around together and then when you're editing the rig all you need to do is select the right hand in this case hold down the ctrl key and then select our weapon which is the slingshot so we've got them both selected and then we're going to click to create a joint and once i do that you see this new line appears which shows all connected in together and now the real test is to open up the animation editor so if you click the animation enter you click your your model then you'll see wait we suddenly had an animation appear this is one that's just loaded in but if you get into the animation editor you can see everything's working so this is one i made earlier this is a very basic uh attack animation right it's not very exciting but it it kind of does the job okay it's not it's not it's not the best um but if you want to animate uh it's pretty simple i'll just delete all these keyframes you just select a part and then you can rotate it right we could rotate this upwards and then you sort of click further along the timeline and then you could rotate it back down so then if i headed to the beginning and i click this little play button then he's going to move his arm up and down that's pretty fast but you can see and so you just build up these little markers along the timeline until you've got your animation now we're going to need two animations we'll need an idle animation just for when he's stood around not doing anything and we're also gonna need an attack animation for when well when he's attacking the mobs so for idle you wanna make sure the loop is enabled and we'll set the animation priority to idle and then all you need to do is click publish to roblox we'll just call this idle slinger or something like that so just so you know what it is then click submit and it'll upload and then you'll get an id so click to copy it close this and now we've got the id so what we're going to do is we're going to add in this configuration folder add in a configuration folder and inside of that you're going to want the animations so create an animation object you'll name this one idle you paste in the id and then for your attack animation is pretty similar but this time make sure that looping is disabled and you'll want to create some obviously some different animation all right it's what a great animation that would be to have him do a little uh wave and then you would publish it to roblox again oh yeah actually you need to make sure the animation priority is set to action and then you can publish that to roblox as well get the id and paste it in for a do an attack one and so then you should have two just like that so i can go ahead and close all this now i'll delete that because i've already got mine from earlier over here but once you're done move him into the towers folder in replicated storage and now you have your rigged slinger so now all we need to do is actually animate him in the game so you might remember in starter player starter player scripts we created this mob animations folder this local script we have a look at it here so this local script it waits and detects whenever anything is added into the mobs folder in the workspace and then it connects it to this function and this just loads the animation and then plays it this is pretty much what we want to do for the towers so we could just uh copy and paste it and do something very similar but that's a lot of repetition to essentially do all the same stuff twice but just to change the animation names so we're not going to do that i'm going to delete that instead we're going to try and make this script um be a bit more general so we can reuse it let's just rename it from mob animations just to our animations so we know we're covering all bases and instead of having an animate mob function let's make this a bit more generic and break it into two different functions because if we look at the first half while everything is always going to be using this to load in an animation folder and object and so on it's only here where we actually specify that we want the walk that it's actually any different so how about we create a function and we just call this one set animation so local function set animation and we'll send to it the object like we did before but then this time the animation name that they want so we'll call this anim name just for short and now when the child is added into the mobs folder uh instead of just immediately calling this animate mob function we'll create a anonymous function like this object so it's all in line and then we'll be calling a play animation folder in a minute so we're going to have another folder another function sorry called function play animation and this is gonna have the exact same parameters as well and this play animation function is first gonna set the animation and then it will play it but we'll call the play one whenever anything is added in so just like that and then we'll specify the animation name which for mobs is what we just want them to walk straight away we haven't got an idle and then for towers well we'll copy and paste it it's pretty much the same thing but it'd be workspace dot the towers folder and the animation to play is our idle animation notice that i'm using capital w capital i because that's what i named my animations in the folder you've got to make sure you use the exact same case as it is case sensitive okay so let's start to dissect uh this function a little bit so let's have a look um the first part where it just grabs the humanoid and the animations folder well we definitely want to do that so i'm gonna select it control x and paste it into the set animation function and then the next thing it does it checks if there was a humanoid in animations folder and we definitely want to do that so ctrl x and paste that in and the next thing it does is it grabs something called the walk animation now we don't always want to grab the walk animation we want to grab whatever that animation name was so i'm going to ctrl x to move that and paste that into the top here but then instead of specifying the walk animation this is just going to be a generic uh animation object i'll name the variable and then instead of saying we'll want to look for walk we're going to look for whatever the anim name variable is equal to whatever that parameter that they sent in this case walk and then in the other case idle so once we've got that animation object check that we actually have an animator so that's going to be exactly the same as we did down here we'll control x and we'll check first of all if we have an animation object if animation object then we'll create the animator and then we're going to want to load that animation track so down here when we're getting the walk track well it's going to be the same thing but this time local animation track and this will be equal to the animator load the animation and then that generic animation object whatever it is and once we've done that then we can simply return okay so that's our set animation function we can now get rid of this old animate model and we've got nothing more we want to steal from it we've pretty much converted it now into this new one and now we just need to call the set animation from the play animation function so that's pretty simple we'll just uh we'll pretty much call it straight we'll copy it paste it in we'll just set everything up so we'll make sure we have an animator and we have the animations folder and everything um but because we're returning it we're sending that animation track variable back to us we need to capture it in a variable so local animation track equals what are being returned and then we can check if we actually have an animation track because remember if for some reason there isn't a folder or they don't have a humanoid then we're never going to get to this line here it's never going to be returned to us so if if it has been returned to us if we've got one then we'll use that and we'll tell it to play otherwise if for some reason something's gone wrong we'll just put a warning out we'll say warn animation that does not exist and if it doesn't then we obviously need to exit out of this something has gone wrong so we'll just return we'll head back so that should be our animations local script done and now it covers both the mobs and the towers and everything is neatly organized rather than having to repeat ourselves and we can now just easily reuse this for other animations so now let's just click play and we'll enable the output now we can start scripting and we will check if that's working when the tower gets added let's check if there's any error so i'll click for slinger and you'll see it's working straight away so even though he's just a placeholder he's already animated into place and i can click him and there we go he's animated he's doing his gentle bobbing up and down actually i'm not sure if i like him being animated while he's in the in the placeholder mode uh if you don't want that simple enough fix i think we're going to remove it so if we head it will stop the game and we'll head into our game controller script and if you remember we had this ad placeholder tower function where we just created that temporary one uh here it is we cloned it and then we added it into the towers folder well we probably don't need to put it straight into the towers fold as soon as it's just a template and then we remove it straight away so i'm actually just going to add it into the workspace we're not into the towers folder so now if i hit play it won't be animated because it's not detecting for it if i add it in now there we go we've got the generic placeholder template and then only once i place it is it then gonna start animating it so that's cool we've got an idle animation going on um but we haven't got any attack animation and we haven't got any attack abilities the only thing doing the attacking right now is this generic power we added so let's stop the game and now the next script we need to merge is this one inside the tower the tower testing script as we called it and we want to merge the contents of this script over into our mob so not on mob our tower module script so at the moment this is pretty bad bones it's just got the one function to spawn in a tower now if we remember in our mob module script we had the spawn function and then we immediately called inside of a co routine the move function as well and we're going to do a similar thing now with our tower script so we're going to create a new function at the top called function power dot attack and this we're going to call from the tower dot spawn so after we've set the collision group we'll say coroutine dot wrap and then we'll wrap it just like we did with the move so tower.attack is the name of the function we want to call and it is going to need some parameters that'll be the the tarot itself now we can't use the word tower as we're already using that so let's just say new tower because it's a new tower it's just been spawned in and what this function is going to do is it's going to find a target to attack and then it's going to deal some damage so essentially what we've already got going on in of this while loop here in our tower testing script so i'm actually going to copy the contents of this copy these few lines i don't need the while true loop anymore i'll show you why so i'm going to paste them into the attack function and after one second is up i can just recall the name of this function i'm gonna call the function from within the function and so now i'm creating a loop so do this code wait a second call the function again which is this and then it will just start again from the top so i'm creating a loop or a recursive function now of course this required defined nearest target function so i'm going to need that as well head to tower testing select this find nearest target function copy and paste it into our tower module script right at the top we're not going to use local anymore so delete that and while we're here we might change the max distance from 50 to like 20 or something we don't need it to be quite so high i see there's a red underline over here we've obviously moved a variable so let's just correct that workspace dot mobs and we'll get children uh we might as well move these commented lines though so we'll delete them and there we go exactly the same as before inside of our attack function um we're gonna want to make some more changes a little bit later on but for now i'm just gonna leave this as it is and check that out attacking actually works so let's have a look we're spawning in and then we're going to call this function oh we need to add the parameters remember the new tower so two more brackets and then we'll put the new tower variable so we'll send it along with it so let's um let's take that power we're using for testing we'll banish it into server storage and now we'll click play and see how we're looking so the zombies are already coming at us let's add in a sling a quick and oh straight away we've got an error let's see what's going on here so it's coming from line 52 hit stop what's happening on line 52 of our tower script line 52 oh where we call the attack function i think it's to do with our nearest target because it's actually line 15 uh is the it's the actual error and i see we're using this tower our verbal or we were doing in our old script but we can't use that anymore because that's the name of the entire module so instead we need to find it relative to the new tower because remember this is a generic module script that's going to be used by every single tower in the game rather than before where we just had the one tower and each tower was the parent of the script so remember we're using this new tower variable send it to the find nearest target function and then in here it will be insert tower.position it will be new tower dot humanoid root part dot position make sure it's parameter at the top of the function there we go so that should get rid of that error for us adding a slinger and there we go he's starting to deal some damage but he's facing the wrong way isn't he and he's not animated when he attacks so clearly that's the next thing we need to do we need to have some attack animations going on that's actually going to be fairly straightforward but we're going to need to communicate between our server script over here and our local script over here in our animations so we're going to go to a replicated storage go to the events and we're going to add in a brand new remote event and we'll call this animate power and then inside of our tower module script we will go up to the top and just like we have the spawn power event copy and paste that but this time it will be the animate power event and we'll be looking for animate power again make sure all the spellings are correct because otherwise it's not going to find it so now we've got the animate power event variable once we've done our attack well actually probably just before we've done the attack we're going to want to get that event and fire all clients or send it to every player in the game and the information we're going to need to send to them is the tower to animate so in that case it's this one new tower and the name of our animation as well which is going to be attack so now that we're firing this event we just need to detect when it's received on the client so we can actually copy this variable head into our animations local script and paste it at the very top we're also going to need some variables for the events folder and the replicator storage so we can copy and paste them over one for replicated storage paste that in oh we want it at the top because it's a service and then we're going to need a variable for the events as well so we'll copy that and paste it in so there we go we've got the the variables that we need but now we need to detect when this event is fired so let's head right down to the very bottom of our script below these two events and we'll say animate power event dot on client event and just like those two we'll connect that into a function and remember those two parameters that we gave ourselves was the tower that we wanted to animate and the name of the animation so we'll call that anim name for short and when we receive that event well now all we need to do we don't need to do any extra coding we just need to call that play animation function so we say play animation the first parameter is the the name of the object the model so that's that tower variable and then the name of the animation we want to play which is whatever the value of anime name is so that's all we have to do so now let's hit play and see if that attack animation is going to play for us we'll click spawn it's born one in and there we go we've got a basic attack animation going on now he's not actually pointing the right way and it seems to be looping so we might have a problem with our animation there so we're waiting one second and then firing off the animation um so what we really want to do is you want to make sure we're firing this animation remote event before we even do any damage and then afterwards we're going to wait for that cooldown which in this case is uh it's just one second maybe we'll change that later on um but after we've waited for one second then we will call it again so i think that should fix our our logic a little bit there let's try spoiling this guy in again we'll spawn him in we'll shoot one two three and there we go he stops that's fixed that bit of logic now this works he's doing a little animation um but he doesn't actually turn to the enemy so at this point well even if the enemies get round the side of him he's still facing forwards which is a little bit rubbish so what we want to do is we want to create a c frame that will face in the direction of the the enemy so to do this we'll hit stop and to help us with this we're going to add in something called a body gyro okay it's a it's a body mover object so we'll head into the tower spawn function down here and after we've created it we'll say local body gyro equals instance dot new and we'll create that body gyro object and then we're going to set some properties so the body gyro dot max torque and that will be that's a vector 3. so kind of like how much resistance you want there to be how much power or how much torque essentially so we want this to be as much as possible so i'm just going to put math.huge for each one uh so this is just essentially infinite but x y and z and then body gyro dot d that's the dampener and we'll set this to zero we don't want there to be any any pause we just want it to be immediate and then we'll set the body gyro dot c frame to be equal to the new tower dot it's humanoid root part dot sleep brain so is root part want it to be its current position and then finally we'll set the parent to the body gyro to be inside of the humerate part so now that we've set the default values for the body gyro we just need to change what its values are going to be when they actually do that attack so let's head back to our attack function up here and we're actually going to check first of all once we've checked there's a target let's just check that the target has a humanoid and also that the target dot humanoid dot health is greater than zero i want to only want to attack something that is alive and now if we know it's alive now we can start to accept that that gyro so it's going to be the new tower not its humanoid root part which is where we place the body gyro so new tower dot humanoid root part dot body gyro and we'll set the c frame of that equal to and we're going to create a variable of this so we'll say local target c frame will be equal to c frame dot and we're going to use the lookout function that takes two different values one a starting position and a second one where you want it to point towards so the first one is the the new tower dot its humanoid route part dot position and they want to look towards the target and it's the same for them that humanoid part dot position so we'll paste that in and so now we have our two position values and we've got that variable the target c frame which is going to be the c frame of the body gyro so once we've set that it's now going to turn towards hopefully our target so let's run and see if that's working for us we will spawn in a slinger over here and oh we have a problem i see down in our output what's going on on line 55 we have a problem oh i see i forgot to put dot new there we go vector three dot new when i was setting up that new vector we're spawning a sling over here spawning in so it's just facing forwards and hopefully when these guys come close there we go he's going to turn towards him and start firing fantastic and then he goes back to his idol so let's add a few more of these guys in we'll add a coupling along the way so hopefully now we can start to kill a few more zombies and we're going to be all pointing towards the direction that we're shooting looks a little bit more realistic doesn't it so here we are a successful uh defense of our land by these very hard working gnomes with their slings uh we're getting quite a bit yet to do but hopefully you've found that helpful we've got all our scripts linked together now all in one place and we can start venturing on with some new features now so thank you very much for watching if you found this video helpful if you enjoyed it then leave a like and why not even consider subscribing thank you very much and i'll see you in the next episode goodbye [Music]
Info
Channel: GnomeCode
Views: 213,043
Rating: undefined out of 5
Keywords: roblox, tutorial, gnomecode, gnome, roblox development, lua, programming, scripting, programmer, learn to script, game dev, roblox game dev, roblox game development, roblox studio, tower defence, roblox tower defence, roblox tower defense, make a game like tds, roblox tds, how to make, tower defenders, gnomecode tds, tower defense, making towers, placing towers, tower placement, animation, animating multiple npcs
Id: RRO75dO7whY
Channel Id: undefined
Length: 28min 44sec (1724 seconds)
Published: Fri Dec 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.