How to use Unity NavMesh Pathfinding! (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm your code monkey and here let's learn how to use navmesh this is unity's built-in pathfinding tool also this video is a lecture taken from my ultimate unity overview course unt is massive so in the course i explain over 30 features and tools of the engine that you might not know about there's individual lectures explaining tons of things like shadowgraph assembly definitions pro builder script execution order and so on also the course will continuously be updated with free updates as i add more lectures explaining more and more tools and features so go ahead get the phone course and learn how to master all of the unity tools to help you make better games faster in this lecture we're going to learn all about nav mesh in unity this is unity's built-in pathfinding system so first of all nav mesh stands for the navigation mesh the system makes a mesh which the agents can then use for navigation so let's see how to use it over here i've got my demo scene so i just have a simple plane then i have a cube acting as an obstacle and finally just a basic player capture so first of all we need to bake the nav mesh now to open the navmesh menu go up here into window then down here into ai and we have navigation so this opens up the navigation window now up here just go into the bake tab and we have a whole bunch of settings but for now leave it all on default and we just go ahead and click on bake and as soon as you do nope nothing happens that's because we need to identify what objects we want to be walkable so in this case let's select the plane and then on the navigation window let's go into the object tab and over here let's tick the box that says navigation static this means that this object will not move so for the purposes of navigation is a static object now another way to set this would be simply through the inspector over here you see a little static and if you click on the little arrow you can see all of the various ways that this object can be static so in this case we made it navigation static now with just that if we go back into the navigation into bake and we click on bake and yep we do see our navigation mesh so it's in light blue there it is so this is all the areas that our agents can walk in however just like this the issue is that it didn't identify this cube as an obstacle so again let's do the same thing first of all select the cube now let's mark it as static and in this case let's make it through the inspector and we can either just individually make it navigation static or just make the whole thing static so these are the various kinds of static things that you can have but in this case navigation static is all that matters so there it is it's set and now back on the navigation if we click on bake again yep there you go now it automatically identifies the cube so the wonka ball area is in light blue and then you can see over here on these corners nope the agent cannot go through there alright so so far so good we have our correct basic nav mesh now let's make the agent actually use it so let's select the player game object and over here on the inspector let's add a component down here go into navigation and let's make this a navmesh agent then again for now let's leave all of the parameters at default now in order to tell the agent where to go we need to make a simple script so let's make it let's just create a new c-sharp script call this the player neb mesh let's attach it to the player game object and let's open the script now here the first thing we want is to grab the nav mesh component and in order to access that type we need to add using unity engine dot ai and then here let's make a simple private void awake and unawake let's do a get component of nav mesh agent all right so we have our navmesh agent reference and now let's make a private void update and on update let's tell it where to go so the way we do that is we go into the navmesh agent and we modify the destination so for the destination let's add a transform reference so we can tell the agent where to go to so up here and let's make it a serialized field so we can set it in the editor of type transform and let's call it move position transform okay so we have that reference and down here just set it to that one dot position okay so our agent will be moving towards our move position transform so back in the editor here i've got an object that i made it just a very simple sphere just a nice material so we can visually see where it is our target so just on the player just drag that transform reference and there it is so now the player should move towards our move target but in this case there's an obstacle in the middle so it should go around the obstacle okay so let's see and yep there's the agent going towards the move target and as you can see it does avoid the obstacle so if i move the obstacle i put it in there you can see that it never goes through the obstacle instead it collates the path and knows how to get there alright so that's the absolute basics as you can see it automatically avoids the obstacles on navmesh so he knows to go around the wall instead of just ramming against it now that we've seen the basics let's inspect some of these parameters and for that here is a slightly more advanced demo scene so i've got this level that i built using pro builder which i also cover in detail in another lecture so i've got some basic floor i've got some stairs an area above i've got some overhangs the usual objects i've got some jumping areas some slopes and so on so now let's inspect on the navigation let's go into bake and in order to bake all of this area once again i need to make sure that all these objects are static so i can just select the parents since all these are children and just mark it as static and automatically apply some children so make all challenge sciatic and now if i go into bake and i hit on bake it automatically bakes the area okay so far so good now let's look at these parameters so you've got these two ones related to the agent so that's the agent essentially the width and height so as i increase the width let's say of one and i hit on bake again let's see what happens over here into this area so as i hit on bake yep there you go as you can see as the agent becomes more thick essentially the area that it can well becomes smaller and smaller so let's increase this a bit more and there you go now the agent is so thick that it cannot actually pass through this gap so the height is a similar thing except it affects some overhangs so for example over here i've got an area above and an area underneath and right now the agent can indeed go underneath here but if i increase the agent height and nope now it can no longer go there next up we've got the max slope so this is the angle that the agent can climb so over here i've got these two nice slopes so this one is on 25 degrees and this one is on 40 degrees and here the max slope and the step height are slightly connected as you can see by the warning here so for now let's make the step height pretty big so we can see both these slopes let's put it on the near maximum so right there so if i put it like that you can see that the agent can indeed climb this slope as well as this one which is quite a bit more intense look at that so it can climb that and that but if i now bring it down to maybe something like 30 yep now the agent can indeed climb through this one but he cannot climb that one so the next one is the step height so over here we've got some real nice steps again these were built automatically using pro builder really useful so if i set it at point one then these steps are bigger than that so the nav mesh does not break so the agent cannot use these steps so just increase them a bit and if there we go now these steps their height is under that one so it can indeed do that so now let's just briefly play around and see all this in action so over here let's just modify the code to only move towards the target position after i press the space bar so in here if just a simple input get key down when i press the spacebar then go towards there otherwise don't just so i can manually tell when to move so like this if i tell the agent to go there so i press space if there you go it does go around that obstacle now if i tell to for example go up there so i press space and there you go as you can see the agent does know how to climb through the ramp and if i go down there he should know how to go down through the stairs just like this and yep it automatically goes down and again for the overhang down there the agent also knows how to do that so you can go through there and go down there but if i try to tell the agent to go through this slope which is way too intense and the agent comes close but it gets there and that's it he can no longer go upwards okay then we have the generated off mesh links so these are connections between parts that are not directly connected so the first one is the drop height so for example if you want the agent to be able to just draw from there down there we can just increase this and look at that now the agent can indeed draw from any of these points on there so if i tell the agent to go up there the agent takes the stairs goes through there okay and now if i drag this and i put it all the way down there let's see what the agent does yep he goes through that link drops down and continues going so those are the various drops and the final one is the jump distance if i set the jump distance to be high enough and i hit on bake if there you go now we've got some links so the agent can jump from here to there and from there there so let's get the agent all the way up there and then move it on the other side press on space and if there you go it is the nav mesh in order to jump up there so this is how you link various nav meshes together and then you've got the advanced tab so these are much more advanced settings first of all the manual unboxing size so under the hood the way that all of this works is unity converts the entire scene into voxels and identifies which ones are wonkable so you can essentially play around this value to get more or less accuracy now if you'll lower the voxel size it will be much more accurate but at the cost of longer bake times and higher memory consumption and if you set it to bigger then becomes less accurate but also more performance but this is for really advanced use case so in most cases you really shouldn't need to touch this at all and then you've got the minimum region area so this the minimum size that region needs to be in order to be walkable so for example right now if i lower the agent radius and i hit on bake you can see that this area here is marked as walkable but if i then increase the minimum region area there you go that one is too small to be considered to be walkable so again this helps on optimization in order to not have to bake areas that are not meant to be walkable and finally you've got the height mesh setting so this impacts things like for example the stairs here which as you can see the nav mesh is really slope so it's not stairs and if you enable the height mesh and hit on bake and over here on the nav mesh display also enable the viewing of the nav mesh if you do that then you can see yep the navmesh perfectly matches the stairs so with the height mesh if i tell the agent to climb through the stairs you can see that instead of sloping he goes up the stairs he's down like that so again this is wrong due to personal performance but it also does have a memory cost in order to create all these separate areas so once again in most cases just having a slope is more than good enough so no need to actually enable over here the height mesh okay so these are all the bake parameters now let's see the agent parameters so here is the default netmesh agent component first one we see is over here the agent type so you can open the agent settings and it opens up the navigation window over here on the agents tab and this is where you can set all kinds of different types of agents so for example if you've got a small humanoid but then a giant orc this is where you would add all the various types so you can set the radius the height and so on then the base offset is essentially the y position over here on the agent so if it's at zero then the agent origin this is what matters for the compilation next up you've got the various steering settings so first the spin this is how fast the agent actually moves then you saw the agent also rotates to face the direction it's moving towards so this is how fast it turns then the acceleration so how fast it accelerates the stopping doesn't so how far from the target position does it start to stop and then auto braking to prevent the agent from overshooting the target then you've got the obstacle avoidance so this is related to avoiding other obstacles they can be fixed obstacles or they can also be other agents so you've got the usual settings radius height then again the quality so naturally higher quality avoidance will look better and avoid collisions more naturally but again at the cost of more expensive performance and then the priority for the agent so for example if you had a hundred npcs walking around and one player character you would obviously make the player have much higher priority in order to have the collisions be much more accurate relative to the player and then the pathfinding settings so the first one does the agent automatically to reverse the nav mesh links so those are the gems that we saw here and drop down there if you want to handle some complex animations or some extra settings when jumping between links then maybe you want to disable this and handle it manually otherwise it will move like we saw then the auto repath which is if you want the agent to constantly collate the path to destination to take into account any moving objects that might block the agent as it's moving and finally the area mask so this is where you set which areas the agent can wank and which ones they cannot so in this case so far we've been using the most basic thing possible we really just have wonka ball and not walkable you can define the areas over here on the navigation window go into areas and you can add all kinds of them so for example you add a new water area then in order to define an area with this particular area type you simply select the object so let's actually make a different one okay so i have this little cube here to mark our area so you just go into object and over here select the object and here you've got the drop down menu where you make what type of area this object belongs to so in this case let's say this one belongs to water and once again just hit on bake and yep now it identifies that this one is some water but you can see especially if we hide the object you can see that it's on a different color so this one is on a different area so you can see the blue is the walkable area perfectly normal and the next one is the water so there you go this one is on a different area then you can for example select the player and over here on the nav mesh agent on the air mask say that the player cannot walk through water and now just like this fight on the agent to go there yep he does not go through there again this area is walkable just not walkabout by this agent but now if i take the agent and i once again say that this one can now walk on water and now move it over there press on space and yep it does now go through that area so you can see how with just these basics you can already do quite a lot with the unity's navamesh system so whenever you need some pathfinding for your games consider the navmesh system alright so this was a lecture from my ultimate unity overview course there's a lot more explaining tons of things like shadowgraph similar definitions pro builder script execution order and so on go ahead and get the full course and learn how to master all the unity tools and features to help you make better games faster alright hope that's useful check out these videos to learn some more thanks to these awesome patreon supporters for making these videos possible thank you for watching and i'll see you next time
Info
Channel: Code Monkey
Views: 41,602
Rating: undefined out of 5
Keywords: unity navmesh, navmesh unity 2021, navmesh unity 2020, unity navmesh obstacle, navmeshagent, unity navmesh movement, unity navmesh jump, unity pathfinding, navmesh, code monkey, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 3d, unity, game design, game development, game dev, game development unity, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials, how to make a game
Id: atCOd4o7tG4
Channel Id: undefined
Length: 15min 24sec (924 seconds)
Published: Mon Jun 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.