Sync Animator and NavMeshAgent States | AI Series Part 3 | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey chris here from mom academy here to help you yes you make your game dead dreams become a reality today's the third part of the ai series and i'm really excited because we get to add animations to our nav mesh agents we're going to bring in a 3d model from the unity asset store a free one so everyone can use it it comes with animations and all that kind of stuff so we're going to set up our own controller with the animations we want to use and hook those up to the nat mesh agent state those two ways that you can drive animations with a nav mesh agent the one we're going to be covering today we take the navmesh agent state and use that to set the animator state you can do it the other way but i find this way is easier but it does come with a drawback that if your animation speed is not in sync with your nav mesh agent speed your feet will kind of slide so that's one thing to be aware of i do find this way is more simple to get set up and to get working since this is part three if you haven't seen part one and two go back check them out there's a card up here i hope i'm pointing to the right side this time that sends you back to the previous one because that's where we set up the scene that we're going to use where we added the nav mesh links where we set up the nav mesh agents and where we set up the nav mesh surfaces so please if you if you don't know what any of that is go back check them out and as always this code is available on github everything that i've done here minus the uni chan model you have to import that yourself is in the project there's part one part two part three all in separate project folders and each of those project folders is the end state of this tutorial so if you open up the third folder that'll set you up with the end of this tutorial so you can see exactly where we are if you open up the second folder you can follow along with what we're going to do on this one let's get started since i don't have a 3d model to give you what we're going to use is the unitychan model from the asset store so you just open up the asset store search for a unity chan and get this free one that'll get you the exact same thing that we're using here it comes with animations it comes with a model and some other cool stuff so we're just going to use this one find the yunichan dynamic model and we'll bring that into our scene put it as a child of the player and then we can get rid of their old models we'll remove the mesh render and the mesh filter from the player we'll reset the scale to be 111 instead of 0.51 0.5 because the unitychan model has the pivot point set at her feet and not the center of her model like the capsule did we'll change the base offset of the navmesh agent to be zero we'll remove a bunch of the scripts that don't do anything for us we'll just keep the spring manager and the random wind and we'll create our own animator controller so we have only the animations that we care about and we'll name that player controller if we again go into the unitychan package folder structure we can find art animations and there's a bunch of animations here the ones we're going to use are jump weight 0 0 and run 0 0 f we'll set it up where the idle is the default state the idle can go to run we can go from run back to idle and we'll make any state go to jump so that way it's easier to manage that whenever we make our controller more complex however where the jump can go to run and walk to manage state transitions we'll add a boolean parameter that we'll call is moving and we'll make a trigger called jump we'll add the conditions to these transitions the any state will go to jump when the trigger jump happens jump will go to run or idle based on the is moving parameter weight will go to run when is moving is true and run will go to weight when is moving is false then if we select the unichan dynamic prefab under the player we'll change the controller to be our new player controller now let's set up the player movement script to control the animator based on the navmesh agent state since we're trying to control an animator i will add a private animator and i'll call it animator and i'll serialize that field so i can assign it in the inspector then in the update function i'll do animator.set bool and here i'm going to use the string is walking which you might notice is not what i called the parameter in the animator i'm going to go back and change the is moving parameter to is walking it's important that these align otherwise you'll get errors saying that the parameter name that you're trying to use doesn't exist i'll add two private const strings one is walking and one jump so i can refer to them in my update function and i'll set that as walking bool to be if the agent velocity the magnitude of that is greater than zero you can't just put zero though the velocity magnitude is always slightly greater than zero so i'm putting 0.01 f here instead but what about the jump our player movement script doesn't know anything about whether the agent is jumping or not that's managing the agent link mover so we'll open the agent link mover and we're going to call back whenever the agent starts trying to jump and whenever they complete a jump and we'll do that by adding a public delegate void link event and i'll create two link event fields one called on link start and one called on link end and what we'll do in the main start code routine we'll call on link start if the agent is on that mesh link and then after any of the code routines done and we complete the off mesh link we will call on link end if you're not familiar with this question mark dot syntax that allows us to in line check if online start is not equal to null and then invoke it if we go back to the player movement script we will require the type of agent link mover and we'll assign that on awake just as we did the nav mesh agent with link mover equals git component agent link mover then we'll add the delegate function on link start to be handle link start and in there we'll just set the animator trigger to jump actually what we'll do is we'll add a new trigger called landed and we'll set that on link end just so that way we show the whole thing on link start on link end since we're using the same animator controller for both the player and the enemy we're gonna have a lot of copy paste code here to manage the animator in the same way so i'll copy both of these delegate functions the handle link start and handle link end i'll copy all those constants as well i'll add that the enemy movement also requires a type of agent link mover and assign that on awake as well with the private agent link mover link mover and then assigning that on awake with link mover equals git component agent link mover then also on awake we'll assign those to delegate functions on link start and on link end [Music] we'll also add the reference to the animator with private animator animator and we'll serialize that field and the last thing to do before we can start testing and hooking it up in the inspector is on update we'll assign the is walking parameter to the animator based on the exact same condition if agent velocity magnitude is greater than 0.01 f if we head back over to the unity editor select our player object we'll hook up the animator the animators on the unity channel dynamic game object so we'll hook that up by dragging the unique and dynamic to our player's player movement script and the same thing for the enemy we'll also come back and add the landed trigger to the animator and we'll rename the parameter is moving to is walking since that's the string we're using in our scripts let's clean up the transitions a little bit jump probably doesn't need to go to idle at all since the agent will always be traversing a nav mesh link whenever they're doing a jump meaning they will always be moving in the transition back to run i will make that happen only after we've received the landed trigger instead of based on some time since we've configured our player correctly and our enemy is going to behave almost the exact same way let's copy paste our unity chain dynamic model parent it under the enemy we'll select the enemy we'll reset the transform remove the mesh render and mesh filter from the enemy and reconfigure the nav mesh agent to have the base offset of zero [Music] and the capsule collider to be centered at position y1 if i then click play and make our player move around we'll see she jumps lands and then goes to idle of course this is only the basic implementation of some animations with navmesh agents we didn't cover multi-states we didn't cover blend trees anything like that there's a lot more that can go into this but this will get you started with your animations on your navmesh [Music] agent i hope you got a lot of value out of this video and i hope you understand how to tie the navmesh asian state to the animator state in the next video what we're going to do is start dynamically spawning enemies into our scene it's really cool and i can't wait to share that with you if you have any questions if you have a suggestion for a topic or if you've implemented this into your game as a result of watching this video drop a comment down below and i'll see you on the next video
Info
Channel: LlamAcademy
Views: 19,407
Rating: undefined out of 5
Keywords: Unity, Tutorial, How to, How to unity, unity how to, gamedev, gamedevelopment, game development, llamacademy, animate navmeshagent, animation, animations, navmeshagent animations, how to animate navmeshagents, unity how to animate navmeshagents, unity navmesh agent, unity navmeshagent, navmeshagent, unity animate navmesh agent, unity animate navmeshagent, animate, llama academy
Id: wLZPM46zgUo
Channel Id: undefined
Length: 10min 0sec (600 seconds)
Published: Tue Feb 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.