Combining Unity NavMesh with Humanoid Animation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
unity has a very powerful animation system including humanoid animation and a very powerful ai navigation system i highlighted each of these separately in other videos however getting those two systems the animation system and the navigation system to work together can be tricky here is an animated humanoid character that i control with the keyboard here is an ai agent with automatic navigation but obviously just a cylinder we would like to combine the two and have a human character with a i navigation the complication is that each of these tries to control the position of the animated character or the ai agent and both can't be in control at the same time the unity manual mentions this issue even saying that it can cause a race condition if not managed properly and suggests a few approaches to solve it the manual suggests different approaches and variations to those approaches and even provides some example code i tried everything unity suggested and more some approaches worked well and some not so well and by that i mean some approaches were kind of fragile and if parameters were not set just right there would be jitter and generally unacceptable results in this video i will show an approach that is easy to implement and works well in most cases by the way if you find this helpful or interesting or entertaining please do consider liking the video and subscribing to the channel so that you hear about new videos as i make them first i'm going to create an animation controller similar to an example from the unity manual in the description i have links to all the relevant pages from the manual the animation controller will be driven by three parameters move vel x or velocity x and velocity y move is a boolean that indicates whether the character should be moving well the other two parameters are floats and have the small difference in position for the current video frame consider the y-axis as being the forward direction for the character and the x-axis as being the sideways or turning direction with positive values to the right and negative values to the left i will use animation clips available from the unity standard assets i start with an idle clip i then add two clips for turning in place to the left and to the right and set the transitions based on exceeding a small threshold in the vel x value for moving forward i create a blend tree and name that move i set up the transitions between the idle and move states based on the boolean move parameter i set the blunt tree to be 2d freeform directional which is appropriate for movement in the same 2d ground level at different speeds i add animation clips for idle and for walking and running straight and with turns to the left and right i then scale the clips based on speed and angular speed we can then preview the controller and see that the vel x and vel y parameters correctly control the character's speed and turning finally we assign the new controller to our soldier i'm using the same testbed that i've used in past videos which is based on the polygon city pack from the unity asset store in my last video i showed how to add a navigation mesh to the city and added a simple cylinder nav mesh agent that navigates to a point when i click i've now created a new human model for this video using the autodesk character generator so we have this military guy to show how we can drive the animation controller using the keyboard here is a simple c sharp script called keyboard control that simply reads horizontal and vertical values from the keyboard which for example can be from the arrow keys and sends these to the animation controller i'm controlling the character with the arrow keys and this is working fine the next step is to replace the keyboard control with automatic navigation let's remove the manual control script add a nav mesh agent and the move to quick point script which sets the agent's destination to be the point where i click again there are more details on this in my prior videos if we try running this the character moves but with no animation this makes sense actually because we aren't yet driving the animator parameters we need a replacement for the keyboard control script that sends appropriate values to move val x and vel y here's a replacement script that works with navigation instead of taking values from the keyboard the nav mesh agent is used to set the animation parameters in the start function we set the agent's update position property to false this means that the position of the nav mesh agent component is decoupled from the position of the character the two positions can move independently in each video frame the agent calculates its next position but does not automatically move the character there instead we ask the agent what its next position would be and compare that to our current position to get this world delta position which is how the ai agent thinks the character should move in three dimensions we then take the dot products to obtain the components in the forward direction and sideways direction and call that ground delta position we divide that by the time duration of the frame to get the velocity the character should move and if that is greater than a small number and we have not yet arrived at the destination we set the move parameter to true and set the vel x and vel y animator parameters it's important that we decouple the agent's position from the character's position so that the character movement is driven by the animation clip and his feet don't slide on the ground and the movement looks natural there is an issue though that the character's position and the agent components position are not unlock stepped and will drift apart by a small amount in each video frame to correct for that we have an event handler that is called when the animator moves to tweak its position to match the agent's position there are other ways to make this correction i tried a few options and this simple solution usually has the best result so how does it look overall very good there may be a very small amount of foot sliding such as when there is an abrupt turnaround that's not surprising because the animation clips we used are for forward walking and running and turning there aren't clips for backing up and swiveling around so the animation for that situation doesn't quite match the positional change however for forward moving the animation matches the movement well and at least to my eye there is no foot sliding referring again to my last video i had created a navigation mesh where there is a lower cost for walking on sidewalks compared to roads and other areas i provide a destination by clicking my mouse and now we have an animated human character with ai navigation that calculates the best path to the destination and walks in a natural way [Music] in the next video we will move beyond setting the character's destination by clicking somewhere really there should be logic in the game or simulation that drives the activities of many characters we will start with waypoints which is probably the most common approach but let's not stop there when i move through life i don't just have a set of waypoints that i patrol around i will talk about some other ways to set the destination of our ai character i also invite all of you watching this video to think about this if you have ideas for how to set the destination for ai driven characters please add them to the comments below and we can talk about the ideas in a future video if you like this video please do consider liking and subscribing and catch up on the other videos in the series you
Info
Channel: Clockworks Games
Views: 4,938
Rating: undefined out of 5
Keywords: unity, unity3d, game development, NavMesh, Mecanim, unity tutorial 3d, Combining Unity NavMesh with Mecanim Animation, unity ai, pathfinding, unity animation, gamedev, unity 3d, unity tutorial, unity navmesh, ai nav mesh, tutorial navmesh, ai and games, mecanim and navmesh, unity npc, npc unity, game dev, c#, indie game dev, enemy ai, indie game development, unity tutorials, unity game development, npc, artificial intelligence, ai, game development unity, learn unity
Id: _yuAaEbl_ns
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Tue Feb 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.