How to do Enemy AI 2D - melee attack, turn around in Godot 3? Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial you will see how to program a basic enemies like this fellow he can walk left right and turn back if he detected the hole he can also detect the player and attack him i prepared here a basic enemy which is kinematic body2d the node type is important because we'll use the built-in function like move and slide i also added a sprite basic collision shape and a few animation if you're not familiar with collisions you can learn more about them in my tutorial how to do collisions in the godu right now our enemy is just standing on the scene let's go to the enemy script and write a move character function in the process function it's a simple function where we move the character to the left in this case 32 pixels per second and we also add a gravity to it more about gravity you can find on my gravity and movement tutorial the link is on the description as we can see the enemy is moving left with gravity effect and that's all next we need to detect the hole in front of the enemy we'll use ray custody for this job in my project i'm using basic layers for player enemy and environment for the collisions more about it you can find on my tutorial about collision which i meant earlier i will raycast today need to detect the environment which is a tileset in my case the logic is very simple if the enemy doesn't see the environment he need to turn around let's add a raycast 2d in the inspector collision mask we can set what the layers the object can see it will be the environment in my case the node needs to be also enabled let's move the raycast somewhere over here and that point is important to use for collision detection now let's go back into the script and add a new function detect turn around first condition is for detecting the collision using raycast 2d if it's not colliding with the environment that means the enemy has a hole in front of him the second condition is a built-in function to detect if the kinematic body is on the ground let's test it and now we can see the output of the console when we need to turn around a good idea is to hold information about where the character is moving let's use the variable is moving left equal to true by default it is because our character is starting from moving left let's use this variable in our move character function that means if is moving left is true then we'll use minus speed value and otherwise we'll use positive speed volume if you don't know one line conditions that will be the same as this piece of code but in more elegant way after this let's use our is moving left volume in the detect turn around function it will be toggled each time when the enemy will detect the hull and as you can see it doesn't work but why to solve this riddle let's use debug and check the visible collision shapes each time the character wants to turn around we need also to set up a site for the raycast and also flip our sprite let's do this it will be a very fast solution and i will explain this in a moment just to the scale x is equal to minus scale x and now we have a nice working movement for the enemy but what happened we just changed the scale transformation for this scene at the start we have one unit of the scale but if we go with negative value all the children should be flipped horizontally in a slow motion it should looks like this this method can be also used for nice flipped animation next thing is to detect the player let me show you a theory for better understanding we need add a player detector to detect the player in front of enemy use attack animation when the player is resigned add a attack detector to check if we hit the player it will be active only for a very short time so about the time time for practice let's add a arriga 2d with collision shape to d to detect the player let's name it player detector and set up the transformation and the layers it needs only a mask to detect other layers now let's add the second area to d with collision shape to d to attack the player it will be very similar to player detector but let's turn off the monitoring and monitor able the first one is for the targeted bodies and areas the second is for being detected after disabling both this area can see and is invisible now let's add a three simple function heat is for activate the attack detector end of hit is for deactivating the attack detector and start walk is for starting the walk we'll use this function on the animation player hit and also end of hit function is used when we want to check the collision between the weapon and the player the last thing to do is to connect detector signals to the script let's connect the player detector using the body entered signal inside we only need to start the attack animation now let's go to attack detector and also connect the body entered signal inside we can just reload the scene if the weapon hit the player we need to also stop moving the character while attack so let's just stop the process function if animation player current animation is equal to attack and that's all for this tutorial as you can see the enemy can detect the player and attack him if he managed to do it the scene will be reloaded so thanks for watching remember that you can find a link to the script on the description like the video if you liked it dislike if you didn't and have a nice day night and see you later party time
Info
Channel: Rafa Fiedo
Views: 44,136
Rating: undefined out of 5
Keywords: godot, godot engine, tutorial, enemy, enemy ai, godot tutorial, godot enemy, godot 2d game tutorial, godot 2d tutorial, godot tutorials, godot 3, godot melee attack, godot platformer, godot enemy ai 2d, godot enemy attack, how to make enemy ai in godot, godot platformer enemy ai, godot platformer enemy, godot enemy turn around, godot enemy ai, rafa fiedo, devlog, why are you reading my tags?
Id: 4WywpSBncFI
Channel Id: undefined
Length: 6min 16sec (376 seconds)
Published: Thu Mar 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.