Godot Beginner 2D Platformer Series P2 - Enemy A.I. , Knockback & Animations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings and welcome to part two of the goto beginner tutorial platformer series in this spot we are going to be taking a look at enemy ai and almost everything that relates to it let's get right into it but before we do that i would like to fix something with our game if you try running the game right now you're going to see that there are these gaps going through your tile map and to fix it all you have to do is go to the project settings and in the menu on the left under rendering you're going to see an option called 2d and in there you're going to see gpu pixel snap once you enable it and you try running the game you'll see that the gaps in between your tile maps are gone next up on the left menu in display in window you're going to have to go to the very bottom and set the stretch mode to 2d and aspect to keep this will help with resizing and the aspect ratio of your game now let's actually start working on the enemy character to do so create a new 2d scene and change the not 2d into kinematic body 2d once you've done so rename the kinematic body into whatever you like to call your enemy as i'm going to call it spider and once you've done that you're going to have to add in the children i'm going to add in a collision shape as well as a sprite you're going to have to add other children later on like raycasts as well as area 2ds but for now you can just follow along let's add a texture to the sprite as well as the shape for the volusion shape 2d i'm going to be adding in a rectangle shape you can use whatever shape which suits your sprite and after you're done with the collision shapes it's time to save it you're going to save it into the scenes folder if you have made one now that that is out of the way let's actually start working on the script for the enemy character create a new script and save it into the scripts folder once you've done so erase everything off that comes by default and start working on it the first thing you have to add in is a new variable called speed which is the speed for your character to move around i'm going to give it something low like 100 next step you're going to add in a new constant called gravity i'm going to give it 32 and below that you have to create another constant called up which is vector 2 dot probably you have to create one more variable for the moment it's called moment which is a vector 2. once you've added in all the variables and constants that are required to start writing the code at the below write down function for physics process in the physics process you're going to write mo with brackets what this does is call a different function which is not built in so let's create function move the first line of code that you have to write down in the move function is movement of y plus equals gravity this is basically the same line of code that you've written down for the player's gravity next step jot down movement or x equals negative speed and finally write down moment equals move and slide movement up this is what enables your character to move around now let's actually test out and see if the spider character is working so append your spider or enemy character by using the link button and then let's try it out as you can see the spider character have moved from its initial position and i have ramped into the wall so let's get back into coding next up we are going to be writing down the code to flip our enemy character and make it move the opposite direction so to do that let's add in a recast 2d i'm going to rename it into l ray or left raycast i'm going to set the cast 2 for y 0 for x negative 50 and enable it always remember to enable it i'm going to be adding in a new variable called moving left and setting it into true because initially the spider character is indeed moving to the left so around the move function right down movement or x equal speed let's go to negative speed and next to that if moving left else speed so what it basically does is if it is indeed moving left it's gonna move with a negative speed if it is moving right then it's going to move with the positive speed so before we start out we have to create a new variable so on ready var i'm going to call it l ray equals doll sign l ray i'm doing this so that every time i have to refer to the array i don't have to write down dollar sign l array so below in the physics process function write down if array dot is colliding so if it is indeed colliding we are going to call down another line of code so moving left equals false so what happens if the is if the l ray goes to the left and detects a collision is going to set the moving left into false so let's try it out so before we test it out i'm creating a new collision tile object it's a basic static body 2d and i'm going to create this since i don't have any collision in the green parts of my tile map so create a new node 2d i'm going to call it collisions and i'm going to append all the collision tile as a child of the collisions so once i've added the collision tile in i'm going to move it over to the green portion of the tile map that i do not have a collision for now the reason that i did not add a collision shape to the tile map itself is because of two things the first one is going to have to then process a lot of collision shapes and secondly it's because you're going to be able to move this invisible static body into places where you want to set a limit for the spider character or your enemy character to move so currently your enemy character starts going to the left and when it hits something it's going to go to the right to add in some extra bit of flair to this i'm going to write down scale dot x equals negative scale dot x now what happens when you take this k value and turn it into a negative scale value is that the entire kinematic body is going to be flipped so everything including the children is going to be flipped so the l ray which is actually on the left side is going to turn to the right side so you don't have to add in two different rare casts now when you play the game you'll see that the character doesn't need to move to the left and collides with something and then go to the right but once it collides then it's not starting to go back to the left and that's because you only set the move value to false you don't set it back to true to do so just set moving dot left equals exclamation mark moving dot left what it does here is going is that it sets whatever it was into the opposite of what it is so if it was false it's going to set into true if it is true it's going to set it into false now you'll notice that the spider character actually turns around when it hits the player but i don't want that so i'm going to go to the collision mask and set it into layer 2 and take it out from layer 1. i'm also going to move to every other object that is in collision layer 1 which i want to collide with this spider so i'm going to change them into collision layer 2. that includes the collision tiles as well as the tile map but not the player however you need to make sure that the tile map is both in collision layer 1 and collision layer 2 since you want it to collide with both the player character as well as the enemy character now let's add in a way for the player character to be harmed by the spider character i'm actually thinking about the classic platformer way of getting hit and getting knocked back to do so create a new area 2d called hitbox l for hitbox left create a collision shape this is going to be the defined area for your area 2d move it over to the left resize it accordingly and then you can start getting back into programming so select the hitbox and over on the right in the norm menu you will get a lot of signals in area 2d select body ended and you're going to get a new function called on hitbox l for the input so write down if body is kinematic body 2d then you're going to be able to detect if the area has been entered by a kinematic body 2d i'm going to add in a q3 just to test if everything is working now back on the right in the notes section in groups add the spider characters hitbox left into a group called hitbox now get back into the player scene and add a new chart to the player the child is going to be in area 2d and you're going to rename it into box you have to add in a collision shape as the child of the area 2d so you're going to resize it by giving it a new rectangle shape you're going to have to then resize it you're going to have to resize it slightly larger than the collision shape of the character itself now just like earlier select the hitbox and in node in signals you're going to have to connect a signal to the player's script however you're not going to be choosing body and dirt you're going to be choosing area entered and now in the function on her box area ended write down if area dot is in group hit box then you're going to print down something so that you can actually test if it is working once you test it you're going to be able to see that it indeed works you're getting a print output on either sides however you won't get one even if you go to the left side and try to move it so what's happening here the thing that's happening here is because you moved you flipped the enemy character using scale which led to all of the children of the kinematic body being flipped which led into the hitbox getting flipped from the left side over onto the right side we will be getting back to that problem later on but for now let's create a new variable it's an export variable called knockback set it into something high like 7000 and back down in the on her box area ended function start writing down moment.x negative equals lerp which is a function which linearly interpolates so lerp coma no pack coma zero point something you can give it any value that you like i'm going to give it something like 0.5 below that you're going to add in a bounce effect so write down moment of y equals lerp 0 and then you're going to have to add in two other values so you are going to create a new variable like knockback instead of writing down a value just like that so create a new variable you're going to create a new export variable in case you want to edit it so export war it's going to be called knock up i'm going to give it something lower than the knockback i'm going to write down 1000 and get back down and lerp zero coma knock up coma 0.6 just like how we wrote down the move and slide in the physics process you're going to write down one here too so because one slide movement and up so when you actually test it out you'll see that the player is indeed getting knocked back but it's not getting knocked up so why is this it's because you did not add in a negative sign before knock up this is because in godot the negative y-axis corresponds to the up direction now when you turn it out you'll actually see that the player is indeed getting knocked back and knocked up so let's go back to the physics process function and address the issue that i mentioned earlier all you got to do to fix the problem is instead of scaling the x of the kinematic body you scale the x for individual children so a rate or scale x equals negative l radar scale x so next step create a variable that references this right child of the kinematic body node so already war sprite equals double sine sprite so below in if already is colliding write down sprite dot scale x equal to negative sprite dot scale x [Music] so afterwards when you finish writing and try out the game you'll see that the player character is indeed getting only knocked to the left which means that the left hitbox is indeed functioning as intended and not flipping out to the right side so now you need another hitbox for the right side so duplicate the hitbox l rename it into hitbox r and then move it over to the right side now that you're done with that you're going to encounter a new issue which side do you expect a player character to get knocked over to you need the player character to be able to knock over to the left as well as the right so as to tell the computer where to knock the player over into you're going to create a new variable a global variable to be precise to create a global variable create a new node 2d rename it into global create a new script for it here is everything that comes by default and then you're going to create a new variable in this script now how to go to project project settings auto load click on that file icon and then in scripts you're going to have to load in the script for the global node click on add and now you basically have a global variable now over in the text editor you're going to have to create a new variable i'm going to call this variable hit site you don't have to give a value to this hit side variable right now because we will be giving it in the code for the spider character next up we are going to add in the area ender signal from both the hitbox l and hitbox r over to the spider script i need the function on hitbox l area ended you're going to write global dot hit side equals negative one and in the function on hitbox or area ended you're going to write down global dot hit side equals one and in the player script i'm going to create a new variable called hit s which refers to the global.his site but do not create this variable you don't have you do not have to use this variable because once you do so it's going to lead into an error that i ran into later on in the recording i'm i'm going to be fixing it later on so just don't create this variable at all so under function on herd box area ended create a new variable it must be under that function create a new variable called knock site in this variable give it the knockback times global.hit site and this is kind of about where i ran into this issue that i mentioned just now so i removed hit s from this line of code and changed it into global dot his side and i removed the hit as variable entirely i don't give by referring to the global build.hit side using another variable doesn't work but now i fixed it so yeah if it works don't fix it so as you can see from this clip the player is indeed getting knocked back and you're just soaked that the player just flew into the sky so what happened while i do not really quite realize what the exact problem is what i found out the problem to be is that the player is actually touching the collision box the hitbox from the top now this is actually going to be fixed later on quite easily so don't worry about it i'm going to be changing the speed of the enemy character a little bit to 75 from 100. i'm doing this because as you saw earlier the player just keeps on getting pushed up and then when it comes down the enemy is right under the a player character so it gets pushed back up again so i fixed it currently with decreasing the speed of the enemy character and now we have no pack implemented into our game there's only one more thing to do and that is animation for the knockback i'm going to be creating a kind of a blink or a flash animation if you want to call it so let's see how we can do that so before we do the animation we have to create a new reference to the sprite so already war sprite equals dollar sign sprite i'm going to create a new reference to the sprite because i'm going to be doing the animation using code so under on her box area and dude we're going to write in a new function called blink so let's create the blank function so below that write down function blank and inside blink function we are going to be writing a few several lines of code and that is sprite divisible equals false yield dot get tree create timer 0.05 and timeout this is how you create a timer in godot using gd script so what happens is the sprite is set to invisible waits for a timer sets back to visible again waits for a timer sets back to invisible and waits for a timer until you set back to visible again this is how you create a quick little bling flash animation using the gd script if you wanted to create an animation you can just create an animation and get the animation player to play it using code so let's resize the collision shape to these for both the hitbox l and hitbox r and i'm going to be moving it down i'm going to be resizing it because i'm going to add in the next collision shape which is going to be the hood box for the spider you need a way to kill the spider and what i'm thinking is letting the player jump onto the character or jump onto the enemy and kill it classic mario style so just like how we did with the hitbox in her box for the player character create a new area rename it to her box add in a collision shape add in a shape for the collision shape i'm going to use a rectangle shape again you can use whatever which suits your sprite you almost always need to use a rectangle shape for the collision for stuff like this but anyway let's resize it i'm going to be increasing the width of this collision shape a little bit more later on that's how i fix the problem that i mentioned earlier on in which the player just flies off into the sky so for the hitbox find the signal called on body and dirt and add it into this script for the spider this is how we are going to be detecting if the player character has actually entered the area 2d and if it did you're going to queue free so write down if body is kinematic body 2d then q3 so once you run the game you're going to notice that the spider character is gone reduced to atoms so what happened here the kinematic body 2d of the spider character itself is overlapping with the herb box's collision shape which triggers an instant death so change the herb box collision over into the second layer and enable the second layer's collision for the player character now when you run the game you will be able to see that the player can actually kill the enemy and it does not kill itself you will have to change the collision layer for the ray cast of the spider the raycast which rotates the spider around to the layer 3 and also the collision for the invisible static tiles we made so that the spider can turn around and also the layer of the tail map all these three must be set to the third layer instead of the second layer and that is pretty much all there is for this part of the tutorial series i'm going to be coming with the next one soon enough so stay tuned for the next one and until then good bye
Info
Channel: DeCipher
Views: 12,824
Rating: undefined out of 5
Keywords: DeCipher, Godot game development Beginner tutorial series, godot 2D platformer Series, game development tutorial, make a 2D platformer game from scratch, unity, game dev, platformer game development, make a platformer game in godot, borncg, 2D player controller for godot, platformer enemy A.I., godot animation player, kids, cancode, make your first 2D game in godot 3.3, godot knockback tutorial, 2D knockback in godot, how to create knockback in godot, indie game dev, knockback effect
Id: KXsVXH6055M
Channel Id: undefined
Length: 19min 7sec (1147 seconds)
Published: Fri May 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.