Basics With Godot | Working with the 3D Navigation System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is mitch with findpoint cgi and today we're going to continue the basics with godot tutorial series where i take you through from not knowing anything about godot all the way up to being pretty decent at godot so today we're going to talk about 3d navigation meshes and we'll probably jump into 2d navigation messages probably in the next tutorial but in this case we're going to focus on 3d navigation inside of godot so what i'm going to do is i'm going to take you through the process of building a level baking the navigation inside of that level and then setting up a small character that you can click and run around that little level so that way you can see how the navigation system works but first we need to talk about what the navigation system is so the navigation system is godot's way to calculate your navigation of all the characters in your scene so what it does is it bakes the path finding grid almost and tells the system when you click on one spot it tells the system the shortest path between those two points it's extremely useful for like grid based games it's extremely useful for ai you know centuries or something like that or if you wanted a point-and-click adventure game you know things like that um it's a super useful system for that for static meshes [Music] now where things get a little funky is when you start adding in platforms and things like that and that's when you want to go for a more verbose navigation engine like a star or something like that now this specific tutorial here is for godot 3.23 and the reason why i specify 3.23 is because in 3.4 when it comes out they're coming out with a new i think it's four actually it's not 3.4 in four they're going to be coming out with a new navigation server which is built upon the a star platform and it's completely different so if that's what you're looking for go ahead and wait i'm gonna make a tutorial on that when it comes out and then we can go from there but with that being said let's go ahead and jump into godot and uh we'll take a look at this all right so now that we're in godot what we need to do is we need to first create our node so let's go ahead and add in a other node not a just regular node you actually need to add in a other you know you need to add another node you need to add any navigation node okay and what that's going to do is that navigation node takes all of the sub nodes under it and uses that to determine navigation so it's very important that you use that as kind of like your root level node almost and that's the node that's going to handle a large sum of your um navigation calculations and things like that so next what you need to do is you need to add in another child node and need to add in a navigation mesh instance now a navigation mesh instance actually takes the meshes that are under it and it uses that to calculate your navigation nodes if that makes sense so it what it does is it takes the bounding box almost of the side of the entire project that's underneath this navigation mesh instance and it creates points so it can determine how it's going to navigate around objects so we'll place that i mean if none of that made any sense basically it just lets you bake your navigation mesh i mean it's really that simple now we're going to do is we're going to right click we're going to add another child note we're going to add in a mesh and we're going to add in a mesh instance and let's go ahead and add in a new uh plain mesh like this we're going to scale this up something decent in size let's say about that big that works for me and then you'll notice that if i click on this it'll have a little tiny node configuration warning navigation mesh resource must be set or created for this node to work so what does that mean well what that means is we need to create a navigation mesh instance here all right so now that i have that little navigation mesh what that is it's basically the thing that your navigation mesh kind of bakes to almost it's the nav mesh instance that it bakes into so now what we're gonna do is if we click bake nav mesh you'll see that i got a lighter color here see and you'll see if i kind of hover down you'll see that it kind of is above right here versus right here it's kind of above your mesh and that's how godot determines things to be navigatable if that makes sense so let me show you if i were to right click add in another mesh instance and i were to add in a cube all right and then we move that cube like here let's say then we click on mesh or navigation mesh and we bake it you'll see suddenly it shows around that object your um collision almost right so the game or i guess godot has figured out that hey this is not a passable area you're not able to pass this so don't you know the character's not allowed to pass that so now what we can do is we can go ahead and duplicate this so ctrl d and then let's go ahead and drag this over let's say here let's drag this down something like that okay and now if we bake this you'll notice weirdly enough it did not create that right it kind of did like a little hump and then up and over right now the reason why that is is because of the navigation mesh settings so if you click this open here like this or if you right click and you do edit it'll allow you to edit it as well and you can see you have a lot of these options here so if i open these all up so i can kind of show them all to you there's a lot of them i mean look at them all they're crazy right so let's go through them one by one because i know this is a lot but trust me once you go through them one by one it's really simple so your first parameter here is the sample partition type okay and you have watershed if i click this open if you have watershed monotone in layers now what these are these are algorithms and they're used for graph traversal algorithms there's a whole lot of information there but basically what does it computes points in your navigation mesh so watershed is basically uses the highest point kind of like a watershed in order to make paths uh for the navigation mesh so you can see here how it makes a point here see point and another point to kind of create this navigation whereas if you did mono tone or layers you'll notice that suddenly this actually works properly right so if i change it over to monotone what it does is it sorts the points in the navigation mesh so it can build a path so you can see here how it suddenly got it correct so what it does is it actually uses the underlying mesh to create um the navigation points and layers allows you to bake from almost like layers it kind of just bakes it up going up until it hits the height that it's allowed if that makes sense so this is a fairly accurate way to do it what i suggest is instead of being you know instead of running through um just run through them and see which one bakes the best navigation mesh for you because they're all just different algorithms on how they handle the navigation system if that makes sense all right so now we have geometry here so if i change this to monotone and then we take a look at our parsed geometry you can see that there's messages static colliders or both now mesh instances means that it's going to bake off of the mesh instances so off of these little mesh instances here static colliders will only bake off of colliders that are static or both and both means it's going to take both mesh instances and static colliders and then use those together as a way for it to create your navigation mesh now generally speaking i like to use mesh instances however if i'm using custom level geometry that has a lot of data static colliders makes more sense it's faster for the algorithm so you're going to want to use static colliders if you're using a lot of custom geometry and stuff like that because if you think about it if your floor is you know a hundred thousand polygons right well your static collider might only be you know 50 polygons and that will help build a proper navigation mesh versus using mesh instances which would really bog down your bake time if that makes sense source geometry is how it determines what is considered a navigatable mesh so if you were to say nav mesh children it's any child that's underneath this navigation mesh instance group with children is if you were to specify a group so you'll see here that there is a group name so if i were to take this object here this mesh instance and i were to add it into a group so if i come over here into node and then i click groups and i create nav mesh and let me copy that real quick and i add this to this group and then i select uh this object and i also add it to that group and then if i go back to the nav mesh instance and i change this to nav mesh when i bake my nav mesh you'll notice that this it no longer got affected so you can see here if i can select that that'd be great there we go you'll see that this did not get affected right if i add it to that group paste add and then i re-bake my nav mesh you'll notice that suddenly that nav mesh is now calculated once again so it's really useful if you have lots of objects underneath a nav mesh you know for instance your entire projects underneath this nav mesh and you want to exclude or or include stuff using a group would be really useful for that group explicit will only apply the group so for instance if i were to select this object and i create a child node and i do mesh instance and i change it to a cylinder let's say so now i got the cylinder sitting here providing that i can find it there it goes that's a big cylinder so let me make it a little on the smaller side [Music] there we go so now i made a little smaller and we'll put it into the ground if i were to put now you can see this object does not have a group you see it doesn't have this little icon so it doesn't it's not in the group and if i go over to the node you can see it's not in the group but if i go to the navigation mesh instance and i go back to my inspector and i go group with children when i bake it you'll notice that it calculates that right it calculates the nav mesh around it if i go with group explicit and i click bake navmesh you'll notice that now it's not affecting it because this object is not a part of that group right it doesn't matter if it's a child if you have group explicit it doesn't care it just navi it just says whatever is in the group is a navigatable mesh so you can see how these are navigatable see so that's how you can tell the difference between those cell size is almost like the resolution of your mesh so if you were to bring this down and you were to bake it you'll notice that see how these are a little bit more divided so if i were to bring this down a bit more and bake it well i guess in this case it's not going to show but if i were to bring it up higher you'll see how those points got those bevels got removed and the reason why is the cell size determines the resolution of this mesh so you'll have to tweak this a lot to determine how much of um like to make sure that all of your nav mesh actually makes sense right because um you know if you have two objects close to each other your cell size may need to be smaller so it can determine that that there is space enough to walk between those two objects height is exactly what you would expect it to be it's the height at which this simulation or this little nav mesh can be baked so if i were to bring this down to something like point one when i bake it i guess in this case it didn't do anything but if i turn it up higher and then i bake it you'll see that the actual mesh itself has gone up see and i bake it you'll see that it comes down so this is useful if you want to offset the height of objects from the ground agent agent height is the actual height of the agent so if you have an object for this thing to traverse underneath right the agent height matters so if you were to take the max climb down you'll notice suddenly it cannot climb on top of this because it is below the max climb threshold if i were to put that up higher you can see now it can if i were to put that up really high you'll see that now they can climb on top of this because the max climb height is high enough that they can climb on top of this see if i reset that to 0.9 radius is the radius of the navigation uh agent so if i were to bring this next to this and i were to let's change my navigation mesh from that to nav mesh children and we'll just bake this you'll see that it creates a separation here but if i pull this out a little bit further and i bake this you'll see that it allowed you to walk through these two objects but if you were to change your radius up now you can no longer walk between those so radius is the radius of the character itself to determine if it can walk between two objects if that makes sense so you can see if i bring that down now you can walk between these two objects max slope is the maximum slope that you can walk up so if i were to take this object and duplicate it let's say let's move oh well if i grab the right thing let's move this over and let's scale this down and rotate it something like this right and then we bake this you can see that he can't walk up this but if i were to change this to 90 you can see now he can the the character can walk up this slope now i would need to mess with the cell size and the height and things like that to get a good there to get a good navigation mesh but you can see that because i change the maximum slope that's the maximum slope that the character can walk up now in this case you know something like 80 or 70 would probably work but generally speaking the default is 45 degrees as you can see how they can't walk up this it's not 45 degrees right so if i were to change this to something more you know 45 degree e and i were to bake it now the character can now walk up this slope [Music] so that's the majority of what you have with the agent again to kind of recap height is the height of the agent radius is the width of the of i guess what is around the agent so what it can fit through in terms of gaps max climb is the maximum height that the navigation mesh is allowing the person to go up and max slope is the maximum slope or the angle that the character can walk up all right so the next section is region you have a minimum size and a merge size so if i were to bake this nav mesh you can see that everything seems like it's being baked right but if i come over here let's add in a new navigation mesh mesh instance and let's make it a plain mesh i don't know where oh i put it over here so let's drag this out over here off by itself and let's kind of focus in on that real quick so what region and merge size is is it determines if it gets baked on a nav mesh so you can see here how this is being baked but if i change the minimum size up something like 73 now this is no longer being baked and it's the same thing over here you can see the cube no longer has a bake either you can see if i bring this down how now the cube has something on top of it so what this does is it determines the minimum size that is considered a walkable surface inside of godot now the merge size is the same thing except for it determines the size at which it can merge into other meshes so if i were to make this a really high number in theory things will start merging into each other so if i reduce this down to a very small number you can see that that merge did not happen it's a way for the nav mesh to to merge and simplify itself if that makes sense they look kind of like a decimation modifier inside a blender write it it pulls the vertices together and combines them so this is really useful for optimizing your nav mesh but it can also create problems inside of your nav mesh such as this right so the character can no longer walk directly through there they're going to walk up and then over you can see if you look at it they're going to walk up here up here and then over it and then down see so let's go ahead and rebake that and there we go [Music] edge is very much a similar to region you can actually determine the maximum length of an edge so if the edges is uh let's re-change that so if i put this down you'll notice that when i bake it there are a lot more edge lengths here see how there's there how it changed it how there's now a lot more edges a lot more things like that it's the maximum length that can be determined if it's a good edge so you can see here if i bake this up how it simplifies the mesh but if i pull this down to something like point one right you'll notice that things start changing well sometimes it changes sometimes it doesn't you gotta find like the special sweet spot to get this thing to to actually do it but what it does is it's the length of an edge that can be determined if it's valid if that makes sense max error is the maximum error threshold of a mesh and again you're not going to see much difference inside of here but if i bring it down you can see that this changed how it how it almost follows the edges closer if that makes sense now generally speaking you kind of want them to be smooth right you don't want your characters to be like bouncing around or anything like that so just make sure that you know you kind of use it to help smooth out your edges if that makes sense verts per poly determines the number of verts uh vertices that can consider to be considered a polygon so if you were to bake this you could say we have six right and there's again you're not going to see much of a difference here as i kind of move it up and i move it down but generally speaking what it's useful for is it's useful for optimizing your navigation mesh so i'm gonna keep that at the default six because to be honest with you i don't really ever touch this whenever i use it for navigation mesh so i wouldn't really worry about it too much detail sample detail or sample distance for detail meshes so if you were to put that at zero you'll see suddenly well everything slows down like crazy and the reason why and it'll probably crash my computer because i probably shouldn't have set it to zero but the reason why is because now this is how detailed your navigation mesh is gonna be all right so godot has crashed for me so i actually just rebuilt my scene it's not quite the same as what you guys saw but at least we can continue so detail is the detail of um the i guess you could say the resolution or how how its second pass happens so if you set this down to something low it may or may not crash your computer so make sure that you're or crash your your uh project so make sure that you're careful but you can see here like how it affects right here right so you can see this and then if i pull this down to something like one you'll see that how it changed it see how it's slightly different it seems to fit better if that makes sense so what we're gonna do i'm gonna bake that down a tiny bit there we go and now finally what we have is the sample max error which is the amount of errors that it can have before it says hey this is not a good enough um mesh to move on if that makes sense so you adjust that to kind of help refine this so if we kind of bring that down you can see it kind of changes it's like if we bring it up it kind of changes it a bit more and you can also see how it can affect see that how it can affect this so you see when i bake it how it has a problem there but if i pull this down you can see that it snapped that up so that's kind of what the sample max error is now generally speaking i don't mess with this i generally just mesh mesh i generally just mess with the agent height and the radius and the max climb and that usually helps me out so you can see when you change those things how it can change how your objects can walk along these things so you can see because of the height they can't walk underneath this anymore whereas if i reduce the height like i've shown you before now they can or if i change the max climb down you'll see that they can no longer climb on any of these objects if i bring it up a tiny bit and i bake it you'll see that now you can walk along this area because now they can climb up here obviously we don't want that so we're going to bring that down a tiny bit there we go so now they can't walk along that but we do want them to have the ability to go through here so let's bring the radius down some and bam now we can walk through here perfect there we are so now if that's the case let's look at filters now filters i'm not going to show you how they work because getting them to work is an absolute nightmare but generally speaking low hanging obstacles allows you to have low hanging obstacles inside of your project and it filters those out so it doesn't affect the navigation mesh ledge spans basically just uh spans over ledges so if there's two ledges that are next to each other it can span between the two and filter walkable low height means it's gonna filter out any walk uh walkable lower height issues so for instance this is too close or it's within a specific margin this will filter those out all right [Music] now we get to come to the fun part coding so what we're going to do first [Music] is we're going to create our character and get that started so let's first right click on our navigation add and type in remove that open up spatial let's take a look we want a position 3d so let's grab that and let's just call this player and then let's go ahead and add in a child node and add in a mesh instance and we're going to make this a capsule mesh so now let's rotate this 90 degrees if you hit control and rotate it will snap it and then we will scale this down some so let's make it smaller i'll see it's i need to change this to be [Music] not local transform all right let's drag this over here real quick oh and actually i need to grab my player oh dear let's get rid of all this let's start it over real quick and what we'll do is we will first scale this down like this and we will scale this down like this something simple and we're gonna rotate this 90 degrees and that should be good we're going to drag this up so now it's a player walking around the world and you can see that this is now at the base of the capsule now that's important because if it's not at the base then the players not going to be able to walk around on this navigation mesh without having some kind of difficulty if that makes sense so now what we're going to do is we're going to add in a camera because you need to have a camera for it to work so we'll go ahead and add our camera we're going to drag this out here and we will place it right here i think so let's oh whoops i am the worst when it comes to navigating some of these things in godot sometimes let's rotate this 45 degrees let's pull you up and then let's change this to local and let's snap that down a few let's take a look at that and see if that's good we'll change our effo feed at 90 because 90 fob is good and it'd be nice and we're going to say yes this is the current one and [Music] quick tip for you guys if you guys just click preview you guys can see what it's going to look like so i don't mind that it'd be nice to move that a bit closer so let's go ahead and move that a touch bit closer like that uh it's still too that's too close so let's pull that back a tiny bit there we go that's good enough for what we're trying to do so now what we're gonna do is we're gonna right click on navigation we're gonna attach a script and call it navigation.gd you can call it whatever you guys want but i'm just going to use navigation.gd all right so the first thing we want to do is we want to have a conversation about what we want to do right so what i'm going to do is i'm going to make an object that follows another object so we're going to make the player follow an object around this navigation mesh so first what we're going to do is we're going to add in a mesh node and we'll say mesh instance let's say and let's go ahead and make that a capsule mesh and let's just make it small we'll go ahead and just make it tiny like this so we're going to do is we're going to tell the player to walk up to here i think [Music] there we go [Music] there that should do it and then we'll take the player and we will place the player over here somewhere so the players not near the navigation mesh and this will force him to kind of navigate around to find the best path to the player or to that mesh instance and we'll call this goal whoops all right let's go back to here what we're going to do is we're going to say on ready var whoops not bar var uh player is equal to get node player oops it needs to be quote all right on ready [Music] var goal is equal to get node [Music] uh goal all right now we're going to do is we're going to go into function ready and we're going to say uh actually we're going to var here path is equal to open and close because we're going to need to create a path for the player to walk along and then what we need to do is we say path is equal to get simple path now you got to start an end and an optimize okay so first what we're going to do is we're going to say player dot transform actually dot translation yeah dot translation comma now we need to get the target point so we'll say goal dot translation and then we will also say true because we want to optimize that point and now what we need to do is we need to force our movement to happen so we need to have it in funk underscore physics process because we don't want it to run on the like if you remember i don't know if you guys remember this from previous tutorials but this is called every frame on your gpu physics is calculated on a set time frame and when you want to move objects you want to try to move it without it being frame dependent because then it depends on your frames and how fast your computer can render something so you want to use it off a physics process anytime you're moving objects so now the first thing we need to do is we need to say var direction [Music] is equal to vector 3 vector 3 and we will say var step underscore size is equal to delta times our speed so our doings are saying hey and i spelled delta wrong because i can't spell what we're saying is hey we want you to take our physics process time and then multiply it by speed and that'll be how much to move the character each uh if that makes sense and then what we'll say is we'll say var um step amount is equal to delta times that's plus times 10 and then we're gonna say if path.size is greater than one dot size is greater than oh zero i guess so now we need to determine our direction so the direction is is kind of like the difference between where we currently are and where we'd like to go so think of it kind of like you need to make your character look in a general direction to move in that general direction if that makes sense so first when you say var destination is equal to path zero and that's why you have that little bit of error handling here is because if we did path zero and it was the size of zero we'd get a hard crash so we probably shouldn't do that and then we want to say direction is equal to destination [Music] minus our player dot translation all right now once we have that now we need to step to the next node if that makes sense so if we don't do that there's a possibility that the character could just keep walking and then it might walk into a wall or you know jump off of a cliff or something like that right so if our step amount is greater than direction dot length then we need to do step amount is equal to direction dot length [Music] so that way what we're doing is we're saying hey we uh don't want you to walk over what our direction expectations would be so we don't want you to go past our point if that makes sense so if you have a goal post you don't want the character run past that goal post you want them to run to that goal post and then what you want to do is you want to remove p a t h i can't spell today path dot remove zero so you want to remove that path from the node since we are going to be reaching this node we don't want to constantly be running to the same node so now that we have that we want to do player dot translation plus is equal to our direction [Music] dot normalized [Music] all right times whoops that's not times times our step amount all right so what does this do well what this does is it creates it moves your player towards that path node and it moves them by the amount right here that we want them to move so now that we have this let's go ahead and test it so let's try and run this and see what it does probably not going to work but let's find out uh see we've got a heart failure right here because i can't spell so let's copy that and paste it save let's refresh it and there you go see how the character just ran to that point fantastic that's exactly what we wanted so let's let's slow that down a little bit so we actually get to see what it's doing so let's change this size down to something like two and now let's try it so you see he runs through climbs up and runs to the location so now we have this we don't necessarily want our player to go to just one goal let's have them go to multiple goals so first thing i'm going to do is i'm going to grab a note and i'm going to make this into a group and just call this gold goals and then we will add that now let's take this duplicate it a few times and let's pull this down here let's grab this one pull this one down to here and let's move this one over here and then let's control duplicate it and move it over here like this so what that's going to do is it's going to give him multiple goals to be able to run to and now what we're gonna do is first we're gonna save so we don't lose our stuff and then we're gonna go back to our script we're gonna say goal instead of get node let's do get underscore tree dot get node nodes in group and then let's go ahead and quote paste our goals and that will give us our goals and now i'm going to [Music] do goals instead i don't think there's any more goals in here so now what we need to do is we're saying hey we're trying to get a simple path of this right but instead of doing it here let's bring it out into our physics process so now that we've moved this down here what we're going to do is we're going to wrap this in an if statement so if path dot size is smaller or equal to zero then i want this to start running so we first need to do is we need to do a uh var rng is equal to random number generator dot new because we're going to create a new random number generator then we're going to create a rng dot random mice and we are going to say var my we're going to say var um goal number is equal to rng dot rand if range we're gonna say zero comma [Music] goals dot size [Music] minus one so what that's going to do is that's going to give us a random goal to go after now we're going to do is we'll open this up and say goal number and that should give us back did i not oh my uppercase versus lowercase there we go so that should give us a random goal to go after so let's see what happens so you see he's running after that goal right there he hits it now he's moving to another goal uh it looks like it's this goal here so he hits it and then he's gonna move to the next goal so that's a cool way to make a kind of like a random policing um character that just runs after different goals if that makes sense and it's random so we don't know which goal he's going to go after until he decides to go after that goal so that's what i have for you guys today so to recap what we talked about was what a navigation mesh is we went through all the different procedures and all the different processes and options that you have that corresponds with the navigation mesh we created a small level we had a crash or two and uh we created a small century-like character that runs from point to point to point so if you guys like this video go ahead and hit that like button if you guys dislike this video go ahead and hit that dislike button because i make content for you guys and i want to make sure that i'm making content that you guys enjoy now this tutorial was a comment asking me to create this tutorial so i do actually pay attention and create the tutorials you guys want so thank you so much for watching i'll see y'all next time [Music] thanks [Music] you
Info
Channel: FinePointCGI
Views: 544
Rating: undefined out of 5
Keywords: godot, godot engine, godot game engine, getting started with godot, godot tutorial, godot navigation, godot 3.1, godot navigation 3d, godot 3d navigation, godot navigation tutorial, godot 3d tutorial, godot 3d navigation tutorial, como usar navigation en godot, godot navigation polygon, navigation mesh, godot beginner tutorial, godot engine tutorial, godot tutorial for beginners, Basics With Godot | Working with the 3D Navigation System
Id: 9yQ5jax2PIA
Channel Id: undefined
Length: 43min 33sec (2613 seconds)
Published: Mon Apr 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.