How to use NavMesh Pathfinding in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we'll learn how we can use nav mesh for pathf finding hey guys it's me opurbo let's get back to the video what we will learn in this video we'll learn how to bake a nav mesh how to climb stairs how to use nav mesh agents what are areas in nav mesh how to make your agents jump and how to make your agents drop and so much more in unity we already have a scene ready to use for our path finding I just have a capsule called player a ground and two cubes for our obstacles so now let's open our navigation window or our nav Mash window so in order to open the nav Mash or navigation window we need to go to the window Ai and navigation now as you can see it opens our window we'll select the bake option and click on bake but as you can see nothing happens because in in order for the nav mesh to bake and nav mesh surface we need to Mark the object as static so how do we do that let's select our ground and go back to our inspector and as you can see right beside the name we have a check box called Static and I'm just going to tick that so after marking the ground as static I'm I'm going to move back to navigation window and hit on bake as you can see it baks our navigation and we can identify that by this bluish color on our ground but as you can see none of the obstacles are cut out so if I move the player it's just going to go right through the obstacles and that's because the obstacles aren't Mark as static so I'm just going to select both of the obstacles and mark them as static and rebake our navigation mesh and as you can see as soon as I do that there are some holes cut out for the obsti now we have a some sort of nav mesh surface going on now let's make our agent to go with it some the select my player and I'm going to add a nav mesh agent component in order to move in our nav mesh I already have a scrip ready so I'm just going to open that script and as you can see it's nothing complicated it's pretty simple I'm using the nav mesh so I'm going to import the unity engine. a library and then I have a reference to my nav mesh agent and a transform point and in the start I'm getting the nav mesh agent from the attached component and then in the update I'm just setting the destination of our nav mesh agent to the point position so let's select our player and add the nav mesh script to our player and as you can see now I need a point transform so I already have a point transform ready so I'm just going to enable it and as you can see it's nothing complicated it's just circle with a smaller radius and I'm just going to drag and drop it in our navmesh strip and then I'm going to hit play and move to our scene View and as you can see if I move the point the agent correctly moves to that Point's location and if I move it behind the obstacle as you can see it avoids the obstacle and goes around it now let's see how we can go upstairs and move from one area to another or drop from a higher point so for that I already have some stairs and elevated platforms created so I'm just going to enable them now I know that this might seem similar to other tutorials that's because in order to explain how nav mesh works I'll need all of this so does other channels that have a tutoral on nav mesh that's why it looks a lot similar trust me if I had any better way of explaining I do that but I think it's the best way to explain navm and that's why I'm using this methods as you can see because we have new obstacles or objects in our scene we need to rebake our NAB mesh for that let's just Mark all our object as static since I have all of my ground objects or my obstacles under the environment empty game object I'm just going to select the environment and Mark it as static and select yes change children to apply the changes to all of the children's now that everything is marked as static let's go to our navigation window and hit bake again as you can see it already baked a perfect area for our agent to walk on and we can immediately try out our new level so I'm just going to play the game and go back to our scene view if I just move the point as we can see the player or the nav mes agent actually follows the point and it climbs stairs and slopes very easily if you are enjoying the video take a second to like the video and another second to sub subscribe to the channel now let's check some of the settings that are on the bake section so first of all we have our agent radius so what this does is actually tells the radius or the width of our agent again this is not changing the actual agent this is actually for baking the nav mesh with this pre-built settings so when baking nav mesh you can have more controls of how you want to bake the nav mesh and we can also change the agent settings on the nav mesh agent and this window or uh the settings that are here it's only for baking the nav mesh so we can change the agent radius we can also change the agent's height we can change the max slope so it's in degrees currently it's set to 45 so if I just decrease the radius and hit as you can see our agent now can't climb this slope that's because it's greater than 30° next we have step height that means our stairs so if I just slide it to something very low like 0.05 and hit bake as you can see now our agent can climb the stairs so I'm just going to go back to our default settings so these are just some basic bake settings now let's learn how to jump between distance areas so what do I mean by that as you can see I have platform here that disconnected from our nav M we can only go to that area from here to there but currently our agent can't jump so how do you fix that as you can see in the big settings we have under the section generate off mesh links we have drop height and jump distance we'll focus on the drop height later but for now let's focus on the jump distance currently it's set to zero that means we cannot jump from any area let me just set to something else like five and hit bake as you can see now there are some lines created showing that you can jump from here to there and from there to here now let's play our game to see if it actually works so let's move up the stairs and stand here and if I just move the point to that location as you can see it jumps to that location now we can easily jump between our nav Mish Bak surfaces if it's within the distance of our jump distance now let's check out what drop height does it's basically the same thing as jump distance if I wanted to jump from here to the ground currently I cannot jump but if I just inut some value in our drop height let's say for five and I play the game move up the stairs and take the point to the ground as you can see the agent actually drops from the platform to the ground previously it do use the closest path to there which was either going with the stairs or with the slope under that we have we have the advanced settings and as I've said before this is that is not our Focus today so we're not going to bother our minds with this Advanced stuffs next I'm going to take a look at our agent settings so if I just move back to the inspector and uh check out our nav mesh agent the first settings that we have is the agent type So currently it's humanoid and if I just click on open Agent settings or go to our navigation window and move back to agents we can create multiple agents here as you can see it's currently I have I only have the default which is humanoid agent but I can easily create another agent rename it and set different values for that agent so I can have different types of Agents the next is base offset then next settings we have is the staring settings so the these are pretty self-explanatory we have speed that determines how fast our agent moves we have angular speed that determines how fast our agent will rotate or move around the corners we have acceleration we did which determines how fast the agent will start moving we have stopping distance currently set to zero that means how far from an object the agent will start stopping and then we have Auto breaking and then we have something called obstacle avoidance currently we're not going to bother our minds with the obstacle avoidance then we have the path finding section which is Auto Traverse of mesh links so what Auto Tri off mesh links means is what we did when we jump from this area to that area currently it's automatic but if you want to have more fine tool control then you will have the setting disable and and you will handle it manually but we're not going to do that in our video then we have Auto repath so that means calculate the path automatically and then we have area masks which is going set to everything but if you go to our navigation window and in areas you can create different types of areas let's say sand I can make this agent to work on everything except sand so this agent will not be able to work on sand but I might create another Regent which will be able to work on Sand so that's what area marks means so how can we set that sand area so if I go to the object tab in our navigation window and if I just select the ground and as you can see I have navigation area currently it's set to workable but I can select it to sand that that's it for this video if you want to learn how to create AI for your games make sure to go and watch this video next
Info
Channel: Ghost Studios
Views: 210
Rating: undefined out of 5
Keywords: ghost studios, ghost game development studios, ghost games studios, unity, unity3d, unity game development, programming, programing, 3d, game dev, game developer, navmeshagent unity, navmesh unity, navmesh pathfinding, pathfinding in unity, basic pathfinding in unity, navmesh areas, navigation in unity, unity pathfinding navigation, navmesh baking, how to bake navemesh surface unity', navmesh surface
Id: R8ENTQ42wWQ
Channel Id: undefined
Length: 11min 9sec (669 seconds)
Published: Mon Mar 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.