Make an Action RPG in Godot 3.2 (P13 | Knockback and Enemy Bat)

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 part 13 for Godot action RPG series in this video we're going to start creating our bat enemy and I'm pretty excited for this however by the end of this video we won't have finished the enemy it's a pretty there's kind of a lot involved in making the enemy so it won't all be in one video it will probably span multiple multiple videos but let's get started and get some of the basics in so we can continue to add to our bat so create a new scene and we're gonna choose a kinematic body 2d for our bat and we'll call this bat and we'll save and we'll come into enemies and save in here now our bat is going to need a few different things to make up a few different nodes to make up what this enemy is going to be so the first thing we'll need is a sprite and this sprite will contain our bat PNG which you can see is an animation so it's got 1 2 3 4 5 frames so come into animation set our H frames equal to 5 and save this bat we want the origin to be down here you know the the actual node T to be down here but the bat is going to have the appearance of flying so we want to set the sprites transform up a little bit higher or it's offset we can use the offset so coming to the offset and we'll set the Y to negative 12 and this will place the bat up here higher inside of our shape inside of our scene and but the actual origin of the origin of the bat will still be down here the sprite is just drawn up here and that will help give us the appearance of flying so we're gonna add another sprite here and this will help with the flying as well and this will be called currently it's just called sprite 2 but we're gonna call it shadow sprite this is going to be the shadow of the bat so we'll come into our shadows here and I think I use the small shadow for the bat and there we go now we get more of an appearance of flying as well you can see that we start to get this illusion that our bat is actually hovering above the ground now we're going to create an animation player well actually we don't need an animation player for the bat we could actually use an animated sprite instead of a normal sprite I just realized because our bat is just going to fly constantly so yes silly me let's fix that so let's change this to let's change our sprite here change type to animated sprite and I might change the name there we go and we'll come into our frames create new frames and click on that and we'll call this animation fly and we'll select this little box here come into our enemies grab our bat open and we have one vertically and one two three four five horizontally there we go select all frames add all five frames look at remembered our offset so that's good we want it to be playing and that's all we need is just to set it like that we'll probably have a 15 frames per second again I don't remember how fast I'm going to check to see how fast my bat animation plays inside of my reference project because I want to make this look good so it's definitely slower than this I'd say it's more like 10 here [Music] that's pretty close 10 frames per second is pretty close and that looks better for the animation I think okay perfect so because our bats moving and idle frames are exactly the same and we're not going to need to setup a blend space 2d for the bat because we're just gonna flip it sprite left and right depending on the direction it's facing that allows us to use an animated sprite instead of the more complicated setup that we have for our player and technically it is possible to use an animated sprite for the player but you don't get the benefits of the blend space 2d you'd have to write the code for that yourself so here's our bat we need a collision shape for our bat though and this collision shape is going to represent the bat as it it's going to represent the bat as it with as it collides with stuff in the room so you want to try and imagine this in a 3d space here and so we're gonna add a new collision shape and we're going to do a little circle shape and we're gonna set the size of this shape to be just inside of the shadow here and this might seem like a really weird spot for the collisions to be but you're gonna have to trust me on this one this since our bat is hovering above the ground you have to imagine like a tube coming down from the bat to the ground and so it's actually if we want it to collide with the player or with other things in the game like the walls and stuff this is about where that tube is going to touch the ground the same the same spot that our shadow is really and so that's why we're setting it down here at the bottom of the bat even though that seems kind of odd okay so we've got a collision shape we need to set our bat so that we need to set the the collision layer that this bat is on we have an enemy collision layer we want the bat to look towards the world layer but it doesn't need to be on the world layer so we're getting back into our collisions again here so we're going to turn off the world layer here and come into our project settings general come all the way down to our physics and we'll create a bat or an enemy layer here just in case we need it I actually don't use this layer but it just makes sense to put the bat on an enemy layer and it's showing up as layer five here we'd have to change scenes and come back and then we get the enemy layer and that makes sense okay so currently our bat won't actually collide with the player and that's good and I'll put the bat here in the room let's click on our wise sort here and let's add our bat let's drag it stick one maybe here one maybe down here and one maybe over here just did ctrl D to duplicate those again and we can run the game because of the way we've set up our collision layers we don't actually collide with the bats we just kind of move through them and so why do we do that well let's let's say that the bat is looking to the player layer as well as the world layer so that would allow the player to collide with the bats well the problem is they feel too solid you can't move through them you can't push them and this isn't the kind of collision we want with the bats especially when they start moving they would just push the player around which would feel really weird because our player has a larger mass than the bats and the bats are flying so it feel more logical for a player to push the bats around so the solution to that problem is actually that we're going to make up our own fake physics for these enemies but we're going to be doing that in a later video so we've got our bats in the room let's actually give them a hurt box right and so one of the great things about having a hurt box is its own scene as let's talk about briefly the difference between inheritance and compositions so in programming you can kind of you want to have reusable stuff right you want you don't want to have to write your code multiple times every time you create a new enemy you don't want to have to rewrite the code for the enemy so you want to have reusable stuff and there's two generally accepted ways to get reusable stuff one of them is called inheritance and it's kind of the one people learn about early on in programming you can have one object inherit from another one and then it inherits you know functions or variables or whatever from that other object and the other method is called composition which is where instead of inheriting something from another object you compose your bat enemy for example instead of your bat enemy inheriting from some enemy class you compose your bat from a whole bunch of different reusable components and generally you want to try and use the component pattern over inheritance if you can and so in this case instead of having a bat enemy that you know that inherits all this stuff in regards to some sort of a hitbox we can just use our hitbox seen here as a component right hip hurt box seen here as a component and we can easily set up the hurt box for our bat with a reusable component that doesn't require us to set up a lot of code we just set up the hurt box that we want for it and hmm let's see I think maybe right about there is what I'm gonna do so this collision shape is a capsule obviously I've got six and six and then it's position is going to be negative 13 and 0 and it doesn't have to be exactly like this I wanted to have a little bit I want to be generous with the hurt box to make it easier to hit the enemies so I might even make this a little wider and come down by one I don't know you want to make it easy to hit your enemies and then be hard to be hit by the enemies always skew the game in the players favor that way it always feels more fair to them even if it's technically not that fair that's more fair for the player than it is for the enemy so ok then we can take our hurt box here we can set this to be coming to our signals here or our collisions here we want to put this on senses a hurt box we want to put it on the enemy hurt box layer and it doesn't look towards any other layers and then we'll set we'll use our signal from this hurt box I'll say area entered connect to the bat oh oh oh it's because we don't have a script so attach a script to our bat here it'll just be created under enemy's bat Gd save you can actually get rid of all of this for now because we don't need any of that then come into our hurt box connect the signal to our bat and what do we want to do when it gets hit well let's start off with a Q free just to test this and make sure that it works ok we can come in and move around our bats and we can now attack them and kill them okay but that's kind of boring we don't really want to just end the video on that I think so what we'll instead to is we'll add to our bats we'll add a knock-back variable and we'll set this to zero for now and then we'll add our physics process [Music] inside of our physics process we'll say knock back equals move and slide knock back and now inside of our on hurt box area entered well we need to have some sort of a friction on our knock back as well so knock back equals knock back dot move toward and then vector 2.0 and why can't I get the zero to work there there we go and then the Delta that we want to have for this friction is going to be pretty low because our enemies are they're flying enemies so they don't have a lot of friction basically this friction is going to be fake because they're not actually touching the ground but they would be trying to fight against the knock back motion and move towards you so we will reduce this down so we'll say what do we do for our player I don't remember what we did for our player let's look at that we did 500 so let's say 200 times Delta and I think that's what we want to do check out our friction for our player friction times Delta yep that'll looks good okay so now when we get hit we can set our knock back to something right so let's say knock back equals vector two dot right well let's do yeah let's do write for now times yeah I don't I'm gonna have to make up some number here because I don't actually have this exact set up maybe we'll do 400 [Music] we'll have to see what that looks like that might be way too much knocked back and then it's set up to always go right so let's attack from this side so it looks good and there we go that's probably too much so let's set this to 100 try again and that's probably too little right so it's crazy the difference between two between I mean it is a big difference right it's four times the amount but it seems so much more significant and we probably want something in between those two so 160 maybe where we get a little bit more knock back still too much I think 150 maybe [Music] whoopsies my keyboard disconnected and reconnected it's been having issues lately it's an old Apple keyboard it's a nice keyboard but still I still feel like that's too much but 100 was way too little it felt like so that seems like the right amount to me we'll have to see because when the enemies are moving towards us we may end up changing this value later so how do we calculate the knock back properly so that when our enemy gets hit we we get knocked back based on the players the direction the players facing before I was actually using the role vector for this of the player so let me show you the code that I had set up for this we're probably going to be changing this later because I don't like this solution at all so we'll save our knock back vector equals okay now our on heart box entered gets in gets the area that we're colliding with so we can say area dot [Music] knock back vector hmm this is what we should do I think actually let's just fix this right now instead of doing my terrible ugly ugly code knock back vector so then we can just say we can do this actually like this [Music] area dot knock-back vector now we could use ya let's come into our player now and let's let's give our sword hitbox its own script okay and we don't want this to be overlapped hitbox GT that's not where we want to put this we're gonna put this inside of player and we're gonna call this sword sword hitbox GT okay like that create so we've attached a script to this and we're gonna give it just a variable here at the top knock-back vector will set this equal to vector 2.0 for now and then we'll come into our player and in our ready state we will set well we want to get access to our sword hitbox right so let's do that here we'll say on ready var sword hit box equals dollar sign here sword hit box like that so we get access to it and then inside of our ready function right here we can say sword hit box don't knock back vector equals roll vector so I'll set it to the same as our roll vector and then we'll do the same thing inside of our move state here whenever we set the roll vector will set the knock back the the sword hit box not back knock back vector knock back vector equals input vector as well just like our roll vector so basically our knock back vector is going to be the same as the direction that we were just moving in that way if you're moving up and to the right and you hit an enemy it will move it it will knock it back up into the right if you're moving to the right and you hit him hit an enemy it will move it to the right so we're storing that information inside of our our sword hit box and this should work theoretically we should be able to attack them to the right up up and to the right it's hard to see straight down into the left there we go and it's colliding with the wall right there and we may want them to kind of bounce off the walls we'll see how that goes when they get hit so we can change that later but you can see we can now hit them in any direction we want now if you wanted to you could make it so that you couldn't knock them up into the right I personally like to be able to hit them in the direction that I'm moving it makes the most sense but you could set it up so that they actually got hit to the left if they got hit to the direction that you're facing right because technically when you're moving up into the right you're facing left so that's a personal choice but I definitely prefer having them go the direction that I'm facing it makes the most sense to me so there you go now we've got a little bit of knock back in our game for the enemies and I think that feels fun I think that's a better place to end this video on in the next few videos we'll be adding in the hurt boxes for our enemies so they can actually damage the player we'll be giving them stats stats components so their own health will be setting up their artificial intelligence and a few different things like that so it's going to be exciting over the next few videos this video is sponsored by my 1-bit Godot Kickstarter backers I want to thank them for their support and thank you for watching this video I hope you enjoyed it and learn something if you did give me a like and hit the bell down there subscribe to my channel and I will talk to you all later
Info
Channel: HeartBeast
Views: 65,391
Rating: undefined out of 5
Keywords: Godot Engine, Godot 3.2, Godot 3, Tutorial, Series, Action RPG, RPG, Pixel Art Game, Indie Game, Game Development, Learn gamedev, Gamedev
Id: mm4D1ETclAc
Channel Id: undefined
Length: 21min 11sec (1271 seconds)
Published: Wed Apr 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.