Setting up 3d Pathfinding in Mins | Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in today's video i'm gonna be showing you how to set up pathfinding for a 3d game for our enemies here so that they can actually follow the player around without getting stuck so with that said let's jump right into this video so first things we actually need a test level to test this on so i actually went ahead and i already created a test level which is the same level that i used for another tutorial which was for my third person controller video which is the same controller we're going to be using for this video for the player and if you're interested on how to do that i'm going to be leaving a link somewhere on screen anyway we want to start off by actually adding a navigation note as a child level level in this case our level is called game it might be called something different for you anyway with the navigation selected we're gonna add a navigation mash instance as a child of it with the navigation mesh instance selected we're going to the inspector navmash empty new navigation mesh and then if we click on this once more it will actually show a bunch of parameters and options that we can adjust we're going to leave that for now so we're actually going to go over to the steam tab and we're going to select our platforms and walls which is what makes up my level and we're going to click and drag them into the navigation mesh instance as a child of it now we can actually bake the nav mesh and you will notice these this light blue that goes all around our level and essentially this defines the areas that our enemy can actually walk on and the areas that aren't light blue are the areas that our enemy can actually go get to essentially now with that we can actually adjust some of the parameters inside the inspector so we're mainly going to adjust the parameters under the agent drop down for example the height which is essentially the height of what's making use of the nav mesh in this case the height of our enemy so we're just going to leave it to 2 for now and then i know for a fact that our enemy is going to have a radius of 0.5 that's what i'm going to set it to and then i'm going to change the max climb to something else so in this case i'm going to change it to 0.2 because i actually messed around with this already earlier and i know that that actually works best for my level it might be different for your level so just adjust it to better fit your level and then i'm also changing the height under the cell to the lowest possible which is point one because again i already tested this out and those options work best for my level you might have to change them like i said anyway once you make your changes you want to make sure that you actually bake the nav mesh again to actually make those changes to the nav mesh and yeah that pretty much actually does it for that part now we can actually select our level in this case it's called the game and add a script to it so i'm just gonna make sure that it's an empty script and then and then i'm gonna save it in a folder called levels and in my case it's just gonna be called game.gd you can name it whatever you want though it doesn't really matter and then just click create to click to create the script and then all we want to do for now is we want to do on ready var player is equal to dollar sign player so we're basically just getting the reference to the player so that the enemy actually can target the player anyway i don't think the player has to be a child of the navigation but the enemy will so let's actually set up that enemy and we're going to start by creating a new scene and adding a kinematic body that we're renaming to enemy then we're going to add a collision shape as a child of it and we're also going to add a mash instance as a child of it and i'm just going to move the mesh instance above the collision shape and then with it selected i'm going to the inspector mash new capsule mesh and then clicking on the mesh once more and specifying a radius of 0.5 and a mid height of 2. then in the actual viewport i'm just gonna rotate it along the x-axis by 90 degrees to just make it stand up vertically anyway i'm just going to move it up into place so that it's basically flush with the floor and then i'm also going to give my collision shape a new shape in this case it's going to be a capsule shape also with a radius of 0.5 and a height of 2 as well so basically it's the same as my mesh instance and i'm also going to rotate along the x-axis by 90 degrees so that i can actually cover the mesh instance so just go ahead and move it up into place so that it covers the mesh instance like so and this looks pretty much all right with me so it does look like it's good to go now the thing is that some other people pointed out is that collisions can have some issues with the nav mesh so in order to get around this so that we don't get stuck in the world we can go to our project project settings scroll down and then find our 3d physics option here so that we can actually access the 3d physics layers and then we're going to define some of the layers here so layer 1 is going to be the player layer two is going to be the enemy layer and then layer three will be the world layer and we're actually just gonna disable the enemy collisions on the world so that way we don't get stuck on the world getting through uh basically essentially working around that issue where some people reported that it can get stuck on things still somehow so we're just gonna have the enemy collision be set to enemy there and then certainly gonna be checking for collision with player and other enemies and not the world that way we don't have any issues with getting stuck in the world whatsoever essentially that's why we're doing that anyway with that done we can actually save our enemy and a folder called enemy so go ahead and actually do that so i'm just gonna name it like so and that actually pretty much does it for our enemy setup and now we can actually create a script and actually before i do my script i almost forgot to we're actually going to add a timer as a child of the enemy as well then in the inspector i want to actually make sure that i set it up to be auto start like so and then the wait time is fine and the timers we're actually going to be using it to actually move the player so you'll see what i mean in a little bit anyway we're going to start off by defining variables in this case i'm going to start off by an export var of speed which is equal to five this defines how fast our enemy is going to move a var path which is going to be an empty array a var current path index is going to be set to 0 and a var target which is going to be the player but for now we're going to set to null and then a var velocity is equal to vector3.0 then with those variables defined i can actually start with my physics process function so inside this function i want to do size is greater than zero if this is the case then i want to do move to target which is a function that we're gonna make here in a little bit and actually let's make it right now so we don't get an error so funk move to target and then for now we're just going to do pass so basically in this line here we're just checking if our path array is not empty uh if it is empty it's actually going to cost some errors so that's what that's the reason we're doing this essentially we're checking if it's not empty and if it's not empty we actually want to move to the target and this prevents some errors essentially anyway in the move to target function all we want to do is if globaltransform.org dot distance to path current path index so basically if our current position and the distance to the point that we're trying to get to is less than 0.1 which i'm actually defining a variable for at the top called thresh fold and setting it to 0.1 and using that instead of the hard-coding the 0.1 directly into the if statement if this is the case then we're just going to do current path index and increase it by one so that we can actually get the next point along the nav mesh essentially else var direction is equal to path current path index minus the global transform dot origin so you might you want to make sure that you actually put the cur path index inside square brackets so that you can actually access that index of the path essentially since it is an array so there we're just basically getting the point in the path and subtracting the current position from it essentially and we're setting the velocity to the normalized direction which if you go into the documentation normalized tries to return a value of one essentially so velocity is equal to direction dot normalized times my speed so that we can actually have the enemy move the proper speed we wanted essentially anyway after that we're going to do the move and slide and then pass the velocity and vector 3. up which is the up direction essentially if you go into the documentation anyway with that uh there's one last thing that i want to do which is going to be a guard class and the guard clause is going to be in this statement so if current path index is greater than or equal to path dot size then we simply want to return so i was running into some issues where the current path index could actually be more than the path that size and they would cause the game to crash so basically we're doing this to guard a can guard against that essentially and not get that issue now with that i'm going to actually define another function called get target path and it's going to take a target position so the player position essentially and we're going to do path is sequel 2 and i'm actually going to set up a unready bar at the top for this so basically we want to get the navigation from our world and to do this yeah i'm just going to do on ready var nav and actually yeah we're gonna do unready bar and f is equal to get parent and get parent is going to get the navigation note from our world or level because it's the parent of the enemy so the enemy is actually gonna be a child of the navigation node on the world and we're using the get simple path here which basically returns a path between two points and it takes in a start value and an end value so we're just going to worry about the start and end so it's going to be path is equal to nav dot get simple path and the start value is going to be our current position so global transform.origin and our end path is going to be the target position so the player position essentially and then we just want to do curve path index is equal to 0 right underneath that line and the timer is actually going to be calling this function so we want to connect a timeout signal to our enemy script so go ahead and do that in the inspector and then inside this function we want to do the git target path target dot global transform dot origin so so it's the it's basically getting the position of our target but right now it won't work since it's set to no so we actually want to get a reference to our player and you could do on ready var players equal to get three dot get root that get node and then specifying the path and all that but we're actually gonna use the owner keyword here instead so to make use of that we're actually gonna do this in the ready function so funk ready and then inside this function we wanna do yield and then inside the parentheses we wanna do owner and then ready so we're essentially waiting for the owner of the enemy node to be ready and in the oh in this case the owner is actually going to be the level or our world and you can control click on it to better uh understand what owner does essentially but like i said it's essentially going to be our level or world in this case and then the parent is going to be the navigation and we're basically getting the player from the game script by doing this so target is equal to owner.player essentially and that's how we're getting the player so with that now done i'm actually adding my enemies into my world as children of the navigation so that they can actually make use of the nav mesh so i'm just gonna instant in two enemies and as you can see they are actually following the player around so it is actually working already like you can see so yeah everything appears to be working just fine but you can actually further simplify this so if you go into the enemy script you can actually get rid of the curved path index plus one and instead do path dot remove index zero so we're removing the first point of our path array like so and you don't need the other if statement so you can actually safely delete that you can also delete that line of code under the get target path you don't need it uh we do we do want to still keep the current path index since we are making use of it in other parts of the code and if we run it as you can see it's still in fact working so as you can see it didn't break the game by making those changes so it still works which is good now you can actually also remove this and instead use a group so let me actually show you how you can do this so for now i'm just going to comment that out and then i'm going to select my enemy and go into the inspector node and add it to a group called enemy like so so now my is part of the enemy group and what i can actually do in the enemy script now is i actually don't actually need the on timer timeout function anymore so i'm just gonna do comment the code inside that function and i'm gonna do pass for now you can actually safely delete it if you want i'm just gonna do this for now though just for demonstration purposes just to show you um another way of doing this essentially and then we are actually going to be using our game scene to actually you know have the nav work so we're going to add a timer as a child of the game or world and then we're going to connect the timeout signal to the game script and then all we're going to do here is we're actually going to do get tree dot co group so we're going to call the enemy group and then the method we want to call so in this case it's called git target path so we're calling the get target path function from our enemy in the game script basically and we're passing the player.global transform that origin to the get target path method by doing this and make sure that the timer is actually set to auto start so that it actually works and as you can see here by running the game it is in fact still working now it got stuck a little bit there but it does seem to be working properly as you can see so let me just test out that it doesn't get stuck again and yeah it's working just fine so we're good now we did almost forget to set up my layers for my player so go to your player make sure that it's set to the player layer and that can actually collide with the player and enemy and the world and then for the game scene or your level uh you want to set the you want to set it to be on the world layer and then it doesn't actually have to mask for anything and now we run the game again you will notice that it still works just fine so yeah we have implemented pathfinding for our 3d game here for our enemies so with that we're done congratulations so i am going to be leaving a link to the github project for this and the description so you can go check it out as you want and i'm also going to be leaving a link to my kickstarter project for a online multiplayer course so if that sort of thing interests you then definitely go check it out anyway if you liked the video make sure you leave a like and consider subscribing and as always i'll see you guys in the next one
Info
Channel: rayuse rp
Views: 3,185
Rating: undefined out of 5
Keywords: Godot, Godot tutorial, gamedev, How to make games, godot 2d, godot 3d, godot 3.3, godot 4, blender, 3d modeling, hololive, anime, channel update, godot multiplayer, rayuse rp, devlog, how to make 3d game in godot, Channel Update Godot and More, 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, Setting up 3d Pathfinding in Mins | Godot, godot 3.3.3
Id: OJSQZZ6s5pY
Channel Id: undefined
Length: 16min 1sec (961 seconds)
Published: Fri Jul 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.