Unity NavMesh Tutorial - Animated Character

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous video we learned how to make our navmesh change at runtime in this video we'll learn how to use links to connect together multiple navmesh surfaces we'll also finish things off by implementing an animated character to replace our cylinder also this video is sponsored by unity so let's jump right into it so as you can see here I have a simple scene with two ground elements both of these are just simple cube x' and I've gone ahead and named one ground one and the other one ground two and if I go to my nav mesh here I can hit bake and you can see that both surfaces are walkable to our agent but there's of course a gap between them so if we were to select our player now we can see that we have the nav mesh agent and I've gone ahead and added to play a controller that we created in the last video and if we play we can move around one of the ground planes but there's no way for us to go to the other one to change this we can use a nav mesh link to connect them together we'll go to our hierarchy right click and hit create empty we'll reset the transform and let's rename it to link let's also add a new component and we want to add a nav mesh link and if we now assume in we can see this illustration of two arrows connecting two points we can of course control where we want these points to be I'm just gonna take the entire object and hold down control while rotating to make it span from one surface to the other I'm also going to go ahead and move it up so that these rings here are placed right on top of the surface and this is pretty crazy but that's pretty much all we need to do if we now go into our game and hit play we can now click on the opposing surface and our nav mesh will use this link to transition from one to the other you will notice that if we click over here our agent has to go through the link in order to reach that position if you look closely you can see that the agent speeds up when using the link this is the agent exhibiting jump like behavior when creating a nav mesh link we aren't creating a surface instead think of it more like a teleporter where when we meet the start of the link it will simply quickly move the agent to the other side of the link there also some different properties that we can modify on michl Inc most importantly we have the width this allows us to make our link wider so that our player can use it over a greater area if for example we wanted to make pretty much this entire gap into a link we would just increase the width in order to do that and if I now hit play and tell a player to move you can see that he just uses the shortest path there we can also choose whether or not we want the link to be bi-directional if we disable this our player will only be able to use the link one way in our case we'll be able to walk across here but we won't be able to transition back and then as with the other nav mesh components we can also choose a area type for this link but right now we're just using a link to connect between two parts of the same nav mesh we can actually use things to connect between multiple nav mesh surfaces an instance of where this is particularly useful is if you are loading in your level in chunks and you want each part of your level to have a separate nav mesh well then every time you load in a new part of your level you can simply connect it together with the old one using links and everything should work just fine so if we go ahead and select our nav mesh here we can actually duplicate it I'm gonna name the first one nav mesh one and the second one I'm going to drag to the top and named nav mesh two and then gonna select my first ground here and add it to a layer this one is going to be ground one and I'm also going to add another layer which is ground two then if we go to our ground one we need to assign ground one here and for ground two will assign ground two if we then select our first nav mesh we can make sure to not include ground - and we'll hit bake so now this only includes the first ground and for our second nav mesh we can make sure to not include ground one and hit big on this as well and now this only includes our second ground so if I disable one of these we can see that they are now on two different nav mesh surfaces and if we hit play you'll see that our link still works just fine between them so that just opens up a whole another world of possibilities and now that we have a great amount of control over our nav mesh it's time to replace this boring looking character with a coal animated one to do that I'm going to open up example 5 here in here I just have a simple level I'm gonna select my navmesh and hit bake and on this level I have the player-character with pretty much all the same settings as before as well as the player controller and we of course want to get rid of this boring cylinder to do that we'll go to the top here and we'll right-click and remove the mesh renderer we'll also remove the capsule Collider as well as the mesh filter so now our player is pretty much invisible instead we want to add in a character called Loman as always if you want to get these example scenes as well as the character that we're going to be using visit the link in the description so if we go under example 5 Loman there's a folder here called Loman in here we have some different folders for our character we're gonna go under models and we're gonna select lilo body now if we select it we can see our simple low poly character try this to our player we'll simply drag it under all player object and right away we're gonna see it in our scene we won't be applying any animation just yet so I'm gonna right click on the animator and remove it and now if we go into our scene view and hit F to focus on him we can make sure to position all player correctly I'm just gonna reset the transform so that he's completely centered on our object and then gonna go under all player and I'm gonna go to our nav mesh agent and remove the base offset so I'm gonna set that to zero and that should snap our Collider right in place and then we can go ahead and place him on the ground here I also want to make our graphics a tiny bit bigger so I'm gonna select little body change to the scaling tool by hitting R and just kill him up a tad I'm also going to go and do the materials and assign the low body material to him so now if we go and he'll play we will be able to move him around our scene just as before but of course it's very boring when he doesn't have any animation applied so how do we go about this well so far we've been using some simple graphics to illustrate our player and whenever we've moved the player in a given direction we haven't been changing those graphics at all and that's just fine when using a simple shape but now that we're using a character we want him to animate as he moves forward and more importantly with one this animation to line up with the way that he's moving in order for this sort of animation to the natural we wanted to look like the characters feet are the one that is driving forward the motion in other words we have to always make sure to synch up our animation with the actual movement in most cases there are two ways to go about this the first one is moving the character using the nav mesh agent this is not far from what we've been doing so far we have our nav mesh agent it wants to go in a certain direction and so it goes ahead and moves in that direction then for any given point we could simply overlay some animation on top of this movement and then do our best to try and synchronize the animation to the movement the way that we'd probably do this is by asking for each point what the current speed of the agent is and then adjusting the animation speed and what animation to play depending on if the character is moving quickly in which case we want to run if he's moving slowly in which case we want to walk and if he's not moving at all in which case we just wanna go and play some kind of idle animation and this is fine for very simple animation but things quickly become more complex because our character is not only moving forward he's also turning to the sides and this should be shown in the animation and it also requires a lot of work to try and synchronize the two perfectly the second way to go about it is using animation to move forward our character here we have the agent running in the background so for any given point we are still running the nav mesh agent but instead of just having it move forward our character for each point we're asking where should we go and we then play an animation that moves us in that direction and so here the animation itself is what drives the player forward now this method also requires some tweaking and quite a bit of code but luckily unity recognizes this and have created a script that does exactly this the script I'm talking about is the third person character script it's part of the standard assets and I've also included it in the example scene if we go ahead and add this to our player it's going to add a bunch of different components but most of these we can pretty much leave as is as for the third person character script itself I found that the fault values for just fine so I'm just gonna collapse that the same thing goes for the rigidbody and as for the capsule Collider we just need to make sure to fit this to a character I'm gonna increase the height to two and move it up by one that's all we need to do here now we can go to a nav mesh agent and change the speed to one and we can pretty much leave all the other settings one thing that we do have to set up is our animator and this is because we need to specify what animation we want to use as well as how this animation should be applied to the bones first of all we need to apply an animator controller if we go under Lomond animation we can see the humanoid controller right here we'll go and drag that in and as for the Avatar we want to be using the Loman avatar so we'll go under little body select the little body avatar and drag that in as well and now a player is actually set up to use animation if we select him and open up our animated window we can see how all the different animations correspond to each other we have a bunch of different animations in here like a death animation you win animation and animation when airborne and when crouching and under the locomotion we have a bunch of different animations for movement in the left hand corner we have some different parameters and the cool thing is that our third person character script will go ahead and adjust all these animations accordingly depending on the situation so pretty much all we need to do is start using our third person character script to do that we'll open up our player controller and here we want to add a tiny bit of code the first thing that we want is a reference to a third person character and whenever we're using something from the standard assets we need to go to the top and say using unity standard assets and in our case we want to go under your characters and use the third person character so now we can create a new variable a public third person character and we'll just call it character then we also want to create a start method so we'll go void start and in here we simply want to tell our agent not to update its rotation this is because our rotation is handled by the character so we go agent daaad update rotation equals false and now we're pretty much set up now we simply need to move our character through the third person character script to do this we'll go to the bottom of our update method and to move our character will use character dot move and this takes in a vector three that defines where we want to move and whether or not we want to crouch or jump and as we talked about where we want to move is determined by our agent and here we can use a super handy parameter under our agent called desired velocity as the name suggests the side velocity is where our agent would like to move at this given point so we'll input agent dot desired velocity and then we'll write false because we don't want to crouch and false because we don't want to jump now of course there's no reason for us to move if we've already reached the end of our path so we're gonna put an if statement here saying if agent dot remaining distance and as we can see here the remaining distance is the distance between the agent's position and the destination on the current path so in the case that our remaining distance is greater than agent dot stopping distance and we set this to point two inside of the inspector well then we still want to move so in that case we still want to call characters move and if not meaning that our agent has reached its final destination along the path well then we want to stop our character from moving and this is maybe a tiny bit counterintuitive but in order to stop our character we actually do have to call move but instead of giving it an actual movement we simply feed it vector 3.0 and so we say stop moving and again we want to input false and false and that's pretty much all we need to do in the case that we still want to move and haven't reached our destination we'll go ahead and move according to the desired velocity of our agent and if we then reach our destination we'll go ahead and stop moving it's actually that simple if we then save and go into unity we now have an empty field for a third person character we'll go ahead and drag that right in and now when we hit a we should see that right away our character transitions to an idle animation and if we start moving our character will realistically animate to the desired position notice how when the character is not moving very quickly he's only using a walk animation if he's moving more quickly he transitions to a run and whenever he's running around corners he will go ahead and tilt his body to show this we can also take our animator and move it to the right here and this will actually allow us to see what animations are currently playing so we can see how we're transitioning our turn and forward motion in order to play the correct animation so from here it's just up to you to have fun and get creative that's pretty much it for this video and this navmesh series if you want to learn more about navmesh or if you have any questions I suggest you check out the documentation that will of course be a link for that in the description now we have plenty of more unity videos coming so make sure to subscribe to get notified once they out on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in February and especially thanks to encode We Trust Patrick McKinley Diego Frank Tristan town dying guy and Buffy o infinity PBR yo Rio myrrh and soft tune sighs mommy Turkey's Kirk fatal Murphy beard I double tap 45 James P J delay Superman the great giant burger guard Dennis Sullivan Jason Latino Alex wicked ski beyond Farooq knob sweet lean smile enough Jin Sasha hafsteinn Sunni Arabs and Gregory Pierce James Rogers Robert Boone Derman Rob fan and Erasmus you guys Rock
Info
Channel: Brackeys
Views: 250,762
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, model, models, beginner, easy, how, to, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, navmesh, navigation, mesh, ai, pathfinding, path, agent, new, high, level, components, surface, generate, move, find, shortest, navigate, intro, link, links, animated, character, animation, player, animator
Id: blPglabGueM
Channel Id: undefined
Length: 15min 0sec (900 seconds)
Published: Wed Mar 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.