How to make enemy patrol in Godot || Dicode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you are creating a top down game then you want your enemy to petrol around the place in this video i will make two kinds of petrol one will be in the circular path and one in a linear path right now i have an enemy that is able to detect player aim at him and shoot a bullet at him if you want to know how this all works then you can watch this video on youtube i will put a link in the description i've just done the same thing that told in this video now to move our enemy we will use this path to denote if you want any object to move around the scene it is the best way of doing it now select the path to denote and click this icon to add new points this pad 2d will create a path along which our enemy will move now moving our enemy in a circular path is easy so we will do that first for this you need to draw a closed path and keep in mind that you need to close the path properly in order to make it work for this click on this button and it will automatically close the path now click on pad to d and add a path follow node to it this node will move along the path that we have created and everything that we want to move will come under it so add our enemy as a child of our path follow node as you can see our enemy is now on the path that we created to move it along the path we come to pathfollow node and here we will find an offset this is the property that is responsible to move every object under it you will notice that as we move this slider the nv will start moving along the path now we just have to do the same thing by code so that when the enemy details the player it stop moving and shoot at him and once the player got away it should start moving again so first create an export variable that will tell us whether this enemy is moving in loop or linear path after doing that this value can be set from the inspector for this enemy i want to be in a circular path so i will select loop here now when we detect the player we want to change its state to fight and when the player gets away we want it to win petrol state so in the process function when the state is petrol we should do the petrolling and also pass the delta value as well now inside the petrol function we first check whether the petrol type is equal to loop or not we will do the coding for linear path later but first let's create a variable that will contain the path follow node and now we just have to increment the offset by speed times delta if we see our game now it is working properly but when the player gets in the enemy's range the enemy rotates this disturbs the enemy rotation value even after the player get away to solve this we check if the rotation value is not equal to zero then we will make it zero i am going to use this love function so that the enemy rotate back smoothly the lerp is used to change the value of any variable smoothly the first argument is the starting value and the ending value and then the last is weight the lower the weight value the longer it will take to complete the higher the value the faster it will be and that's it we have completely created the loop petrol type now for the linear patrolling it is a little bit difficult first we will make a path using the pad to denote but this time we will not close it now add a pathfiller node to it as well and add the enemy as a child of it don't forget to set the enemy's petrol type to linear now what we are gonna do is increase the offset and when it reaches the end of it we will wait a little bit and then rotate our enemy and start reducing the offset value similarly when we reach the starting point again we will wait a little bit and rotate it again and this process goes on so to keep track of when our enemy is moving forward and when it is moving backward we will simply create a variable here one will mean forward and zero will mean backward so inside our petrol function when we get that it is a linear path we first check for direction and if we are moving forward then we just have to increase the offset but before doing that we need to check if we reaches the end or not in the path follow node we also have a property called unit offset it is just a percentage of how far we have traveled so 0 will mean starting point and 1 will mean the ending point so if the unit offset is 1 that means we have reached the end and if it is not then we keep increasing the offset value but if it is 1 then we will wait for 0.3 second by creating a timer with yield function this yield function will stop the execution of the current function until the timeout signal is received now when the timer will end we will rotate our enemy by 180 degrees and now i'm gonna create another timer so that the enemy will get enough time to rotate completely if it is not done then the enemy will start moving backward without completely rotated and finally we set the direction to zero indicating the backward movement of the enemy we will do the same thing in the backward movement also just instead of increasing the offset we decrease this time and we will check whether we reach starting point or not we will set the rotation back to 0 here and at last we will set the direction again to 1 and here you go now whenever you need to add an enemy just create a path and add a path following node and then the enemy and set the petrol type accordingly and you're all done i hope this video will help you also if you want me to make video on any game mechanics write down in the comment section and i will try to make it and don't forget to like this video and also hit the subscribe button and i'll meet you in the next video
Info
Channel: Dicode
Views: 9,550
Rating: undefined out of 5
Keywords: Godot, enemy patrol, patrolling in godot, 2D patrolling, patrol, tutorial
Id: 8UklQumjD2I
Channel Id: undefined
Length: 5min 1sec (301 seconds)
Published: Sun Jun 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.