How To Create PATHFINDING in Godot 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I'm in go.4 and as you can see this path updates 10 times every single second to find the best possible path to get to our player no matter if we're behind obstacles or anything and it does not get stuck on walls it will slide across it but before we get started I just want to ask if you can go down below hit that subscribe button drop a like on this video to help you to push this to any aspiring game developers who are trying to learn to make their own Godot games and need path finding for those go.4 makes this pathfinding super super easy so let's just get started and let's create this pathfinding here and go.4 okay so to create pathfinding in godai so what we have in this game already we have a couple of starter items which we have a camera 2D just so we can zoom in close we have a player which has some basic movement and our player is just a Sprite and a collision shape and we have some very very very basic movements so if you want to copy this down for your player if you don't already have a player but for pathfinding you probably already have a player and you just need help on the pathfinding part so we have a simple player and then we have a tile map which as of now our tile map has no collisions our navigation 2D or anything and we need to go and we need to add navigation 2D to this tile map because that's going to be important to pathfinding for our pathfinding node that can help us you know move around so let's think about this we want Collision shape on all of these darker squares right these are all collisions so we're going to make this all with Collision 2D or with collisions and then on this light lighter gray which these are their own tiles I know they look like the background but they're not I'm actually going to add a color erect real quick just so we can kind of see the difference so we can see it behind as you can see these are their own tiles I it's just like if you see this right here this is my tile set it is a it's like it's kind of it's almost the same color as the background but it is its own top so if we go to our tile map this is the first thing we need to add navigation and we need to add Collision so to do this we need to go to our tile map over here in the inspector our physics layer we're going to add a physics layer and then we're going to go to our navigation layer and we're going to click add element and we're going to add a navigation layer now if we open up our window down here and we click on tile set we can go to you know whatever tile set you have for me it's just two tiles in here but we can go to paint select the property physics layer and we can just click on this now the darker Square does have a physics layer so it's going to have a collision and then if we go to our navigation layer and then we add navigation to just this one you don't want it on this on this one but you just want it on the ones that you want your enemy to be able to go on top of so like the ground this is like the wall this is like the ground right so we'll do that we'll save and then now if we play the game we should have collision and you have navigation 2D on the bottom but you cannot see that you know right now but it will show in just a minute when we have our enemy created okay so now for our enemies so we can have our path finding here in the game we actually currently have zero enemies so we're going to create a new scene we're going to do other node we're going to do a character body 2D we can name this as our enemy and then we're going to just keep a basic enemy just like our player a Sprite 2D we're also going to have a collision shape and we're also going to need some other stuff which We'll add here the second let's just make our enemy so we'll have this and then let's make it very small so I'm going to scale it to like point one that's the size of my player and then we can go to our visibility module it and then we can change this to like red simple red color just like so so our enemy is red and then for our Collision shape we can make a circle and we can make this pretty small right and then on our enemy up here you see our player is classified as a grounded that's the motion mode on our player but our enemy is gonna have to be different because it's gonna have to be able to move along the side of the walls because if you don't do this it's going to get stuck on the walls it's going to try and Pathfinder around but it's going to be stuck on the wall it won't be able to move so we need to go to a floating and we need to do wall minimum slide equal to zero degrees so we can just Glide against the walls smoothly right and then on our enemy we're also going to uh all we're also going to need some other stuff we're going to need a navigation agent this is what's going to do all the work in our navigation so we'll use a navigation agent 2D and then we're also going to need a cool uh a uh a timer node so we're all yeah we're also going to need a timer note and on this timer I'm going to go and we're going to set this to an auto start and then on our navigation agent we this is going to be like so so on the debug if you want to see the line you're going to turn this enabled on we you know we'll leave it on Just for testing and then we can turn it off at the end to see how it looks after but if we save this and then we're going we're going to obviously need a script so we'll create our enemy.gd script now a bunch of stuff is going to spawn or is going to like be already be in here I'm going to delete it all because we don't really need it and also I'm going to go over one more thing so just for this video just for the sake of this video If we go to R uh Collision we're going on our enemy so this is the character 2D we're gonna have to set the layer to two and the mass to one and two and then we're going to go to our player and we're going to set the layer to two in the mass to one and two as well so the two is going to be the enemy and the player and then the one is going to be our tile set so if we save now we have our script I'm also going to go to our timer node and we're going to set the timeout to our enemy function and we'll save that for a little bit later but what do we need in a player to make a player first of all we need our speed right so we'll do speed equals to like 20. whatever you know the speed obviously depends on however your game is set up we're also going to create and VAR or a variable for our player and we're going to have this as an export VAR and this is going to have to be our player or it's going to be name player and we'll just it has to be a node 2D right so now or it doesn't have to be a node 2D but that's what that's how you do it right so then we export so then we're gonna have another export bar export bar and this is going to be called our navigation agent and in our navigation agent we're going to have this equal to our you know on our scene tree we're gonna have to get our navigation agent 2D which is over here in our scene and then we can have this as navigation agent 2D right so now after those are all the variables we're going to need now it says something's messed up which won't return nodes in the scene tree okay yeah yeah this is my bad I put an export this has to be an on ready yeah so this has to be an on ready bar boom okay now air is fixed and then we're gonna have a couple functions or it's going to be like two functions so first of all we need our basic physics process function yeah our basic physics process function and then instead of this just being a Delta we need this to be a float as well because our our enemies uh our enemy is a is float the movement of our enemy is going to be classified as a float and then we can just send this to void and then in this function we're going to create a variable which is going to be our Direction and this is going to be so whatever our Direction is go our direction is going to be based off of our navigation agent so this navigation agent right here is going to do all the work it's going to find out the best path to go it's going to find out the best way to get to our player but we're gonna have to find which direction it's telling us to go if it's telling us to go right left up down you know where is it telling us to go so we'll say to our local so two local and then we'll do our nav agent which is our variable that we created up here and then we'll say get next PATH position so whatever position which is an already made function in Godot for the navigation agent and then we can just dot normalize this right and then we'll go and then this is for all the movement so we have velocity is going to equal the direction which we just got times our speed which is the constant at the top of the script and then that's all we need and then for our player or for our enemy to actually look like it's moving on the screen we need to call the move and slide function right and that is all that we need in the physics process so now we're going to have to be able to make the path right so if we go down and we make a new function we'll just name this function something like make path because we're going to have to make a new path to find out what's now the best way to get to our player right so we'll say we'll get our nav agent and then our we'll get our nav agent and then we'll get find the target position Target position equals our player.globalposition global position right so the next the target position where we're trying to get to is going to be equal to our player position currently we don't know what the player is so to do this we're going to go to our world and we are going to instance our enemy to our world scene and then we can move him anywhere but if we go to the inspector and we click on our enemy you can see this is the uh exported variable that we have we can assign this to anything this is going to be our player so we want to assign it to our player so now we have a player right so this node right here this is what it's tracking is our Target so if we go to our enemy back into the script our player is now equal to the whatever we put into that VAR on the export and then which we put in our player and then on every single time the timer times out we want to make uh we want to call our make path function right because every single time this timer times out which is going to time out every single one second which we can go faster we'll do like half a second so it will make A New Path every half a second so theoretically everything here should be complete and if we were to play the game everything should go smoothly but normally you know when you make games normally everything goes not as planned and you can see everything does go as planned so our enemy is a little bit slow but as you can see the path that is being created the enemy will go the fastest way to reach our player and you can kind of see how the path is super super delayed so first of all I want to go and I want to raise the speed of our player and then I want to go to our timer node we can go over here to our timer node and we can just like do point one so now it's going to update 10 times a second right so now Global position is equal to null I don't know did I just completely mess something up I don't know what just happened but okay so I reloaded the game uh I just x'd out a Godot reloaded it opened it back up changed the wait time of the timer back to 1.1 kind of fixed up the code I put this uh function the ready function here it's not I mean it's not not technically necessary I don't I mean that's just to call it just off the start so as soon as the game starts it's not necessary because the timer is going to go off point one seconds after the game starts but I just added that in there to make it look uh because it's a ready function right so you call it as soon as the game starts and then we have everything everything else in this entire script is the exact same player script everything's the same uh everything in the entire game is the exact same so if we play it you see it works and you can see how fast the uh the the thing updates right how fast the path updates every updates 10 times a second it's gonna find the pat fastest way to get to our in play right so this is path finding and Godot as you can see as soon as we get here as soon as we go up then it's gonna start going up now it's gonna start going down right whatever the fastest possible way to get to us is that's the way the enemy is going to take and as you can see if we tried to go like this way it does not get stuck on walls right it will slide against the wall that's why we had to make it into a wall slide of zero degrees and as you can see everything in the game works and it looks perfectly good we can change I mean we can just in or turn the path off and you'll see that now you have the enemy chasing the player but there is no path and it looks very very good so this is pathfinding and Godot you know I hope this helped you create pathfinding in your own go.game I hope it teached you a little bit something or teach you something about pathfinding in go.4 and if it did then please go down there hit that subscribe button and drop a like on this video so YouTube knows and pushes this video to more inspiring game developers so they can learn to make their own good at games as well because pathfinding is something that a ton of people have been asking me for and how do you do it in go.4 and this is kind of it's actually much much much much much easier than it was in the old Godot so I'm happy that they fixed it up and made it so easy but yeah so this is I mean this is Advanced pathfinding that it used to take forever to do in the old gutters now it's now it's super easy so I want to thank you guys all so much for watching if you need any help on anything and any go dot game that you have let me know in the comments down below I'll be sure to get back to you and hopefully help you out but stay safe and have a wonderful wonderful rest of your day [Music]
Info
Channel: DevWorm
Views: 45,791
Rating: undefined out of 5
Keywords: godot 4, godot, godot pathfinding, pathfinding in godot 4, how to create pathfinding in godot, enemy pathfinding in godot, simple pathfinding in godot, pathfinding 2d godot 4, godot 4 pathfinding for enemy, enemy pathfinding in godot 4, how to make pathfinding in godot 4, godot 4 pathfinding tutorial, pathfinding for godot 4, godot 4 tutorial, player pathfinding godot 4, enemy pathfinding godot 4
Id: Lt9YdQ6Ztm4
Channel Id: undefined
Length: 14min 43sec (883 seconds)
Published: Tue May 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.