Godot Engine - Projectile Attacks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning afternoon or evening wherever and whenever you are my name is Benjamin and welcome to a quick Godot tutorial video this video is going to be a collaboration with GD quest I'm going to put a link to his channel and the video that he's doing I'm going to be doing in this video I'm going to be doing projectile attacks and in his video he's going to be doing melee attacks so let me show you by the end of this video you'll have a simple project that looks about like this and you can move your dragon up and down and you can press space to fire a projectile from your dragon and hit the enemy dragons so nothing too fancy but we're going to focus mainly on the projectile so let's get started I've created a new project I guess I should also mention that I'm currently using Godot 1 or 2 dot one dot for stable so this is the stable version of Godot I think Gd Quest is going to be using 3.0 for his which is an unstable branch but it's it's a gonna be stable here pretty soon and so I think he's just kind of planning ahead learning the new engine so there will be a few slight differences because of that but the principles should apply no matter which version you're using there might be some slight implementation differences so I've got a new project setup here and the first thing we need to do is create our fireball and so we can come into our project here and click on 2d and let's actually do that let's actually do the project setup so we'll do that very first then we'll create the fireball so come into scene and then project settings will go into display I'm gonna set the width to 320 and the height to 180 so that sets the width and height of our game room and I guess the view in the room and now we need to set the window that it's displayed through and we'll do that through the test width and height you can just check both of these and our test width will be 1280 and our test height will be 720 close now this first scene that we're in you can see we can now zoom in on this just a little bit this first scene that we're in is going to be our I guess our world scene so I'm going to create a new base node just do the very top one in here node and we'll name this world now we can save this scene I'm just going to hit ctrl s in order to save it then this is a directory we can save it in and it wants to save it as world dot T sen and that's perfect so we'll hit save and once you've done that you can try and run the game and you can say needs we need a main scene defined so should we select one let's select one and we'll select our world scene and you can see then it runs the game just fine there's nothing in the game right now but it does run it okay and it doesn't know for sure that this is a 2d scene yet so it's kind of freaking out a little bit but we'll let it know here soon so let's create a new scene so create a new scene now this will be our fireball scene and this scene will be made up of a few notes so our fireball needs to be able to detect collisions okay and it doesn't need it doesn't actually need to have anything super fancy with it with its collisions so we can use an area 2d for our collisions an area 2d is basically just like an area that can detect whether or not it's colliding with another area or with a body but it doesn't have any physics associated with it it won't need to bounce it won't need gravity really it's not going to need anything in physics so we're gonna start with our area 2d so let's come in here and I'm just gonna search it area to eat there it is and we can just rename this fireball now our area does need a defined collision shape because this area doesn't have a defined collision shape so it won't know whether or not it is colliding with something so if we add a new node to this we can name the or we can search for body or let's see collision collision shape 2d is what we'll want this very bottom one so I'm going to double click on that and we need to give this collision shape node an actual shape so right here in the right hand corner here you can see a shape it's null we're just gonna click on that and do new circle shape 2d and it creates a circle shape for us and we're going to if you press this little teeny arrow over here going to the right we can get the properties of this circle shape and we need to define its radius it's currently a radius of 10 we'll set it to a radius of 8 and I just found that that's a better radius and we can press ctrl s to save this scene and we'll save it as fireball dot t SC n okay now I have some resources for this project so I'm going to copy those into the project folder I'm going to come to my window my Explorer here come to desktop and i'll click on fireball tutorial resources i've got the dragon the fire particle and the red dragon I'll just do ctrl C to copy these come to my documents Godot tutorials fireball and just paste these inside of here ok once I've done that they should appear over here inside of my file system I've got the dragon the fire particle and the red dragon so click on your area to D up here on the fireball we're going to add a particle particles to D here so we're going to add a new node and just search particle if I can type it there we go well let's attach a particle to D and you can see that automatically creates a particle and it's creating some particles we're gonna change some information here about the particle the very first thing we want to do is set up its and this is all the information about our particles right over here we want to set its texture very first because we don't want to just use a white pixel so we'll click on the fire ball or the fire particle right here click and drag it and we can just drop it right into our texture boom right there and you can see we've got the fireball texture now if we press this little over arrow on the texture we can get the properties of the texture we want to turn off filtering and we want to turn off MIT maps so we don't need either of those flags now you can see it's pixelated and sometimes when you first import it the turning off filtering doesn't quite work I'm not 100% sure why but if you reload the scene and well it will work so it doesn't really matter because it will show it will show working in game so I'm just gonna leave it for now now that we've set the correct texture we want to set the correct blend mode so if you scroll all the way down we're gonna have a blend mode under canvas item and we're going to set this to additive and it's starting to look a little bit more like fire okay we're gonna change some more properties the first thing we're going to change is where the fireball is created from and this can be done inside of your half x tents so if we spread these out like say we put this 2 8 and 8 it would create them kind of in a square of 8 around here so we're just gonna do two and two for now we don't want to spread them out too much we're gonna set we're gonna leave our amount at 32 but we're gonna set our lifetime at point two so that is basically how long the particle lasts its lifetime okay scroll down and we're gonna start changing some of the params right here so for our direction we'll do 180 and that should create them create the fireballs so that they go upwards for our spread we're also going to do 180 that should make them kind of go in random directions and it's already starting to look a lot more like a fireball than it was before for our linear linear velocity we'll do 25 this affects basically how fast they move our gravity direction will be 180 that will make the gravity pull them upwards and our gravity strength will be 490 and that will just make it pull upwards quite a bit stronger and this is already looking like a pretty good fireball we haven't even done some of the other little finer details so our initial angle we can set this to 180 and our initial size we'll leave that at 1 but we'll set our final size to 0.25 this will make the particle this will make the fire shrink so you can see as it gets closer to the end of its lifespan it will shrink okay so we've done all the params now we can come into randomness and randomness basically it's a scale each of these numbers of the scale from 0 to 1 and it affects it affects how random that thing is so for example if we set our initial angle to 1 it will mean our the randomness our of our initial angle we'll be a hundred percent random basically zero to 360 and you can see now we're getting that's a much better fire particle in my opinion because it's completely it the angle of the particle is random and we're looking really good for a particle now there's not really a lot that we need to change from this point on we've got most this stuff how we want it now these values they're kind of hard to understand at first but if you just keep playing around with them and you can see what it does right away inside the editor so if you just keep playing around with them you can kind of get a good idea of how they work now I want to show you something that's not quite working yet let's save our scene just do ctrl s to save it if you grab the fireball oops that's moving our particle so if we want to click on the parent and not accidentally grab the children you can actually click this little icon right here which make sure the children can't be selected and then we can grab the parent but you can see if we move it the fire stays relative like the particles stay basically relative to the parent right here and that's not what we want it to be like so let's let's set our fireball click on the fireball and come down to let's see where's the position set both of these to zero so we'll put it back at the zero zero right on the axis now if we come into our particles there's a property inside of here that is whether or not they have local space and we don't want local space so we'll turn off local space now what happens is when we move the particle or move the the fireball you can see the particles aren't local to the fireball earth to the space anymore and this is what we want it to look like when we're moving and that that looks really cool so our fireball is pretty much done as far as the looks go and the set up we still need to make sure you set the position on your area to be back to zero and zero we still need to actually program it though so if you click on the fireball right here on our area to denote and create a new script it will just create a script called fireball Gd that should be fine for now and I like to make sure I name my scripts up here at the top with a comment so that I know what script I'm in if this windows not if I have this window hidden for any reason so extends area 2d just means it inherits from the area 2d which is what we want and we don't need these comments right here so the ready call um I should mention here that since most of most of the people watching this video probably are my existing users or existing subscribers and they all use GameMaker I'm gonna explain some things and kind of a game maker ish with a game maker perspective because that's where I'm coming from but the ready function is kind of like the creative event in game maker basically that means that once this scene or object in game maker is is created it will run this bit of code so all we're gonna do in here is set process equal to true and what that does is it basically says run the process event or run the process function and the process function is the equivalent of the step event in game maker in other words it runs every single frame of the game so we'll say func underscore process and you can see it comes with the Delta variable which is how long it took to process the previous frame so we're going to create some variables will create one var speed underscore X we'll set this equal to one var speed underscore Y we'll set this equal to zero now we're going to create a vector for our motion so a vector is basically just a combination of the X speed and the Y speed inside of a single variable so we'll save our motion equals vector 2 and that's a very simplified explanation of a vector but in this context it makes sense speed y so we're basically taking the Y speed and the or the X speed and the Y speed and we're converting them into a vector right here then we want to have we want to give our fireball a speed 2 so we're going to create a constant up here Const which is just like a variable but it never changes fireball speed we'll set this equal to 200 and we're gonna multiply our vector right here by our fireball speeds so times fireball speed okay now we need to update our fireballs position because we're not actually updating it so we'll say set position set to pose for set position get position plus motion times Delta so this just gets our current position and it adds the motion that we've just created to our current position timesing it by Delta because because our frame our Delta will be different every single frame of the game and so basically if your computer's running faster or slower we need to compensate for that and so we multiply Delta by the motion so that we should compensate for whether or not the the person using the computer if they're using a faster computer or a slower computer it shouldn't matter because we're multiplying by the Delta to compensate for that this is how we can set up our fireball to move and if we if we actually run there two ways to run the game you can actually press this right here and will run the current scene so if we run that we should see the fireball shoot across the top of the screen and it does clear up there at the very top okay did my did my project settings actually work let's come into display with height - oh so there's one last thing you need to do in the project settings which is to set the strip set the stretch mode so we're going to set stretch mode equal to 2d and that just makes it so that it will stretch the it will stretch the game the room size up to the window size now if we run this again it should show the fireball quite a bit bigger yeah there we go that's what we wanted okay now that we've done that we've created our fireball let's create a new scene for our dragon so come up to scene do new scene and this scene isn't going to need a lot of code really so we're going to let's see click on 2d so we can see the scene we're going to just use a sprite is really all we need for the scene so create a new node search sprite and you can rename this dragon and we'll save this scene and we'll just want to save it as dragon Tsun perfect that's exactly what we want and we can come over to our sprites texture and we'll set it to the dragon where's there we go the dragon dot PNG and once again we don't want the flag set on this texture so click this little teeny arrow right here click on flags and remove MIT maps and remove filter and you can see like I said it didn't really remove the filter very well but this doesn't matter when we run it in game it will look fine okay so we've got our dragon set up now our dragon is going to need basically we need a position for creating the fireball from so I'm going to add a new node this will be a position to D and we can just put this right in the dragon's mouth and that will be where we create the fireball from okay now let's add a script to our dragon and just name this dragon Gd that's fine I'm gonna put a comment up here dragon GD you can see this script in extends sprite or inherits from sprite and has a ready function as well now we're gonna need to create some constants here so let's create one Const fireball scene this allows us to get access to the fireball scene which in GameMaker terms would be like the fireball object so how do we get it we call preload fireball TSC n there we go so we're getting access to that scene will create another constant called speed this will be the Dragons movement speed and we'll set this equal to 100 inside of our ready event right here or script we'll just name we'll just set process equal to true if you don't set process the process function won't even run so you have to turn it on in game maker 3.0 it's on by default and you have to turn it off but okay so once we've got the process on we want to get our Y speed so VAR speed why we'll set this equal to zero starting out but if we actually put in some input we want to move so if input dot is action pressed UI up : speed y equals negative speed l if that's like saying else if L if input da is action pressed UI down speed y equals speed okay once we've once we've adjusted the speed we actually need to move so set position get position so we'll get our current position plus vector to creating a new vector for our X we don't need anything so there set that to zero for our Y we'll use speed Y and then we'll multiply this by Delta Delta so that's the same as you saw before this just gets our current position adds our velocity to it and then multiplies that by multiplies a motion vector by Delta to make sure that we move smoothly no matter how fast the framerate is okay let's save this and we can press this button right up here to run this specific scene dragon will be up in the top left corner but we should build and move up and down we're looking good okay now I'm going to show you how to instance the dragon scene inside of the world basically we're gonna put the dragon inside of the world this is like in game maker terms this would be like adding your object to your room as basically what it is so we've got our world instead of pressing the plus button right here press the little chain link button right here that's what it looks like anyways I'm not sure exactly what it's supposed to be but that is for instancing scenes and you can see we can choose one of our scenes so we'll choose the dragon scene and that puts it inside of this this world and then we want to move it now so we'll move it down it's important to remember that you don't want to move it inside of its own scene see how we have at the origin right here inside of the dragon scene we don't want to move it anywhere inside of here we want it to stay at the origin however when we instance it inside of our world we can put it wherever we want and that's exactly what we're going to do so I'm put the dragon right here now if we run our game we'll have the dragon inside the world we can move up and down and we've got a really good start to this so how can we create some fireballs let's come into our dragon script if we come back in the scripts click up here and come into the dragon script we're going to use another input check inside of our process function so we'll say if input dot is action pressed you I accept that's pretty much space and you can set these input actions inside of the project settings if you go to input map you can see where all these are set you can add new ones you can delete old ones but yeah you can see you I accept is set to return enter space or on an xbox controller a possibly yeah it's looking like ok so for you I accept we can then create a fireball so we'll say var fireball equals fireball scene dot instance okay now this is a perfect time to talk about this because we just barely just barely inside of our world seem instanced using this button the dragon scene inside of the world scene right press this button shows the dragon scene it put it inside of our world well when you have a scene like this that you've that you've loaded in and you call the dot instance it pretty much does the exact same thing it instances that scene and then assigns it to this variable now there's one more step to it that this button does automatically which is it adds the dragon scene to as a child of the world scene of the world node right here you can see that we have to do that ourselves and code so we want to add this fireball as a child of the world scene since our dragon is a child of the world we can do get parent in order to get access to the world node right here and then just do dot add child fireball so we do have to do that one more step ourselves inside of code this button does it automatically for us because it it just automatically added the Dragons scene as a child to the world node we have to do it in code so this gets the world node right here using get parent and then it adds the child it adds the fireball as a child of it okay now we want to set our fireballs position because if we just instance it like that it will start up in the top left-hand corner and let me just show you that because it's best to show you instead of just telling you so I'm gonna run the game and you'll see when I press the space key it creates a fireball creates a lot of them if you hold it actually but it's up in the top left hand corner of the room okay and actually real quick if we if we do that and then pause the game right here if you pause it fast enough you can come into your remote inspector and you can see we have the world node right here and we have a dragon and then we have a bunch of fireball notes because we created a whole bunch you can see they're all children of the world node okay so let's make sure we set the fireballs position so we'll say fireball set position now the position we want to set it to we're inside that this is the dragon script right if you remember our dragon has a position to D right on its mouth so we can get this position to D inside of our Dragon script so come back to the script and we'll say get node position to D and the reason we can get this note is because it's a child of our dragon so that's why we can get it then we'll say dot get global position now we can't get the child's children are always relative to their parent so if you look at the dragon you can see that the position of or the position to D has a position of only like 15 and negative 4 that's because it's relative to the parents position which is 0 0 but if we get the global position right here that will get its position globally in the game and that's what we want in order to get it to set it at the right spot I'm going to show you what happens if we just do get position because it won't work how you expect it to get position gets it's creating it from the position of the of the position to D but it's relative to where it is in the parent which is only like like we said what was it like 14 and negative 4 or something you know 16 negative 4 so that's where it's creating it as 16 negative 4 that clear up here terrible position but if we get the global position then it's going to create it relative to the entire game globally and you can see we can now move that's a lot of fire it's going to slow down the game it's a lot of fun though okay so one thing that we can do here because let's let's create a bunch of fire right well let's just create one doom that's probably more than one but it probably only created a few right went off the screen but if we pause our game if it'll let me pause the game right pause debugger may be remote inspector so we accidentally created two firewalls you can see the fireball still exists and they'll exist for forever and that's gonna be terrible right it's going to slow our game down so we need the fireballs to destroy once they go outside the room and we're gonna learn about what's called a visibility notifier which is a node so click on your fire ball right here when you're in your fireball scene add a node search visibility notifier and we'll just use this one visibility notifier oops that's the 3d one we don't want that one okay so visibility notifier 2d that's the one we want and you can see it has a little shape to it in case to tell you when this when it's visible pretty much so this visibility no Godot has something called they're called signals and basically a signal is it's like a it's kind of like an event where it just says this thing happened and you can do something whenever that happens and I'm going to show you how to connect a signal inside of the UI so our visibility notifier has if we click on the visibility north notifier and then instead of on inspector we click on node there's a signals button right here and we have a signal called exit screen and we want to use this signal so double-click on the signal and we'll open up a new little window here and what it's going to do is it's going to attach the signal or basically it's going to make our fireball subscribe to the signal so whenever the signal fires it will run this function inside of our fireball script and you can see we have make function turned on because we want it to and we are clicked on the fireball node because it has a script so when we connect it what it's going to do is it's going to create a function for us inside of our fireball script and this function right here will run when the visibility notifier detects that we've exited the screen went basically when it sends its exit screen signal and you can see now that it's connected right here it shows this little green connection and inside of here we've got our function so if we just do print left the screen and then we run the game again every time the fireball leaves the screen it's going to print down here and say oh I left the screen right so now what we want it to do is destroy the fireball so we can just call Q free what this does is there's two functions for this there's free and there's Q free Q free says destroy myself as soon as possible without ruining other things that are happening in the code so if you have other code and stuff that might run potentially after Q free can help prevent errors that way so generally you're just going to use Q free by default now we should never be able to have too many fireballs in the game because even if we create a whole bunch of them once they leave the room if we come into our debugger we can easily see that inside of our world Sea World node there's no fireballs we've we've destroyed them all now that we've got our fireball set up to destroy itself we're gonna use kind of signal we're gonna use signals again except on our dragon so that we can't fire infinite fireballs like that so we're gonna create we're gonna add a new node and we're gonna add a timer node and this node is going to have a signal as well so we're going to come into node right here and it's time out which basically just means when the timer has ended we'll connect that to our dragon I'm have it make it function for us and if we come in here in our dragons code we can just set the timer active equal to true so time well first we need to get a reference to our timer so up here at the top we can get a reference to the timer we can say on ready VAR timer equals get node timer okay not should get the node right here the reason we have to do on ready var is because this keyword right here makes it so that Godot waits to get this variable until everything is ready and that just makes sure that the timer node is already there it already exists so we have to do that but once we've got our timer then inside of our timeout we can just say timer dot set active and we'll say false so we're going to turn our timer off when it when it finishes now we need to use this timer and I'm going to create two functions and these are just functions that we can call inside of our process event so we'll call one func create fireball and we can move all of our fireball code into this one right here so copy that paste it in here make sure you've gotten the tabbing correctly Godot is very tab heavy so you're gonna get errors if you don't tab things correctly then inside of here we can call create fireball like that so we just made a function for all this so that it makes this more readable right here we know what this does now very easily and we can create another function we'll call this one restart timer and inside of here we'll just say timer set wait time 0.5 which is half a second timer set active equals true so we'll set it to active and timer don't start oh we don't have to do anything in there so this sets the timers wait time to half a second it sets it to active and then it starts it once we've done all of this we can check inside of our action pressed right here we can say if not timer dot is active so if our timer isn't eck isn't already active then we create a fireball and restart the tire so restarting the timer will set our timer to active so we'll no longer be able to create the fireball because we'll have to wait until the timer times out and then sets active to false again in order to create the fireball now we should only be able to create a fireball and just we're we're not getting anything happening right now so I need to make sure that I've got this setup correctly got a bug somewhere timer not start let's make sure let's let's check inside of our dragon scene it'll just run the dragon scene and then we'll pause the game and we'll check our remote inspector we do have the timer there so that's good let's check and see oh I know what's rolling we never actually set the timer because see this sets that this sets the timer active defaults but the timer by default is not is not active so we need to set it to active inside of our ready function or our create event you could say so we'll say timer set active false so make sure that it's not active that way we can fire it the first time and then have it obviously then set itself to false every time after there you go now we can only fire a fireball every half second because of our timer finally we're going to create our enemies and deal with the collisions so we'll create a new scene and we'll Nate will for our enemies we're going to use for the Red Dragons we're going to use a an area 2d again and you can see over here on the recent we can just double click area 2d a matte red dragon we're gonna give the red dragon a sprite and we can drag over our red dragon dot PNG and turn press the little arrow key over again this one right here turn off filter and Mitt MEPs save this scene as red dragon T Sen just with ctrl s we're gonna add another node to this we need a collision shape instead of using a collision shape to D for this I'm going to use a collision shaped polygon just to show you guys how those work coalition shape obviously it's very game dependent which one you're going to need polygons are going to take a slightly more processing power but you can get them to be more accurate so once we've created the polygon right here in in a collision shape polygon you have this nice little edit button right here that we can click and this will allow us to basically click to draw where we want our collisions to be like this you can determine how accurate you want this to be the more points the more processing power but you know and just click on this one to link it up and we've got a nice collision polygon 2d if you want to you can drag this up above the sprite so then it draws behind the sprite so that it's not in the way or you can also just hide it like this okay so if created our red dragon but now we need our fireball to be able to detect a collision with the red dragon so we're going to close out of the scene close out of the dragon scene once we're in the fireball saying you can click on the area 2d and click on note and you can see area 2d has its it has some different signals here and the signal we're going to use is area inter because this will this signal will fire as true when we enter another area so are we set up our red dragon as an area so we can double click on this area inter signal and we'll connect it to ourselves and we'll create a function connect so now we've got a function for when we enter another area and all we're going to do is we're going to call cue free to destroy ourselves and then area cue free to destroy the red dragon or whatever we're colliding with and we need to put we need to instant some of them inside of our world come back to the world click 2d I'm going to instance a red dragon scene right here and if you want a shortcut you can just do ctrl click on the node and do control D to duplicate it and then click and move it it's now I can create a few Red Dragons inside of this world that might be a bit much but it will and run the game and now you have a working projectile that's all for this video tutorial thank you guys so much for watching it be sure to check out GD quest channel hopefully you learned something new about Godot I am still going to be doing game makers to do videos so don't don't worry about that I just I want to do some videos on Godot as well I've been using it recently having a lot of fun with it and I want to do more tutorial content on it as well you guys can kind of test both engines find out which one works better for your tastes or your specific game because they have pluses and minuses for different games as well I know a lot of you have been asking me to do like a video comparison I'm hesitant to do it because I don't feel like I know Godot that well yet so we'll see eventually I may do one you guys will convince me but anyways thanks again for watching please like the video and subscribe if you enjoyed it and I will talk to you guys later
Info
Channel: HeartBeast
Views: 65,334
Rating: undefined out of 5
Keywords: Game Design, Game Development, Indie Game, Godot Game Engine, Godot 2, Fireball, Dragons, Projectiles, Attacking, GDscript, Scripting, Free Game Engine, Open Source Game Engine, 2D, Pixel Art, Particles
Id: rY7wzK59-Jw
Channel Id: undefined
Length: 46min 7sec (2767 seconds)
Published: Fri Oct 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.