C# Godot | Creating a Simple Slime Enemy AI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is mitch with fine point cgi and today we're going to talk about making a slime enemy inside of good dough so we're going to go through the process of setting up our npc we are going to go about making it so that it can't fall off of any platforms it's on or things like that we are going to add in the ability to do damage to the player and last but not least we are also going to allow it to kind of collide against the wall and turn around and continue on its way so that's what i have to start for you guys today let's go ahead and get started okay so the first thing that we're gonna do is we need to create our slime npc scene so we're gonna come up to our scene here we're gonna click new new scene and we are going to create a 2d scene we're going to go ahead and rename this scene as slime enemy and then we need to make it look like a slime so the first thing that we need to do is we right click add child node add in a sprite let's go ahead and focus in on that you can see that we were off a little bit so now i'm going to come down here to our platform metrovania we're going to go to our enemy sprites and our slime and i'm going to take my walk animation right here and i'm going to drag that in as my texture i'm going to come down here to my animation section and i'm going to go ahead and set my h frames up to the value that makes sense so in this case it's going to be 15 so if i go ahead and walk through all my frames you'll see that he jumps and lands which is perfect that's exactly what we wanted so now from here we need to go ahead and make it so he can animate right so he can jump up and down when he's moving so let's go ahead and right click on our slime enemy add a child note add an animation there it is animation player we'll go ahead and add that and then we're going to come up here to our animation section we're going to click on that add in a new animation and call it move and then we're going to go ahead and click add track add a property check and click on our sprite we're going to hit ok and what we're going to do is we will add in our frame as our thing we're moving so we're going to click on that and hit ok and then we are going to go to our sprite and put in a break point on our v frame so break point we don't need to use bezier curves we're going to go ahead and create that whoops not our v frame i'm sorry our frame we're gonna click and you'll see weirdly enough it moved up to one and it created ourselves a little animation image here so if i start going through here and i just hit the key it will just keep incrementing as i go so i can just keep clicking and there we go our entire animation is done i can actually just kind of come in here and hit uh loop and then hit play and there we go that easy that's one of the awesome features of godot is that you can actually just keep clicking that key and it'll kind of put in each frame afterwards for your animation so it saves you a lot of time now from here we need to give this little slime guy some collision so let's go ahead and add that in so what we're going to do is we will add a collision shape to and you'll see that it says hey um this doesn't work unless it's an area 2d a static 2d a rigid body or kinematic body now we're going to be controlling this little guy through code so we're going to want a kinematic body so let's right click add in a child node kinematic body 2d and we are going to right click this and make it the scene root so i'm going to copy the name and paste it and actually i don't need that slime enemy section so we'll go ahead and get rid of that there we go so now it's a kinematic 2d and what we're going to do now is we're going to click on our collision shape we're going to add in a small little rectangle shape and we will make it about yay big if i go to my animation player and i kind of run this through oh i'm getting some errors here oh it's mad at me because it doesn't think that this exists so let's go ahead and trash this sprite add a track add a property track sprite okay and let's go ahead and add in our frame and then let me come over here and just start off at one and then run through again goodness there we go that fixed that issue and now i'm going to see how high the little guy jumps he jumps about that high and he lands about that far down so i'm going to expand this and move it down just slightly there we go and honestly now that i'm thinking about it i actually don't really need collision to be that big he just needs collision so that he hits the ground so i'm gonna go ahead and do that there we go i think that'll work so now that we have some basic collision shape we need to detect if we're on an edge so what we can do is we can right click add a child node and add in a raycast 2d not 3d whoops it's my fault right click add child node raycast 2d and then i'm going to go ahead and name that left raycast and i'm going to duplicate it and name it right raycast now i'm going to click on my right raycast and i'm going to move it over to the right and for the left right cast i'm going to do the same thing but move it over to the left and then i'm going to take these two and make it so they only cast to about 10 i don't need them to cast so far down i don't think it's necessary and what this is going to do is it's going to say hey i'm going to raycast down and if i don't detect something then i know this is an edge and i need to turn over and rotate and go the opposite direction and i'm going to go ahead and enable both of them so that they're enabled and then let's go ahead and get to coding so let's right click here attach a script and call it slime enemy.cs and visual studio will pop up let me make this a little bigger for you guys and as always visual studio is yelling at me to update okay so the first thing that i'm going to do here is i'm going to go ahead and grab my objects so i'm going to make some globals for it because it's going to be easier if i do that than if i don't so like for instance if i'm going to be modifying like velocity and things like that like moving an object i'd rather have that up here you know as a local variable because it just makes things a little easier so the first one i'm going to do is my sprite so sprite sprite i'm going to do a raycast for my two ray casts and i will call this one bottom left and i'm gonna do the same thing but bottom right so ray cast and then i need to go ahead and do [Music] my velocity so i will say private vector 2 velocity so now i'm going to come down here and type sprite is equal to get node sprite and we need to point to where our sprite is which in our case is called sprite so we'll do that and then we will say bottom left is equal to get node raycast 2 d and we will say i think left raycast right raycast i guess left right cast and then we'll say bottom right is equal to get node ray cast to d and we will say right raycast awesome so now we want to move this on physics we don't want to move it on delta process because if we move it on delta process it's not going to move during the physics frame and i'd rather have everything kind of move on the physics frame versus on my frame rate it's just a little bit more stable that way so we'll go ahead and do that so let's get rid of this and we will say public override void underscore physics process float delta and i don't need to worry about my base dot float delta the first thing that we're going to do is we're going to add some gravity to this so velocity plus equals gravity right so we need to come up with what our gravity is going to be for this character now i'd like that to be global as well so i'm going to come up here and go private into gravity i'm going to make that equal to about 200 because that's what it was for the player so i'm going to make it the same so i'm going to say plus equals gravity times delta and i forgot it's going to be velocity.y not just velocity all right so now that we've got that what we need to do is we need to make sure that we don't go too fast so we're going to say hey if velocity dot y is greater than gravity then we don't want it to go past that point so we'll say velocity dot y is equal to gravity oops not grid container all right so basically this is just creating velocity for the character so when the character can just fall and then it'll land on an object and stay grounded to that object now what we can do is we can go ahead and move our character to the right so what i'm going to do is i'm going to come up here and i'm going to give it a speed parameter so private int speed and we're going to make that equal to 30 and we'll go ahead and set our velocity here dot x to 30. or actually we'll do it to speed instead to 30. that way we can mess with it later all right now from here we need to go ahead and move our object so we're going to do is we're going to say move and slide velocity comma vector2.up and that's just going to allow us to move our character based off of our velocity and i were to play this i got a feeling it's not going to work out very well well actually it's not going to work out because i just uh because he doesn't exist on the on our plane yet does he so i'm going to save this as slime enemy i'm going to come over to our player test scene and i'm going to drag in our slime player here so let's our slime enemy i should say i'll drag this in just throw it somewhere in our game manager area and let's go ahead and grab him and put him somewhere like here ish i think we'll be okay if i go ahead and i hit play you should see him going this way look at that perfect that's what we wanted now you'll notice that he just keeps going and he just falls until he hits this and then he just kind of stays which is good and bad so we now know that gravity works so that's good now we just need to make it so that he doesn't um fall off edges right we don't want to make him that stupid right we want to make it so he sees an edge and doesn't just fall off so what we're going to do is we are going to go ahead and shoot out some rays for collision and see if anything comes back so what i'm going to say is if not bottom right dot is colliding then i'm going to want to go the opposite direction so velocity dot x equals minus speed but he's just going to turn around and move the opposite direction and then i'm going to do the same thing but if the bottom left is colliding so else if not oops i misspelled bottom bottom bottom all right bottom left dot is colliding right then i want my velocity dot x to be equal to speed all right so what that's going to do is say hey if these aren't colliding then go one way or go the opposite way so now if we go ahead and hit play he'll go my way then he hits this and he bounces right back see that simple enough awesome so now we have him bouncing left and right okay so now from here we need to go ahead and tell him to flip his sprite if he is going one way or the other so what we'll do is we will say hey if this is not colliding sprite dot flip h is equal to true we're gonna do the same thing but the opposite here we're gonna go here and paste flip h is equal to false and this may or may not be backwards so let's go ahead and check it out and make sure oh he's going backwards yep he's moonwalking so let's uh switch that over to true and false and that will go ahead and fix that and from here now we know that he's moving and he's going to be moving back and forth so let's go ahead and make him play an animation so we're going to say hey if animation uh do i have access to oh i should probably make an access up here because i was going to just do get node but that would be really inefficient so let's do private animation player player and let's go ahead and grab right here and i don't think i would want to do let's go with animation player instead actually because i was going to just do player but if i do player then if it needs reference to the player it would be a it would be confusing so we'll go get node animation player and we will make it whoops this animation player alright so that should allow us to get access to the animation player and we'll go here if animation player dot is playing and we want to say if not actually because i did if um then we want to go ahead and play our animation because we don't want to play our animation if it's already playing because then it's just going to overwrite our previous animation that it's playing and i want to finish the animation before it continues playing it again right so we now can say animation player dot play and we can do our move animation all right so now if we go ahead and hit play you'll see he's now running and jumping awesome that's exactly what we wanted so now we want the slime to be able to do damage to the player so we need to go ahead and set up some collision detection here so let's go ahead go into our slime enemy here and then let's go ahead right click and a attach a child node and let's go ahead and add in a area 2d and we're going to right click and add another child note and we're going to add in a collision shape 2d and we'll go ahead and make that into a sphere and let's go ahead and drag that out to be something like i don't know let's see if we go to our animation player we have him jump up what uh what are we looking at um about yay big let's see collision shape if we drag this down and we make it about this big which is about how big he is right and then we go ahead and we make sure that we animate this with him so let's go ahead and go to our transform and keyframe that and go ahead and add in a position keyframe so you can see a little position keyframe going in and then we will bring this up like that will lock another position keyframe so you can see how it squishes with him and then when he jumps we're gonna want to get him up to the top of his jump and pull that all the way up like that and put another keyframe there and then he's going to fall back down and you'll see that we now have our little animation here so we hit play awesome now you can see that there's a little bit of finickiness here i believe it's right i guess that's okay i mean you have that one little snap right there that we could fix up just a tiny bit by pulling this up and throwing another break point and then move to the next one yeah that should be fine so now if we hit play there we go so now the little uh collision shape will follow the character so that way you can get hit by him when he jumps up but when he's down you're not going to get hit by him is the gist of what we're trying to do so now that we have that let's go ahead and click on our area 2d go node go into on body enter go ahead and double click that and click on our slime enemy and let's go ahead and copy this connect it and let's come down here and go public void on area 2d body entered and taken a body i'm sorry taking an object and name it body whoops taking an object name it body go ahead and open and close and say if body is player controller then we need to go ahead and do damage to that player controller so we can say player controller player is equal to body as player controller and then we'll say player dot take damage and we will give him the amount of damage that we need let's see does this take in it does not okay so everyone takes one point of damage i forgot if i changed it so this would take in damage or not all right so now if we go ahead and hit play we come over to our game we hit play here we come over here we jump get hit by the enemy and there we go you can see we took some damage perfect that's awesome all right so now we have one last thing that we need to do and if we go ahead and we take a look at our slime enemy and let's drag it down here so let's say it's down in this area and we go ahead and hit play we kind of run down here and fall you'll see that the slime enemy just keeps going in one direction and we don't want that so what do we need to do well we need to put raycast so it knows which direction it needs to go for it to be able to detect if there's a wall on this side so let's go ahead and close that and let's go ahead and go up to our slime enemy here right click add in a child node and add in two raycasts so raycast 2d let's go ahead and duplicate that and let's call that left middle ray cast let's call this one right middle raycast awesome so now if we save this come over here we can go ahead and add in right middle and left middle then we can come down here we can say left middle is equal to get node raycast 2d quote and i believe i call it left middle raycast all right i'm going to copy that i'm going to say right to middle and i'm going to change this to right middle raycast so i should match up so now we can come down here and we can say else if not right middle dot is colliding then if we look at our bottom right you can see we're doing velocity speed minus x so we'll go ahead and do that here and then we'll basically just copy and paste this as well and we will say left middle whoops not left middle and actually we'll put we do need to be colliding so let's go ahead and do that and now if we go ahead and save this and we come over here and we hit play hope it doesn't seem to be working hold on oh that's because i'm dumb okay my bad so we gotta take these and we gotta move them into the correct position so left and right and then we need to go to our inspector transform z rotate this 90 degrees it looks like this one needs to be negative 90 degrees and then we do the same thing with our left raycast so we need to go to transform rotate this 90 degrees we need to select both of them and make them about 10 or so and honestly i kind of want it to be closer so i'll put it somewhere like that and then i'll grab this one and do the same because i kind of want them to be really close to the guy so that way he doesn't uh so he can get close to the walls instead of being super far away so now if we let that run i 90 sure yep i forgot to turn him on i just noticed that so let me go ahead and enable them and then let's see what it does and there we go you can see he runs and he hits the wall and he turns around oh and i just got hit awesome sweet and you can see that he got caught on that that's the thing i did not expect now one last thing before i call this tutorial done is if we want to fix that collision issue what we can do is we can come down to our slime enemy go ahead and take him off of layer one and on to layer two now if we go ahead and we hit play what's going to happen is our slime guy is going to fall through the world because he no longer is colliding with the world so what we would need to do is we would need to come up to our tile map here and we'd have to change this to collide on both layers here with the correct masks now something else that we'd have to change is we'd have to come over here to our our left and right middle raycasts and change the collision mask to the middle as well so that's something else that we'd have to make sure we do although we don't have to but it would make it so that it collides with the platforms um so we don't have to get rid of these but we can and if we do it will solve that issue as well so if we go ahead and we hit play you will see if i drop down here he's now here he exists so that's good news and he's not being stopped by the platforms so that's awesome that's exactly what we wanted now we do need to wait for him to go through a platform so i'm just going to go ahead and let this go and we will go from there nice see you can see that he went right through it i know it's hard to see but basically he goes right through it but that is all i have for you guys today so if you like this video go ahead and hit that like button hey you know if you dislike this video go and hit that dislike button because i am here to make content for you guys now this tutorial was a viewer suggestion as always i take your guys suggestions very seriously so if you guys have any suggestions go ahead and throw them in the comments below and i'll be more than happy to take a look at them if you guys have any questions about this or anything that you have seen on my channel before you just want to hang out and chat come by my discord say hello we would love to talk to you you know the group of guys that are there are all really cool people but that's all i have for you guys today so thank you so much again for watching and i will see you all next time thanks you
Info
Channel: FinePointCGI
Views: 2,069
Rating: undefined out of 5
Keywords: godot, godot engine, godot game engine, creating a game, how to make a top down shooting game in godot, how to make a top down game in godot, how to make a game in godot, creating, make a game, how to make a game, enemy ai, learn godot 3, how to creat a character like genshin, simple platform game, enemy that jumps towards player, water in godot, jump attacking enemy ai, jumping enemy ai, godot game, godot tutorial, jump attacking enemy ai in unity 2d
Id: yxlt8rGPbBM
Channel Id: undefined
Length: 28min 9sec (1689 seconds)
Published: Mon Sep 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.