Pathfinding with Navigation2D in Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thank you, this is very well explained! I'm looking forward to you next tutorial! :)

👍︎︎ 1 👤︎︎ u/golddotasksquestions 📅︎︎ Apr 11 2021 🗫︎ replies
Captions
so you want to make pathfinding in your godo game i am here to help you hey it's ives in this new mini series i am going to teach you the best finding done with the navigation property of tilemap tiles the tutorial was repeatedly requested as a logical sequel to my heartbeats crossover and my combat essentials series so here it is the art is by jenny will link to him is in the description there is also a github repository where you can download this project including the initial version with which i am going to work right now this being said let's dive straight into it so here we are in godot as usual first i'm gonna introduce you what we already have here's our level scene it has a tile map that contains two tiles floor and wall they are of size 16 by 16 and for the wall i have already set its collision we also have the player that has just movement and a set sprite with collision shape and the enemy that is basically of the same sprite but it's red has its collision shape and it just has some base for movement we also have our tile set right so first of all in order to make our enemy find path we somehow need to define where it can go and tileset can help us with that go to tileset this floor tile add navigation on it so what it basically means is that whenever you ask to create a path this very tile will be considered as one that you can traverse so the wall will not be traversed it has no navigation on it and furthermore it's a solid block and also in the level we need a certain node that analyzes all that and creates the path godo already has it it's called navigation to the node and you must put the tile map as a child rename it to level navigation just for the sake of convenience why do we need to make time up its child because this way the navigation node can access it and get what tiles are traversable okay we are going to create the path in the aim in order to do that we are going to access the level navigation but how we can access it thread multiple ways but i personally prefer adding it into groups so add the level navigation to level navigation group and just save it here and add the player to player group okay now let's make some basic template room here is our room let's add the player and the enemy to it now we can create the pathfinding algorithm itself go to enemy here first create a few variables a path array it will contain positions that we need to go through level navigation and the player well if these variables will store their reference to these two nodes we have just put into groups okay now we need to access these level navigation player in my opinion the best way to do that since we need the navigation from the very start is do it in the ready method here it is so we get the tree just not to write get tree all the time and here we check if the tree in other words the level has something that is in group level navigation and if it does it will return an array of these nodes and we'll pick the first one as you said it will have the reference however there might be times when one of the nodes is not ready maybe it's not instantiated yet in order to fix it add this line boom it says wait until you get three emits the signal idle frame in other words it will wait one frame now create two methods navigate that defines the next position to go to and generate paths and it's obvious what it does so first of all let's generate our path so we check whether the level navigation and player are not now and if we can work with them get simple path we generate the path what does this function mean well here is the function we are working with we give the start position the enemy's position the end destination the player and optimization as it says if optimization is true the path is smoothed by merging past segments where possible what it means that it makes it shorter whenever it's possible however it's not always work and you'll see why later for now set it to false now to the navigate method check whether we have some path at all and calculate the velocity based on the direction to the next point and multiply it by speed if we reach the next destination then delete this point from our path because obviously you don't need to go to the position you have already been to you just need to forward right okay as we have it now let's add it to physics process and it's quite easy actually check whether the player and level navigation exist and if they do generate the path and navigate it's not the best practice to generate paths every frame however our project is small and we are beginners here right so it's fine for now also before we start it will be quite useful if we had something that show us its path and we can add it go to enemy add line 2d to it and what it does is it makes a line that connects the dots in its points array at the reference to it and in the generate path sets its points to the path value so always update it and in the physics process set its global position to vector zero let's go to our level scene and see what we've got it's not working okay okay so for some reason in the level the style of the floor was covered with navigation but the scene itself wasn't so i set it up and it's working now as you can see it's drawing the line of the path and it's going for us right there are times when this enemy could just cut the path and go straight to the player and that's exactly what the path optimization does however there is another problem if you do something like that it just stops it tries to cut the path and sometimes something like that can happen usually you would like to have some circlish collision shape to prevent that or some clever system but that's not the way i want it to be so that's it and in this episode i taught you how to make basic pathfinding in godot with using its navigation in the next episode we are going to enhance the system and add a detection area for the enemy but for now thank you for watching i hope the video was helpful and you liked it and if you did don't forget to like share and subscribe it was live's and until next time you
Info
Channel: John Ivess
Views: 8,758
Rating: undefined out of 5
Keywords: godot, pathdinding, tutorial, programming, game dev
Id: gFlGMLmg8yg
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Sat Apr 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.