Basic Pathfinding Tutorial in Godot 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys in this tutorial I'm going to be going over how to set up a navigation system in good 04 since the basic system for this has changed and there aren't really a ton of updated tutorials out there yet this is going to be a very stripped down basic system for how navigation works so first thing we're going to do in our world scene up here we're going to add a new node to our base scene up here and this will be the navigation region and with this node selected go on to the right hand side here and in the navigation polygon property we're going to make a new navigation polygon and with that selected we're going to add some points I'm just going to kind of create a shape here and once you finish the loop you will have a mesh that the navigating entity can stand within this space next up we have to actually create the Navigator so we're going to make a new scene at the top we're going to select another node and we're going to select a character body 2D and we're going to rename this to enemy since we will have some enemies that chase a player for this example and with the Enemy selected we need to actually create a collision shape so we're going to search for Collision shape 2D add that to the scene and on the right hand side we're going to add a new shape into the shaped property and this will be a circle shape 2D if we zoom in a bit here we're just going to expand this a bit and we're going to add some visuals here so we're going to add a new node this will be a Sprite with the Sprite selected we're going to go and load in the icon which comes in every project and I'm going to scale this down quite a bit just hold the shift key so that it is symmetrical along the x and y axis and then in the transform property we're going to reset this to zero zero and we'll have this in the center and then we can actually just drag the Collision shape in front of the Sprite so we can kind of see how big this is and we're going to make it just a bit bigger to match the size of the Sprite okay and then next up we need to add a navigate Asian components and this will be called a navigation agent so we're going to search for a navigation agent and it will be the two-dimensional one we're going to add that to the scene and this is all we'll need for the base scene set up here so we can go ahead and press Ctrl s to save and I'm going to put this in an enemy folder I created and just save it here now I do want to test the scale of everything so I'm going to go back to my world scene we're going to go up here to the world node and add a new enemy instance here just drag it over here and you can see that I guess the scale works for now we can just keep that as is but for the navigation region I'm just going to grab these points and maybe keep them a bit smaller within the bounds of the viewports and we're going to move this a bit and then we're going to move the enemy onto the mesh so that it starts on the navigation shape now back in the enemy scene we're going to go up at the top and we're going to add a new script to this node so you can go ahead and click that the default should be fine save it in the enemy folder and it will open it up here I'm just going to zoom in a bit so you guys can see what's going on but right at the top we're going to add a new export variable we can write export VAR and this will be the movement underscore speed and this will be a float and we'll set it equal to a default of 200.0 next up we want to assign a Target point that we will move to and a navigation agent resource that we can call all of our navigation Logic on now what we're going to do for the Target is we're going to set up an export variable and this will be called the movement underscore targets and this will be a node 2D and then under here we're going to say at export VAR this will be the navigation agent and this one will be a navigation agent 2D node so how this is going to work is for the movement Target we're going to assign it to a point in our world um temporarily for this example okay so if we go into our world scene up here and we add a new child node we're going to search for marker and this is basically just a node that you can visualize its position easier so we're going to add this and we're just gonna drag it to any spot inside of this navigation mesh and then if we go ahead and click on our enemy here on the right hand side in the properties tab we can see that our movement Target can be assigned and we're going to select the marker for that Target and then in our script we're going to use that targets position for the location that we want to move towards or navigate to and I'm also going to rename this to nav Target and then one more thing we need to set up in the enemy scene here is select the root of the scene which is the enemy and we're gonna assign the navigation agent to the navigation agent inside of the scene and then Ctrl s to save and then back in in the script we need to write function underscore ready and we're just going to do a couple things on the ready function the first one will be navigation agent dot path desired distance we're going to set this equal to 4.0 and the next one is going to be navigation agent dot Target desired distance is going to also be set to 4.0 and these are some defaults that I took straight from the Godot documentation these would obviously need to be changed depending on your specific project but you can kind of play around with these values the desired distance here as you can see in the documentation is just a threshold before a path point is considered to be reached and this typically just prevents the movement script from overshooting and jittering around the desired like reached point but you can play around with those values if you are experiencing any jittering after that though we need to say call deferred and we're going to call a method inside of here just a second but we'll leave it empty for now since we actually need to set up this method so right down here we're going to say function actor setup and inside of here we're just going to pass for now so that we can call it in the call deferred method and we're going to pass in actor setup inside of this function that we just created first thing we need to do is await and we'll say get tree dot physics frame this will basically mean that on the first frame of our game if we try to get anything for from our navigation server it will return empty since everything needs to be synced to the physics frame so we just have to wait one frame so that everything syncs to the physics correctly and then after that happens down here we can call a new function which will be set movement targets and for the Target we're just going to pass in our movement Target variable up here which if you remember is the node 2D or the point for the navigation targets inside of our world that we want to move to so we're going to copy movement Target and put it inside of here and then also this method isn't defined yet so we need to copy this name and we're going to Define it down here so we can write function paste set movement targets and we're going to require one argument for here which will be movement targets and it will be a vector two and then since we're actually requiring a vector 2 here we also need to go up into where we're calling this function and say dot position here so that we are retrieving the position of this movement targets and then back down here in our function all we have to do is say navigation agent dot Target position and we'll set this equal to our movement targets and I actually just realized that these are named like the same thing so I'm going to do this quick for the movement Target inside of our method here we'll call it Target point and then just replace the assignment here with the target points argument there and then we won't have any uh confusion with the same name as this variable up here so next up we'll create one more block of code down here and this will be inside of the physics process so we can type function underscore physics process and then inside of here we'll say first off if navigation agent dot is navigation finish so if our navigation is finished we will just return otherwise we will say VAR current agent position which is a vector 2 is set to the global position of this node next up we'll say VAR uh next PATH position which is also a vector 2 will be set to navigation agent dot get next PATH position and then down here we'll write new velocity which will be a vector 2 and this will be set equal to next PATH position minus current agent position and then we will say new velocity equals new velocity dot normalized so that we can get the normalized vector which we will then multiply by our move speed with new velocity equals new velocity times movement speed and then lastly down here we'll just say our velocity which is the built-in variable inside of good o4 will be set to our new velocity and then we just need to call move and slide to actually initiate the motion of our node and the script should all work correctly now there's a couple more things I want to set up quick first one is if we go into the debug settings at the top here we can just enable visible navigation and this will show our navigation mesh or our navigation shape during run time and then also if we go into our enemy inside of the navigation agent under debug we can say enabled and then this will draw a line showing our entire navigation path which is also kind of helpful for debugging so now if we go ahead and run the game oh we need to select the world scene for the main scene here just so that you guys can see what's going on here zoom in here and you can see that the enemy followed the path all the way to the specific point which is defined by the position of the Navigation target here and it's also good to note that you do not want to be updating the navigation path every frame for the processing because that will slow down your game so ideally you'd only want to update the navigation path at specific times but you do have to be careful since a lot of functions you can call on the navigation agent will update the path to recalculate that frame so you do have to kind of keep that in mind and remember to optimize all your codes so that you are only updating the path at certain times and then you are simply executing the movement code something like this when it's not updating obviously but that's that's about it for this tutorial I know it's been a while since I've posted a video but life has just been very busy so sorry about that but I'll try to be a bit more consistent instant here in the future but either way thanks for watching and I hope you learned something new from this tutorial if you did enjoy the video or you ended up learning something make sure to leave a like and consider subscribing but with that being said thanks for watching again and have a great day
Info
Channel: Queble
Views: 20,319
Rating: undefined out of 5
Keywords: godot, tutorial, game, engine, howto, video, programming, coding, how to, game development, game design, video game, learn, efficient, description, stats, 4.0, new, logic, objects, pathfinding, enemy, chase
Id: M0bod16k5Eg
Channel Id: undefined
Length: 11min 36sec (696 seconds)
Published: Sat May 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.