Unity NavMeshAgent Off Mesh Link with Custom Animations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone today i'm going to be showing you a video on how to have unity nav mesh agents traverse and off mesh link using a custom animation based on the type of off mesh link so what this means is if you have let's say a npc that needs to jump over something climb you know into a window climb over a wall that is possible in unity using the built-in tools so let's look at a quick example of that all right here in my unity scene i have two npcs these are basically simple nav mesh agents they do have some additional scripts attached to them but for this particular case it doesn't really make a difference so in the nav mesh agent you're going to have some some different settings in here and one you're probably familiar with is auto traverse off mesh link so right now i have that enabled on so let's talk a little bit about the navigation mesh first before we jump too far into some of these so if i click over into navigation here in unity there is the navigation mesh that i've already baked into this level so in this level i have a giant plane here that's marked as navigation static so if i click on it and just go into static you'll see navigation static and i also have this 3d model here of a building i have some just custom colliders boxes and things that i've uh just kind of manually placed on there but this uh particular building is also navigation static as well and let's take a look at the settings when you actually click on an object so with the the building selected here or with the plane on the bottom i can go into the object tab under navigation and there's a setting here to generate off mesh links you'll notice that it's disabled so it's not checked for the building as well it is also disabled so at this point we have an agent that we would like to basically jump through this window and chase after our player so our players is right over here in the scene and the uh my nav mesh agents which are the zombies are out here so in order to do this there's a couple of different ways you can do it this is the way i chose to do it so even though in navigation when you look at the off mesh links they're not auto-generated but it doesn't mean that we can't explicitly add one so what i've done is in the building here i have a series of right now i think the game objects are just called cubes uh so they're 3d they're basically cubes so if you right click on it and you go to 3d object and you click cube that's what these are and then the cube itself is basically just scaled down so i put two cubes per window and this means that uh in this case there's two entry points so i'm just using one off mesh link per cube so you can see the component here this off mesh link so you can go in here and you can type in off mesh link this is a unity component it's built in and you have to give it a start and end a cost if it's bi-directional so you can traverse in or out if it's activated uh auto update positions so this is something i'm i'm not currently using and navigation area so if i go back to navigation these uh tabs across the top there's one called areas you'll notice that i have two custom ones right now these are the built-in ones indicated by the word built-in so this one here which is called hurdle is the one that we're going to be using so this is a custom one and then this column here is the cost of the area so you'll notice that jump has a higher cost than a walking area so that'll help drive the nav mesh agent in its decisions when generating its navigation path so hurdle this is one that i just added in here and i just set it to one right now because i'm fine if the agents want to choose to jump over something is the same cost as walking around potentially uh you'll also notice too that in the navigation mesh uh there's a because nav off link uh mesh generation is is uh is turned off for the building you know you'll see this kind of border here and you know you'll see the border on both sides right so this is just you know basically generated from the uh the agent configuration and its radius right so in this case if we look if we zoom in here you could probably see it you can see this little circle here from the navigation mesh uh and then a little arrow and then you know the arrow it just kind of goes through from one point to the other and it points in both directions so this is actually the visual of the nav get uh nav link mesh so if i click on this cube here i'll expand it you'll see there's two empty game objects uh there so there's just a transform and it's on a it's got a start and an end it's just what i named it so these off off mesh links are essentially what gives the mesh the additional behavior versus letting the object itself generate so sometimes leaving it on for the object and generate lots of different off-mesh links and we don't want that behavior we want very well-defined specific links in this case so by just putting this object here basically marking it uh navigation static um and then under under the navigation here so i've got my top level object my cube not start and end selected here i basically have it as navigation static and it is generating off mesh links um and then you can set the navigation area here to hurdle so this is that area that we defined under areas so now that it's selected we just create two additional game objects here a start and an end and then inside of the the cube still in the game component uh we have the off mesh link we assign a start and an end and then you know we set up these parameters and you'll notice here that we we set navigation area to hurdle so what that does is if you look at the navigation mesh here you'll notice this this color this green color here and over here it's blue if i click on areas you'll notice that each area within the navigation mesh has its own mostly distinct color so hurdle in this case is green if i changed it to fall would change to this like magenta type color here the idea is that this is actually showing you the different nav area so what will happen is the agent will move towards it here let's just hit play watch a quick demo so what'll happen is right now my nav mesh agents are aware of my target here and they're they're pathing towards the target if i click on it and i look at the actual nav mesh agent you'll see it's uh it's path being generated and then he'll hop he'll basically move to the start transition to the end and then continue on his way so this is part of it right so if i set up the cube i set the area i set this uh off mesh link component and the start and the end on it and you know i indicate it's it's bi-directional so they'll they'll turn around let me demo that real quick just to kind of prove that that works um so you know we can essentially see that they'll you know they'll navigate and then they'll um you know traverse it in one direction and then traverse it in the other so i'll just take my my player target here i'll let them jump through and then move it back and you'll see he'll jump through the other way so the rotation of the character is basically you know controlled by in this case custom code you can just let the nav mesh agent generate your rotation it's up to you but now that you have the the nav mesh set up and and basically if you're baking it at this point you should see these little circles and you should see the arrow if you don't you know one thing you want to do is you just want to go into your object and just make sure that the 3d surface is mostly in line with this one uh so things like your step height step height here and max slope under the agent setting in the navigation mesh will uh potentially change that so if i took this object and i raised it up let's say this high and then i baked it you'll see that it's no longer considering this part of the mesh because it's outside of the you know the parameters of the step height or the slope so if i move it back and i bake it also something to be aware of is that when i turn off this so that's probably a question you're wondering is i don't really want these to be visible so you can turn them off and and essentially have you know an object that you can move around uh that's not actually rendering anything and it's just um you know you technically don't need the box collider either um so you could technically have these objects turn them on and just be a placeholder in the scene to kind of generate this mesh so this is um just how i'm choosing to do it probably do it other ways all right so let's talk about um how does the agent you know do this actual hurdle right how is it playing the animation what kind of logics involved so if i click on one of the agents here under the nav mesh agent itself now that we've created that new area mask we can come into here and make sure that it's selected so this is now an area that it's considering so if for some reason your agents get stuck at kind of the boundary like even you have this the gap in the mesh but they're not traversing it you probably just want to check that auto traverse is turned on um maybe even auto repath but then the area mask has the the um you know particular area your links are using so now at this point the agent's going to consider that to be a viable path the path is generated and baked into the mesh so let's talk a little bit about how the animation set so right now i basically have an animation called vault uh so this fault animation here it's just a simple animation and the name of its vault um and it plays and then it transitions out so you know essentially if i look in the preview that's you know what it looks like down here so with this bolt animation in place and ready to play um let's talk about triggering it from the code so i am using root motion for my agents now technically the way the root motion works for a nav mesh agent is the nav mesh agent actually controls the simulation and the root motion is synced with it so basically what that means is that root motion is um secondary and then the speed of the navigation agent is actually driving the uh you know that traversal speed within the animation it's it's translation um so the idea here in inside of the code uh so this is my my character uh inside of unity it's my my base character code um the idea here is that on the nav mesh agent itself there's a property called is on off mesh link the off mesh link has a a struct here so you can actually pull current off mesh link data and then what that'll let you do is it'll let you have access to the area so you could you know pre-compute these instead of doing it all the time uh but the idea is it'll it'll essentially allow you to compare to say okay am i on this area so i have a fall one and i have a hurdle or a bolt one um and the idea here is that you know if uh you know if my agent's enabled and it's on the mesh in the area that i'm in is this uh hurdle area then you know go ahead and play and then set this um you know set hurtling to true um and then when he is no longer on and off mesh i just uh re reset these things uh so essentially this is just kind of you know uh recording the state so you don't try to play it you know more than once but um this is essentially what what makes the animation play when you reach that off mesh link um and it's it's basically that easy so one other setting in unity that's pretty interesting here if you don't know about it is when you click on an animation you can add animation behavior so you can see this add behavior button here and you can generate a script this particular one is something i was kind of experimenting with what this allowed me to do was create a simple animation behavior so when the state is entered and exit i can change the speed of the agent so essentially what happens here is when it enters the state it it basically changes the agent's speed to uh like a hard-coded value and then and then after the animation we reset it back to the same speed that it entered the state with um so what this allows me to do is kind of control how quickly the agent can uh you know traverse the threshold so if i go ahead and i just bump that up to 0.8 instead of 0.5 which is what the original value was and then i go back in and i play the scene just as an example you'll see that the agent will traverse uh you know the off mesh link he'll just go from start to end a little faster and sometimes this is uh you know this value is is really good if you have agents that move at different speeds so you'll see he'll go a little faster and in this particular case he'll start clipping the wall because he's traversing too quickly so if you have uh you know based on how quickly the animation is how quick the agent is that's how you can kind of control the agent speed um and then them you know essentially clipping the wall or not um and again the main reason for this and and you know setting the speed here is the fact that the nav mesh agent has auto traverse uh off mesh link enabled so if you disable this you could essentially drive the transform change via code and you you know don't have to let the unity nav mesh agent do it itself in my case i left it on and i chose to just have an animation behavior and just you know tweak that value a little bit until it looked reasonably good all right um that's basically it for the video please like the video and subscribe if you're interested in more topics around game development or c-sharp or javascript i plan to release more videos in the future um also check in the description below i'll include a link to the root motion nav mesh agent video on youtube that i followed i thought it was a great video and it really kind of nails some of the gaps that the unity documentation has so thanks
Info
Channel: Russell Canfield
Views: 5,548
Rating: undefined out of 5
Keywords: unity, game development, unity3d, navmeshagent, remain alive, unity 3d, nav mesh, nav mesh agent, animation state, custom agent, unity ai, unity navmeshagent, unity navmesh agent
Id: Yz1Y6qhHoq8
Channel Id: undefined
Length: 15min 47sec (947 seconds)
Published: Fri Jul 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.