Godot 2d Pathfinding in Mins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in today's video i'm going to be showing you how to set up pathfinding for a 2d game so without further ado let's jump right into this video [Music] now i did recently do a video on how to do this for a 3d game and if you want to check that video out i'll leave a link somewhere on screen but to do this in 2d we want to actually start by adding a level so in this case i just added a 2d scene which is a note 2d and i renamed it to level 1. as a child of the level one i'm adding a navigation to the node and as a child of the navigation 2d node i'm going to go ahead and add a tile map then with the tile map still selected i want to go over to the inspector and then i'm going to use tileset empty new tileset tileset once more and i can actually add a tileset now by clicking the plus sign here or i can simply click and drag a tileset into the area as shown there anyway i'm going to be using my square.png as my tile and i'm going to be using a new single tile and then i'm just simply defining the region for my tile now in the inspector you want to make sure that you actually set the snap options for the step to be the size of your tile so in this case my tile has a size of 32 by 32 so you want to set the step to match that anyway since i want to use this tile for navigation i want to make sure that i don't actually define a collision for it all i want to define is a navigation shape so go ahead and define the shape for your navigation for this tile and you want to make sure that actually covers the entire tile and now we can actually click and draw our level but as you can see it's actually the wrong size so to fix this we want to go over to the inspector under the saw options we want to set the size here to match the size of our tile so in this case my tile has a size of 32 by 32 so that's why i set it to a size of 32 by 32 for the cell and now i can actually draw my level without having any issues now the areas where we're drawing the tile with the navigation are the areas that the enemy will actually be able to move and walk on so just go ahead and create a level now and when you do you're pretty much set so i actually went ahead and created a level beforehand as you can see in this case it's just a normal node which i renamed to world and as you can see it also just has a navigation 2d as a child and which also contains a tile map as a child of it in this case the tile map which the first tile is the blue is my navigation tile which is like i said gonna be the area my enemy will be able to walk on the turquoise colored tile is my walls which actually have collision and then the third tile which is the dark blue tile that's going around my walls is a tile without collision or navigation and the reason i did that so that my enemies don't actually get stuck or too close to the walls anyway that's actually make our enemies now so i actually went ahead and created my enemy beforehand as well and it's just a kinematic body 2d with a sprite which is the same square.png texture as earlier and then i just set the self modulate to a red color as you can see here when i hide the collision shape and then for the collision shape it's just a rectangle shape and i basically just resized it by using the handles on the viewport as you see here anyway that's pretty much the setup for my enemy now let's actually add a script to it so that we can actually have it make use of the navigation so go ahead and add a script in this case i'm just calling it enemy saved in a folder called enemy and in this script we want to start by defining some variables in this case i'm going to define an export var for the speed and in this case since it's 2d and not 3d we're working with pixels so we're going to set the speed to 100 and then var velocity is going to be equal to vector 2.0 and then it's going to be var path is equal to an empty array and then var threshold is going to be equal to 16 and then var nav is equal to null for now so it's basically going to be the same code we use for my 3d example of pathfinding but i do have to adjust it to work with 2d but it's going to be pretty similar and we're actually going to get the neff uh in a different way from my 3d example so in this case we're going to get it by doing funk ready and inside this function we want to do yield owner and we're waiting for the owner to be ready and when the owner is ready we're just going to do nav is equal to owner.nav in this case the owner is going to be the world anyway after that we want to do func physics process delta and inside this function we want to do if path dot size is greater than 0 we want to do move to target which is a function we're gonna make right now so funk move to target is simply gonna do if global position dot distance to path and then square bracket 0 so the first index of my path array is less than the threshold i want to do path.remove index 0. else i'm going to do var direction is equal to global position which is my current position that direction two path square brackets zero so the direction two basically just returns a normalized vector pointing to uh pointing from this vector to b and it basically just gets the direction to the to the point that we define in this case it's going to be the path at index 0. anyway underneath that line of code we want to do velocity is equal to direction times speed and then velocity is equal to move and slide velocity and we're doing that so that our enemy actually moves anyway we actually want to get our path as well so we're going to define a function for that so funk get target path and it's going to take a target position or target pus as a parameter so inside this function we're going to do path is equal to nav dot get simple path and for the get simple path we want to do global position and then our target pus and then we're gonna set false so if we actually search for get simple path as you can see it basically just returns the path between two given points which is my start point and my endpoint and then optimize is by default set to true i'm sending it to false because i noticed that if it's set to true it kind of gets stuck on the wall more so i'm just setting it to false so that it basically you know kind of fixes that issue a little bit anyway i'm actually going to add my enemies to my world scene here and i actually have a node that i made beforehand to act as a container for my enemies so if i run the scene now it will actually crash the game and the reason behind that is because my owner.neph is actually not defined yet so like i said the owner in this case is the world so we are not to fix that issue we want to actually add a script to my world so i just called it world.gd and then all we want to do here is we want to get a reference to the player and to the navigation 2d so on ready bar player sequel to dollar sign player and then on ready var nav is equal to the navigation 2d as you can see if we run the game it won't actually crash now but the enemies aren't moving and that's because we're actually still not calling the get target path function in our enemy screen so we need to actually call this function somewhere and in this case i'm actually going to call it by using a group so for my enemy i'm going to go over to node groups and add it to an enemy group as shown here now over in my world i want to go ahead and actually start by adding a timer and this timer is going to be what's going to actually call that function on our enemy so we're actually going to go to the inspector with the timer still selected and we want to make sure that it's set to auto start and then the white time of 1 is fine and then we want to go over to node signals and then connect a timeout signal to the world and inside this function is where we want to call the get target path function on our enemy so we want to do get tree dot call group which and we're calling the enemy group what uh method we're calling so we're gonna call the get target path method on our enemy and then we're passing the player.global position as the parameter to the get target path method on our enemy and as you can see the enemies are actually now chasing the player and moving towards the player so everything seems to actually work properly and if i move around as you can see they will continue to actually follow my player around like i said the dark blue uh tiles are there just so that the enemies don't get too close to walls therefore getting stuck on the walls you can do it in a different way if you don't want or you could honestly probably disable the collisions as well and work with the collision layers if you want i just did it this way as a quick fix to that anyway i'll be leaving a link to the github project in the description if you liked the video make sure you leave a like and consider subscribing as we're on our way to 1000 subs also if you're interested in online multiplayer using gold.then consider following my kickstarter project which would be a course on online multiplayer using go dots so if that's something you're interested in then definitely go check it out as whether i make the course or not depends on how much interest i see people have in set course anyway i'll see you guys in the next one
Info
Channel: rayuse rp
Views: 2,725
Rating: undefined out of 5
Keywords: Godot, Godot tutorial, gamedev, How to make games, godot 2d, godot 3d, godot 4, blender, 3d modeling, hololive, channel update, godot multiplayer, rayuse rp, devlog, how to make 3d game in godot, godot dedicated server, indie games, intro to 3d in godot, godot 3d games, 3d movement in godot, 3rd person controller godot, 3d person controller, godot 3.3.1, godot 3.3.3, godot 3.3.2, Pathfinding in godot, Godot 2d Pathfinding in Mins, top-down controller godot
Id: 5xoqp9QMKeg
Channel Id: undefined
Length: 9min 34sec (574 seconds)
Published: Thu Jul 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.