Unity NavMesh Tutorial - Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This guy's videos are great.

Anyone know of a good resource for baking navmesh at runtime?

For example, if walls move, or new objects are created?

👍︎︎ 5 👤︎︎ u/WheatonWill 📅︎︎ Mar 15 2018 🗫︎ replies

Can't wait till the next video :D

👍︎︎ 2 👤︎︎ u/MengKongRui 📅︎︎ Mar 15 2018 🗫︎ replies

I seem to be kind of stuck. I have downloaded the example project, however none of the scripts seem to work.

I currently have 11 compiler errors, all with a similar message saying "Assets/NavMeshComponents/Scripts/NavMeshLink.cs(43,9): error CS0246: The type or namespace name `NavMeshLinkInstance' could not be found. Are you missing an assembly reference?"

"Namespace name" errors:

NavMeshLinkInstance, NavMeshCollectGeometry x2, NavMeshData x2, NavMeshDataInstance, NavMeshBuildSettings, NavMeshData, NavMeshBuildSource x3

Anyone have any idea how to fix this?

👍︎︎ 2 👤︎︎ u/AX_ZonE 📅︎︎ Mar 19 2018 🗫︎ replies

Just a quick check if anyone knows, is this new NavMesh system multi-threaded in the backend? I mean does it utilize the new Jobs system or is it purely a backend implementation?

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Mar 15 2018 🗫︎ replies
Captions
in this video you'll learn how to create simple pathfinding using unities navmesh features at the end we'll have a working pathfinding system where the player can click anywhere on the level and the character will then find the shortest path there to do that we'll be baking a navigation mesh using the nav mesh surface component which allows us to define walkable surfaces for our agent will then write a script to control a nav mesh agent along the surface also this video is sponsored by unity alright let's get started so as you can see I've set up this simple example scene in here we have a player that's the green cylinder here as well as a tiny level the level consists of a ground cube and on top of that some walls and that's pretty much it now the improved high-level nav mesh components are available now as you can see I've included them in my project here you can use the link in the description to get both the nav mesh components as well as this example scene alright so the first thing that we want to do is bake a nav mesh this means that we will define what surfaces are walkable for our agent to do that lets go to the hierarchy let's right-click go create empty let's reset the transform on this and let's rename it to nav mesh let's then drag it to the top so we can easily see it so this game object is going to be responsible for baking our nav mesh to make it do this that said add component and we'll add in the nav mesh surface component here we have a few different settings but if we just go ahead and hit bake we can see that right away unity creates a nav mesh for us all the blue tinted areas represent surfaces that or aging can walk on and so we can see that he cleverly avoids all the walls we can also see that the agent can currently walk on top of some of the walls and that the nav mesh currently treats our player himself as an obstacle to avoid this we edit the include layers here we can select what layers we want to include when baking our nav mesh in our case we don't want to include our player so we can go select our player go to the top right Wed set Slayer and add a new layer and we'll create a player layer will then select our player and make sure to assign this new layer to him then when we go back to our nav mesh we can simply disable this layer when we then hit bake we can see right away our nav mesh simply ignores all player now we can easily control what parts of our environment we want to take into consideration when baking a navmesh we can also choose whether we want to bake or navmesh from the render meshes meaning the actual look of our level or using physics colliders now I don't currently have any colliders in my scene so if I go ahead and hit bake we can see that nothing really appears however if I go in here and add a Collider to the ground I'm gonna add a box Collider and then go back to the navmesh and hit bake we can see that this now shows up but we can simply use the render meshes and we bake that finally we have a setting at the top here called the agent type and right above this we have a drawing with some different values if you go under the agent type we can open up the agent settings this is part of the navigation window you can always bring this up by going window navigation now we can choose some properties for our agent and these properties will affect how the nav mesh is built right now it's just using some default values for a regular humanoid character but if we go ahead and hit the plus sign we can create another one this one is going to be an ogre we can increase the radius because he's probably going to be fairly big we'll set that to one I could also imagine him being pretty tall so we'll set the height to three then we can adjust the step height which is probably going to depend on the length of his legs I like to imagine that he has pretty short ones and finally we can adjust the max slope which is how well he can climb angled surfaces let's say that he's fairly lazy so he'll only do about thirty degrees now if we were to go back to the inspector and change the agent type to ochre and hit bake we can see that our ogre will have a much harder time getting around our maze in fact there are plenty of places that he won't be able to get through I'm just gonna change this back to humanoid and we bake now we don't want our walls to be walkable so to change this we can go and select our walls and here we'll add another component called the nav mesh modifier the nav mesh modifier allows us to change nav mesh settings for a given object we could for example ignore it from Build which means that if we re peg our nav mesh it's just gonna completely ignore our walls we could achieve the same effect by simply assigning our walls to a layer however we do want our walls to be used we just don't want them to be walkable so we'll deselect ignore from build and instead used override area this allows us to change the area type for this object if we select this you can see that we have three default area types walkable not walkable and jump as you've probably guessed if we select not walkable and we generate voila our player can now only walk on the ground finally we can choose what agents this is going to affect by default this is set to all but we can't be specific this allows us to create areas of the map that are different for each agent say if you're creating an RTS game and you don't want your regular units to pass through fire but you want your fire elementals to freely walk past it so affected agents can actually be really useful now that we set up our nav mesh we are ready to start moving around the player to do this we're going to add a component to a player called the nav mesh agent a nav mesh agent is a character that can navigate around the mesh here we have a bunch of different settings we'll look into these in a second but first we want to create a simple script that will control a player so we'll hit add component and we'll create a script called player controller let's say create an ad let's double click it to open it up in visual studio now before we start scripting let's have a look at how to go about this so controlling a nav mesh agent in unity is actually really easy most of the time we use a simple function called set destination this allows us to input some kind of position and our agent will then automatically find the shortest path there in our case we have a level and on top of that level we have a player we also have a camera that looks at our level and we want to be able to click on some part of the level and then have our play automatically move to that location to do that we'll look for some kind of mouse input and when the player presses the mouse will send out a ray to the point where the player pressed will then look for where this ray hit and we'll get the position at that point and then by using set destination on the agent will have him automatically calculate a path and move to that location so to do this in code we first want to remove our start function and inside the update method we want to check if the player presses the left mouse button to do this will write if look under the input class and will use a function called get mouse button down here we can specify what mouse button we want to check for we want to check for left mouse which has the index of zero we then close off our if statement and in here we can specify what we want to happen when we press our left mouse button in our case we want to use the current position of our mouse on the screen to send out array we can get the position of our mouse by going input dot Mouse position and this is our current mouse position in screen coordinates but we need a function that can convert this to a position on the screen into array that we can shoot out luckily our camera has a function that does exactly this so we need to go to the top here and create a reference to our camera we'll create a public camera let's just call it cab then at the bottom here we can use cam dot and then we want to access the function called screen point array this is going to take in our mouse position and it's going to convert it to array that we shoot out in the direction that we click we then want to store this ray in another variable so we'll create a variable of type Ray let's also call it Ray and set it equal to whatever we get from this function and before we actually shoot out this Ray we want to have a variable that we can use to store information about what it hits to do that we create a ray cast hit and let's just call it hit so now we're ready to shoot out the Ray to do this we write physics dot ray cast will feed in the Ray that we want to shoot and remember we want to take the information about what we hit and where we hit it and store it inside of our hit variable so we'll write out and then hit this is going to shoot out our ray but we don't actually want to move our agent unless our Ray hits something if we just click somewhere where there isn't an object then we don't want our agent to do anything this is in fact really easy to do because physics that Ray cast will be true if we hit something and false if we didn't so we can actually turn this into an if statement at the beginning here we'll write if and we'll make sure to close off the parentheses as well and then we can open and close if statement so what we're doing here is basically checking to see if we click our left mouse button if we do we take our current mouse position and create a ray in that direction within shooting out this ray and we are gathering information about what we hit in the hit variable and in the event that our ray actually hits something we're going to continue into this if statement and so this is where we want to go ahead and move our agent and whenever we're working with the navmesh system through script we need to make sure to include so at the top here we'll write using unity engine dot AI we can also go and remove our two namespaces at the top because we won't be using those and now to move our agent we of course need a reference to him so at the top here we'll write public nav mesh agent and we'll just call it agent then down here where we want to move our agent will write agent and he will use the function that we saw before called set destination and the destination that we want to set is the point that we hit so hit dot point and that should be all of the code that we need to do so if we now go into unity we now have two empty fields under the player controller here we'll drag in our main camera as well as our nav mesh agent component and if we now hit play we can see that we can click anywhere on our level and our agent will try and find the shortest path there in fact we can even click again to change direction while the player is moving pretty awesome we can also configure steering settings under our nav mesh agent here we can change the speed let's set that to eight the angular speed which means how quickly it rotates that set that to 180 the acceleration which means how quickly it starts moving or changes direction we're gonna set that to 15 and finally the stopping distance is how close we need to get to the end of our path before the agent stops moving I'm gonna set that to point one if we play now we should see that the agent moves around a lot quicker and we now have a working demo yay that's pretty much it for this video and the next one will dive further into the navmesh system so make sure to subscribe to get notified once it's out on that thanks for watching and we'll see you in next video thanks of the awesome patreon supporters who donated in February and a special thanks to encode we trust Patrick McKinley Daegu Frank Tristan town dying guy and Buffy Oh infinity PPR yaaay Omer and soft tune sighs mommy dark Eames Kirk fatal Murphy beard or die DoubleTap 45 James P J delay Superman the great John pure guard Dennis Sullivan Jason Latino Alex wicked ski beyond photo ops wetlands villain of gin Sasha hafsteinn Sunni Arabs and Gregory Pierce James Rogers Robert Burns derman Rob fan and Erasmus you guys Rock
Info
Channel: Brackeys
Views: 819,544
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, navmesh, navigation, mesh, ai, artificial, intelligence, pathfinding, path, agent, new, high, level, components, component, generate, move, find, shortest, navigate, intro
Id: CHV1ymlw-P8
Channel Id: undefined
Length: 11min 37sec (697 seconds)
Published: Wed Mar 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.