Pathfinding on a TileMap with Navigation2D (Godot 3.1 tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to look at navigation 2d in combination with a tile map a tool that you can use to find the path between two points and find the shortest path as well so this allows you to make characters walk and avoid obstacles automatically for you we're going to talk about how to make it work of a tile map and about the system's limitation now this is a collaboration with Chris Redfield from KITT scan code you can go to his channel to watch the second part where he'll show you how to work with isometric Maps and hex base maps and how to avoid the most common issues you can have creating navigation polygons you can find the start project on our github repository starts a quest 3 you can get the January 19 navigation to the entire map copy the path to the start folder and then when you open go to import that folder to get to the same starting point it has the character the map we're going to navigate and the node tree prepared for you so first let's look at the node tree I have a simple note demo to start the game to initialize some code then we have a navigation 2d nodes and a tile map as a child of it so navigation 2d is the node that finds paths between points and it does that using a polygon using a collision mesh and the tile map is going to provide that mesh we're going to look at that in a moment but if you want to create new navigation to the node you press ctrl a and you search for navigation 2d can find it here now you can manually create the polygon the character will navigate using a navigation polygon instance but you can also create that navigation data inside of your tile set the element that you used to draw the map as I've drawn on screen and then the character is a sprite and we have a line to denote to draw the line visually on the screen to draw the path that actor will walk so let's open the assets folder tal sets and double-click on grasslands dirty rez this will open the new tileset editor in guru 3.1 going to expand it using the button at the bottom right of the bottom docker and click on grasslands to open beetle set can press alt anytime to toggle the name of details which makes it easier to spot them we need for each of the tools that we create for example I'm going to get the Grice tile in the top left to go to the navigation section of the editor and then you will see that I have a polygon that I've created so I'm going to delete the polygon on the grass by clicking the trash bin icon and create a new one to do that you left click the polygon icon and you click to add new points and you want to click on the starting point to close the shape and create the polygon now on trial sets it's very important that these polygons take the entire towel or that these snap perfectly that the paths connect perfectly between cells if you were to create a bridge with some gap inside the town you absolutely want the bridge to be connected to some edge of the town the top and bottom on the left and right edges but we're going to create polygons that take the entire town and to do that we're going to use snapping so click on the grid snap icon and we're going to set up the grid so that we can easily create our polygon so these styles this town map uses 80 pixels by 80 pixels sized cells so I'm going to go to the inspector and expand the snap options you can see that the step is 32 by 32 pixels we want 80 by 80 so now the snapping grid will align with my tiles and so I can click to create a new polygon make sure that snapping is on so this is the blue icon here I'm going to click on all four corners of my car so let's click left click left click you should see the points appear and I finish by clicking the top left vertex and this closes the shape doing this means that the character can walk on the entire cell can go through it and there are other cells which don't have a navigation polygon this one the plant the character cannot go through it now the tricky part bound bat is at the moment you have to go through each and every tile and add the data to it manually so you have to create a navigation polygon on each tile in your tile set which can be a little cumbersome but this allows the navigation 2d to find the path between two elements so with that done you save ctrl s and I'm going to fold little set editor by clicking on the bottom panel we can now get started with the code and I went ahead and create two GD script files that attach to the demo nodes and the character node to save us a tiny bit of time we're going to work on demo first because it's the one that will request the path to navigation to D let's click on the script icon next to it and I'll expand the script editor with ctrl shift F in the top three lines of codes access the three nodes that we want to use in that script so navigation to D line to D that's what I'm going to use to draw to the screen and the character we're going to give the path we asked to NAV to d2 we are using the unhandled inputs method to listen to the players input so we're going to check when we left click somewhere and ask a path to where we clicked to nav to D an end all input is just like the input method that you've seen if you went through the getting started guide in Godot the difference is it's often recommended to use that one for the player input because good will first call input and then we'll forward the input events to the user interface and you only want the player to move if the inputs is not interrupted by the interface so you want to use an handle input to get that now we are going to check if the player pressed the left mouse button the way I like to do that is to do it subtractive Li so if the player did not click the mouse button I'm going to return from the function let me show you how this works so I'm going to use a negative condition not event is inputs mouse button I'm going to return from the function this is a very common way to work in Python and you put that at the top of the method this helps to say if a certain condition is not met we don't want to run that function now we have to check that this event is the left click and that while clicking down it's the very moment while pressing the button so we're going to do it the same way negatively if the events button index is not Mouse left or button left this is a constant provided by Godot or if the event is not pressed we return as well and if we go past these two conditions here it means that we left clicked on the canvas somewhere so we have an input event mouse button an input event mouse button gives you access to we have to go back up to input event Mouse here to position and global position the place where the player clicked so we're going to get our path from that we're going to store it in a new variable call it new path and I'm going to use type hints here that's what : equal does it tells good to please assign type static type to this variable based on what I put after the equal sign we're going to ask nav 2d for the path and navigation 2d has a get simple path method that we can ask here so the starting point is going to be the character's position all the characters global position I recommend using global position in those cases because if you move the character and different nodes and they started a different position than the levels origin then the code will not work anymore but with global position it will the endpoint is the advanced global position and that's all we need this is going to give us a path and the path will have the type four vector two array now we want to do two things first we're going to get our line 2d and line 2d take some points which is a poor vector to array as well we're going to assign it the new path and finally we're going to do something similar to the character we're going to create or we have a path variable that I've created for us that property and we're going to assign it the new path and this is all that the input code will do then it's up to the character to move along that path all right so now let me go back to the scene tree control shift F in and open the character script because we need to handle the movement along the path on the character there again there is some template code get us started the character will have a movement speed and that path property that we used in the demo script it's of type pull vector to array because this is what the path will be an array of vector to an array of positions if you want and it's using a setter a method called set path and we're all going to use it to set up holo path we want to do two things we're going to use the process method that gets run on every frame to move the character along the path but at the start because there is no path to move along we set process to false and when we update the path we want to set it back to true so that the method runs and the character will move along the path and we'll use the move along path method to do that so that starts at the bottom with set bath we're going to do a little check first we want to be sure that the value that we get so the the path that we get is always a path that has some places where the character should go so if value sighs remember it's like an array that path so it has a size method that returns the number of points on the path if it's equal to zero which it is at the start of the the game right we want to return from that function we will however assign a value to the path variable you always want to do that in a setter method and then if the size is not equal to zero we are going to set process to trip the reason we have this condition is because setter methods get called at the very start of the game before the nodes get added to the same tree when you initialize the class if you want if you when you create an instance all the setters get called they also get called when from demo you say character dot path equals new path Godot will actually call that setter it will not directly assign the value to the path it will call set path with the value that we pass and this allows us to set process to true when we update the path if there is a new path now in the process method it's going to be fairly simple we're going to calculate the distance the character should move this frame let's create a new move distance variable and we're going to use the type hint again and we're going to multiply speed times Delta so the character moves 400 pixels per second we have the data value to represent the seconds the time between the last frame and this one this is the move distance and then we move that distance along the path so we pass move distance to the move along path method and with that we're going to focus on the biggest method in this tutorial move along path so this one is going to be a little trickier because we have to go through all the points in our path array one after the other and move the character towards that point so we're going to start by storing the character's current position its previous position if you want so it's going to be the last point or the starting point and we're going to assign the character's position to that then we are going to loop over our path for ion index in and we're going to use the arrange method to create lists of indices and pass it the paths I so we're going to look through all of the points in the array and the first thing we are going to do is calculate the distance to the next point so distance to the next point it's going to be equal to the starting points dot distance to the starting points is a vector two so it has this distance to method which we can use to get the distance to the first point in the path move along path takes a distance this is the distance that the character will walk so want to check if the distance is lower than the distance from the characters position to the next point on the path we're going to move as far as we can along the line that connects the two points the start and the end so if distance is lower equal to the distance to next and we want to make sure that there is some distance left to walk you'll see why in a moment so if it's greater than zero we're going to change the characters position so position is going to be called a starting point that we're going to use linear interpolate and linear interpolate from vector two allows us to find a position between two vectors two positions very conveniently so we give it the point we want to go to in this case this is path zero so the first points in the path are right and the ratio is going to be distance divided by distance to the next point and this is going to give us the position the character has to move to and if we meet that condition we break out of the loop we don't want to move any further we stop there now if that distance variable is greater than the distance to the next point the character will move past that point and sometimes in the path the points can be very close so the character might move past one or two points in one frame so then if you don't meet that condition you want to subtract the distance to the next point then we're going to update the starting point we're going to set it to the next point in the path and we're going to remove that from the path path dot remove index zero and this will allow us to move past points to move one two three points on the path based on the distance the character has to walk this frame and finally we want to make sure that when we have reached the distance the character should walk we're going to break from the loop so if the distance is lower than zero we're going to assign the next points to the characters position set process to false and break from the loop and with that you can try the game click anywhere and the character will move to the place you click on the map Thank You kami for watching I invite you to check the second part in the video description you should have a link at the top to Chris's part because he's going to show you how to work with isometric trial maps and Hextall maps that said be creative have fun and let's see one another in the next one bye bye
Info
Channel: GDQuest
Views: 71,295
Rating: undefined out of 5
Keywords: godot navigation2d, godot pathfinding, godot navigation 2d, godot navigation, godot navmesh 2d, godot navigation polygon, godot pathfinding 2d, godot pathfinding tutorial, godot game engine, godot tutorial, game development, godot beginner tutorial, godot 2d beginner tutorial, godot engine for beginners
Id: 0fPOt0Jw52s
Channel Id: undefined
Length: 17min 1sec (1021 seconds)
Published: Wed Jan 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.