Create a navmesh at runtime in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I want to take a look at creating a navmesh surfaces dynamically so as you're seeing changes I want the nav mesh to actually update to allow AI characters to follow you now the example here I've created three floors that are separated from each other so there's slight gaps and I've created the bridge that can be switched on between these two surfaces I then want the nav mesh to actually span over that bridge to allow the AI character to follow me that bridge will be switched on by this red button on this one where I've got a slight Gap my player will be able to kind of jump across I also want the enemy to be able to jump across as well so that's what we're going to be setting up in this scene okay so the floors are just simple uh cubes that I've sized the bridge once again is just a simple Cube okay the enemy is a capsule that has a capsule collider and a nav mesh agent I've also got a pre-written script for the AI so basically we'll just Target the player set the destination to the player position so I can now drag the AI script directly onto the enemy and the player I'll just manually move that with my in this scene so we can see that and for the actual button um oh actually first make sure that the player is tagged as player for the script to work for the button I want to make sure that this has a trigger collider so that I can walk into it I also need to give it a rigid body component made like kinematics and it doesn't fall so that I can interact with this now what I want is that whenever I press the button I want the bridge to switch on and I want the nav mesh surface to update so I've got a pre-written script for this in the button script and the on trigger enter is used in order to switch the bridge on and as long as we're using Unity engine AI I can then create a new variable of navmesh surface I've just simply called my nav surface you can call yours whatever you like and all you have to do is say nav surface that's that name dot build nabmesh and that's the command that will actually rebuild any kind of a nav mesh so you can do it dynamically now I wouldn't do that every frame as it will diminish your frames per second I would generally do this whenever you add new items and now chances are my enemy and player may very well uh create like little holes in the nav mesh so make sure that we give these nav mesh modifiers and make sure that they remove this object from the nav mesh same on the player also same on the button as well oh I've given that a volume don't need the volume I need the nav mesh modifier and remove that object okay so for the button I'm going to drag in this button script script and it's expecting a bridge which is currently switched off so that that can switch that on and it's expecting a nav mesh surface okay so to create that it's game object Ai and the nav mesh surface okay from here um if I check my AI navigation I've made my radius of my humanoid pretty small because this is a relatively small scene so 0.3 is the radius for this character okay so that now I can simply just bake and you can see there what I've got okay so that's the nav mesh surface as I get closer should be fine um if I switch this off there is no nav mesh underneath that's fine but you will see that when I switch the bridge on the bridge does not have any kind of a connection okay so under the button I'm gonna now drag that nav mesh surface directly into there I also want the ability for the enemy to actually jump across this Gap now if we go to game object AI we have a nav mesh link okay as soon as I add this we'll see that we've got this little icon of a little line This Is Well it describes how far the actual character can jump and in what direction so let's just bring this up as you can see it's got a little icon of a bridge on there um and I want to rotate this so it's facing this way make sure it is kind of lined up with the surface and what I want to do there is reduce the start point and the end point to get it roughly to the right size so something like this should be fine as long as it's actually touching one of those blue areas that will now allow my AI character to actually jump that small Gap okay and that's fine um area type walkable I could for example change that to jump which is fine okay so let's uh actually test this out now then so first of all in my game I'll get my main camera set up a line with view so I can see that um so I'm now going to press play jump over to the scene View and my player ah now if I check my enemy I forgot to put the player into the slot there let me do that if I try it again there we go okay the enemy has instantly found my player so wherever I moved player is moving now I'm going to go and switch on the bridge as soon as I switch on the bridge did you notice how the nav mesh re-baked and now that character can follow me come over here the character has to try and go across the bridge now when I come over to this side if I go all the way up here notice that the AI character is able to jump across excellent okay so that's how you dynamically create a nav mesh at runtime and now you can add these little jump areas for your AI characters hopefully you enjoyed the video if you did why not subscribe thanks for watching
Info
Channel: Creative Media Tutorials
Views: 5,443
Rating: undefined out of 5
Keywords: AI, Navmesh, AINavmesh, ai, unity, Unity, new AI navmesh, runtime, create a navmesh at runtime in unity, off mesh link, jump for navmesh, AI jump
Id: bELFjNNOfn0
Channel Id: undefined
Length: 7min 10sec (430 seconds)
Published: Sun Aug 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.