How to Use the New Navigation System in Godot: Enemy Pathfinding

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you'll learn to make an ai follow the player around the level using godot 3.5 new navigation system there was such an algorithm in godot 3.4 but the new one is faster more flexible and a lot more powerful in this video we'll see the basic navigation and in the next one we'll see how to use the new real-time obstacle avoidance feature you can see in action right here with the two black holes this video is sponsored by our good courses we have courses for beginners and experienced developers more on that at the end of the video this project is free and open source you can find all the demos and videos we made about new features in google in the description below let's first talk about what is new with the navigation in good old 3.5 and good o4 you've seen the ability to avoid obstacles in real time the previous system did not allow for that this is simple avoidance but it's much welcome and it has a low performance impact talking of performance this new system is supposedly designed to support multi-threading and offer greater performance than the previous one i haven't benchmarked it so you'll have to test for yourself another thing is the flexibility and usability improvements it brings you you can see right here i have some gaps in the area my ai here can navigate when i run the game though it can cross the gaps no problem the previous system needed you to perfectly match edges of navigation polygons and it was a real pain to use so this is a major ux improvement now you can also recalculate bake or stream these navigation polygons at runtime and you can move them and do lots of things there's a lot more flexibility speaking of which you now have access to the navigation server a new api that gives you fairly low level access to the navigation system and allows you as you can see for example the region bake nav mesh function to rebake the navigation meshes in real time so i'll let you look into that it's for more advanced uses than most of us have but it's still a welcome addition for larger teams also the way it works is a bit different from before but the code is fairly simple once you've learned it and you're going to do that in this video you still use a navigation 2d node and then you place these new navigation agent nodes in your hierarchy and they will automatically detect the navigation 2d and work and update the navigation calculations for obstacles you use these navigation obstacle 2d nodes which we will see in the next video for now let's look at how to create a scene where the enemy follows the player around the level in the goodu project linked below the video you will find a scene you can use to quickly follow along you can press ctrl shift o to open the quick open scene and search for tutorial to find the navigation server to the tutorial start scene this one contains a background that tile maps and collisions to get us started quickly an enemy that's a kinematic body with a sprite collision shape and a timer will use to recalculate the path to the player and a player that can move around the level if you press f6 to play the scene you can move the player the enemy has a script attached to save some steps but it's empty with that let's start setting up navigation as with the previous system we need to start by creating a navigation to the node so select the root node of the scene and search for navigation 2d i'm going to create that one it's the parent node for the navigation system it allows godot to find paths between agents and targets now to work this needs a couple of polygons that define where the agents the ais can move so to define that you add a new node as a child called a navigation polygon instance actually you can create several of those which we're going to do because with one polygon it can be a bit difficult to cover all the gaps in our level so first to create your polygon you click the empty slot next to nav poly in the inspector and create the navigation polygon resource this then allows you to click to create polygons like this now we're going to make this as clean as we can so we're going to select the node and click to create a point that avoids the borders of the level and the reason for that is that our enemy has a large collision shape so we don't want it to bump into the edges too much you know we we want the pathfinding to avoid these curves around there so we put some margin between the levels edges and the enemy because all the bright blue you can see here is going to block the enemy and the player now you can see here i'm making one polygon and i'm going to make another one for the bottom area and a couple more to bridge these areas so i'm going to skip ahead i went ahead and created three more navigation polygon nodes the same way as the first one and created shapes to cover the map now you're going to see some gaps and some overlap between the shapes in there and with this new system we have a setting to automatically connect the shape at runtime um if the gap is small enough to control that you can select the navigation 2d node and you will see the edge connection margin in the inspector this is the distance allowed between edges of different navigation polys that the ai's can cross in pixels so we can use something a bit large like 10 pixels just to make sure that our ai here the enemy will be able to navigate through the entire level like this the next step now is to add an agent a new kind of node that will allow the enemy to navigate this navigation to the area select the enemy ship or your ai in your game and add a new navigation agent to the node as a child of it this new node has a couple of properties you can use to control how far the ai can stray away from the ideal path mostly with the path maximum distance when the ai collides with elements in the level it will necessarily be pushed away from the the calculated path on the navigation polygon so you can increase this value to give the ai a bit more leeway now the next thing is the navigation agent by default will not automatically find this navigation node the easiest setup we can create is placing the enemy as a child of the navigation node there are other functions that you can use if you look for the navigation server that allow you to connect an agent with some navigation but the simplest setup is this one you place the enemies as children of the navigation node and the navigation agent will automatically find the navigation to the parent with that we have everything we can to code the path finding and make the enemy ship follow the player so let's get scripting the first thing that we need to do is to get a path to the player node to do that in a flexible way we're going to export a new variable called path to player and we're going to store a notepad in this this allows us to get a path to a given node but also to change it at runtime or from a script and something like this so you can select the enemy ship node and click the assign button next to path to player to get the player node at this stage well to get the path to the player node then we still need to get the node and to do that we create an already variable i'm going to call it player and we're going to call get node the getnode function can get the name of hl node or something like that but it also supports notepad values stored in variables so this allows you to create multiple enemies that might change target or that have different targets those kinds of things next we need a reference to the navigation agent 2d so in good 3.5 you can control click and drag on the node to automatically create an already variable for it and i'm going to call that variable agent we're going to use it to calculate the path to the player now we're going to start in the ready function by calling agent dot set target location and that location will be the player's global position this is the function you call to update the path finding and have to recalculate the path to the player then to update the internal state of the ai agent in a processing function like physics process so every frame you need to call agent agent.getnextlocation right it's going to calculate the next place the agent wants to move to and the thing i like to do is to use steering behavior to smooth out the ai agent's motion so what i'll typically do is calculate a direction to the next location from that so you can say i'm going to take the current global position of my enemy and calculate the direction to the agent's next desired move location it's the next location on the path it found to the player to the target from this we can define a new variable that we use to keep track of our enemy ship's velocity and increase or decrease it as it moves so i'm going to define a new velocity variable it's going to start at vector 2.0 and we have a steering equation that we've covered before that goes like this we calculate the desired velocity it's the direction we just calculated multiplied by a maximum speed so we can say for example 500 pixels per second and then we calculate a steering vector it's the difference between the current and desired velocity and we add a portion of that to the current velocity this makes the ship accelerate and decelerate as it moves closer and farther away from the target and gives it a smooth motion and then because this enemy is a kinematic body 2d node we can call move and slide and pass that the velocity and you can also assign the resulting value to the velocity variable and if you do that your enemy is now going to move to the player it's going to bump into a wall and then it's going to oscillate because once it reached the target right now it's it's trying to keep going towards the player to avoid that we can add an extra line at the start of physics process we can say if the agent has finished navigation we can check that with the is navigation finished function then we'll return from this and if you still have the navigation agent selected you can control uh how far from the player it considers that it finished navigation with the target desired distance property so you can say from 15 pixels away from the player i consider that i reached the target and so now if we do that the enemy is going to move to the player and it's going to stop after one oscillation right now the thing is we calculated the pass to the player once but then the enemy stops and it stops following the player around so how do we make it keep following the player the simplest way is using our timer here it has a small wait time and it's going to cycle and emit its timeout signal periodically and every time it emits the signal we will recalculate the path to the player i'm going to select the timer node control click and drag onto my script to create a new unready variable and in my ready function i'm going to connect the timer so we can say timer dot connect we want to connect to the timeout signal and we're going to define a new function that we'll call something like update pathfinding like this that will create at the bottom of our script a very simple function um [Music] that is going to call set target location on the agent so we can actually copy this line from the ready function right there and we can replace the line in our ready function with update pathfinding because i set the wait time to 0.1 seconds this is going to get called 10 times per second updating the patch of the player 10 times per second right and now if we move the player you will see the ai changes path and thanks to the steering behavior it does that fairly smoothly one more thing that you can add is changing the angle of the sprite of our enemy so i'm going there again to create an already variable for the sprite and in my physics process function at the end i'm going to write a sprite dot rotation is equal to velocity dot angle this is a function that calculates the angle of a given vector and now the enemy is going to rotate towards where it's moving you can see a couple of small hiccups and the code we are using here is functional but it's also pretty simple and so sometimes when the enemy recalculates the path it's going to stop for a second and this is something that you want to smooth out in your code if you're having the problem you can smooth out the steering equation by giving the enemy some more drag or you could also modify the pace at which you update the path from the enemy to the player right now it's every a couple of frames but you could lower that at a higher performance cost using the timer you can balance between performance and the ai's behavior anyway with that you know how to set up ai pi finding with the new navigation system in google 3.5 and in the next video we'll see how to use the new obstacle avoidance feature to go one step further this video and open source demo is sponsored by our courses if you're a beginner you will love learn to code from xero with godot it's a complete course to get started with game development with tons of lessons and cool interactive practices if you're more experienced then go to node essentials is for you it's the biggest knowledge base about all the things you can do with godo's nodes
Info
Channel: GDQuest
Views: 39,781
Rating: undefined out of 5
Keywords: godot navigation, godot pathfinding, godot 3.5, godot tutorial, godot 3.5 navigation, godot 3.5 navigation2d, godot 3.5 pathfinding, godot ai pathfinding
Id: aW4Oa-4dyXA
Channel Id: undefined
Length: 16min 10sec (970 seconds)
Published: Fri Sep 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.