Combining Unity NavMesh with Humanoid Animation
ฝัง
- เผยแพร่เมื่อ 26 ส.ค. 2024
- Bring your humanoid characters to life by combining Unity's NavMesh navigation with Mecanim animation. Combining these can be tricky, and this video show you how to do it to create autonomous, AI-driven animated humanoid NPCs for your games and simulations.
These pages from the Unity Manual are mentioned:
- Using NavMesh Agent with Other Components - docs.unity3d.c...
- Coupling Animation with Navigation - docs.unity3d.c... (also shows how to make the Animator Controller)
The video uses the POLYGON City Pack from the Unity Asset Store: assetstore.uni...
This video is #6 in the series "Humanoid AI in Unity."
• Humanoid AI in Unity
See the Clockworks Games website at clockworks-game...
Subscribe to the Clockworks Games Facebook group: / gamesfromtheclockworks
#unityai #gamedevai #unitynavmesh - วิทยาศาสตร์และเทคโนโลยี
Wow, amazing! I was looking for an example of navmesh + animation without jittering, and found it here. Animations are smooth, especially when added more animations to blend tree. please keep doing awesome content like this
Thank you for the nice feedback, and I'm glad the video was helpful. More videos are in the works. :)
When I first got into unity I really thought it was gonna be like Legos. Ah, poor naive slightly (6 months) younger me. Been digging for a while to find videos describing the process of merging various pieces of things, and I think this may be the only one on youtube, so thank you for making this. Even if it's not for these exact assets, it gives me an idea of what's needed, and I can play around from there.
That said, if anyone who sees this comment wants to chime in with other great sources of info on the subject, I'd appreciate it greatly. Lol. So many great packages, and so many things, but man, it sure seems like combining parts for a different car. Sure, a Honda fuel pump will work in a Ford, but it takes a loooot of hacking at it.
Thanks for this comment, and I agree there is a lot of expert knowledge required to make all the parts work together!
Oh man, I spent WEEKS trying to do this, UNTIL I came across your videos! You've saved me TONS of hours of experiments! Thank you so VERY much for your channel! Subscribed & like! Thanks again Sir!!!
I'm glad that the video was helpful!
This is exactly what I needed -- I was having trouble with "foot sliding."
Glad it was helpful!
Found this video today after struggling with animations for weeks. I'm so glad I found this because now, I'm going to hopefully be able to finish my game.
Glad it helped, and good luck with your game!
@@ClockworksGames Thanks a lot. I subscribed. I look forward to seeing more of your content.
@@ClockworksGames One question. I can't seem to get a hold of the Standard Assets animations. Would you say that I can substitute the animations with animations from Mixamo?
@@CodemasterJamal Are you able to get the assets here? assetstore.unity.com/packages/essentials/asset-packs/standard-assets-for-unity-2018-4-32351
@@ClockworksGames I found them in the link you provided. Some of the links have changed, which is why I was confused but, I found the assets eventually.
AMAZING video this is just what I was looking for.
I have been researching this problem for 2 days now
I'm glad the video was helpful!
This is awesome! Thank you!
Glad it was helpful!
I didn't need all the controls to move a char in real time because I'm making a tower defense and have enemy AI characters heading toward a goal point. I found a much easier way to do what I needed. I just wanted the animation to move the enemy toward a set goal point so that it would move at the speed of the animation itself rather than being "pushed" along by the navmeshagent comoponent. To accomplish this, I set agent.updatePosition = false and turned root motion on for the walk animation clip. All I needed was an if condition to check when the enemy position was at the goal point, in which case I set the animation controller enabled to false to make the enemy stop. It's that simple.
Thanks for sharing this approach! Do you have any foot sliding in your animation?
@@ClockworksGames In this approach, the forward pace is driven by the root motion of the walk/run clip, so there is no foot sliding. I did get my simplified approach to work under test circumstances, but it turns out that it's not the right way because the navmesh still controls the rotational steering while the forward motion is driven by the root motion of the animation. What can happen is the character will seem to get lost if the navmesh component is not keeping perfect pace with the character. For now I'm using a multiplier for the navmesh speed so that it can provide correct timing of the rotational steering. I'll probably implement the "recommended" approach later in my project.
@@hplovecraftmacncheese Thanks for describing your approach. There are many ways to do all these things, and it often takes experimentation to find out what will work.
Thank you for this! I could not figure out what simple dumb thing I was missing and you nailed all the little things I need. Awesome video!
I'm glad the video was helpful!
Hey I did it in a similar way myself. Very helpful video for beginners. Kudos
I'm glad the video was helpful!
It works great! Thank you!
Glad it helped!
What's the point? This hero movement (is sliding) because its not based on root motion (animation not sync with move).
Root motion:
nav.nextPosition = transform.position;
The complexity here is that both the animation system and the navigation system are trying to change the position of the agent, as explained in the video. In OnAnimatorMove() I actually have the reverse of your suggestion to keep the NavMeshAgent and the animated agent at the same position. What I show in the video worked best after a bunch of experiments that I tried, but there may be other good solutions to this problem as well.
This is exactly what I was looking for. Thanks!
Glad that the video was helpful!
Amazing. It is really helpful for me :)
Thank you for the comment!
Sir, thanks for sharing your knowledge.
Thank you for your nice comment! It is my pleasure.
Nice video! worth a sub.
Thanks for the feedback and for the sub!
Great video. This is super helpful!
Thanks, I'm glad the video was helpful!
Amazing tutorial. I was just wondering if you're gonna make any future tutorials on Enemy State machine behaviors such as chasing, attacking with the root motion system.
Thanks very much for the nice feedback! I have a roadmap of upcoming videos including NPC / enemy behaviors, senses of different kinds, and associating reactions with senses. I will add some discussion of state machines as well, which will certainly be required.
You see so nice😄
This is very helpful, I am a little confused as to the transform.position = agent.nextPosition bit (I understand what it stops the agent doing) but doesn't it just negate the rootmotion? the character will move as if the it just had the basic nav agent movement?
Thanks for the question. The main movement of the agent is driven by the animation clips in the Update() function. The line that you mentioned, called in OnAnimatorMove() just provides a small correction because the NavMesh agent and the character model would gradually drift apart otherwise.
@@ClockworksGames I believe Cunnah is correct here. agent.updatePosition = false; only stops the transform from being updated by the agent automatically, but the nextPosition will keep simulating to its destination. You correctly use the agent properties to derive animation parameters and let RootMotion take control of the player transform. You also correctly state that this would result in the agent's nextPosition deviating from the animation's derived transform position. When you implement OnAnimatorMove the animation will no longer update the transform directly, so the idea here is to use your animation's positionDelta as input for moving the transform manually with contraints. Your constraint here is just to set the transform back to the agent's nextPosition, bypassing root motion all-together. What you actually want to do is use the animation's positionDelta and override the agent's nextPosition = calculatedPosition (which will apply navmesh boundaries), then you can set the transform position to the agent's nextPosition. The result is you used the initial agent's velocity to set root motion parameters, and root motion velocity to then update the agent's position, then setting the transform to the new agent position to sync them.
@@robu4005 Thanks for this comment. I have been trying to understand what you said in detail. Do you think something in my approach is not correct or, if correct, could have been done better? I am not surprised if there is yet a better approach. As I mentioned in my video, I tried a lot of things, and what I showed is what seemed to have the best results. But there may certainly be opportunities to improve on it. I would like to understand the details of what you are saying. Thanks again!
For a crouch idle and crouch walking animation movement, do we need to create a separate blend tree like the 'Move' or will it be in the same tree?
For crouching movement, I would create a separate blend tree in the same animator controller.
sub sir that what i was looking for ty
Glad that it could help!
Thank you!
You're welcome!
Great tutorial! I was having same issues with the jittering, but after using your example it went away! Thanks a lot. I suspect it has something to do with the way the unity example handles frame smoothing, can anyone confirm / deny? Anyways GJ, your code works better than the unity docs.
Thanks for the feedback! I'm glad the code worked for you!
I used a couple of animations from Mixamo. Weirdly when I automatically calculated the blend values it produced the wrong values. The animation actually moves the character much faster then the animator blend values suggest. I manually figured out the velocities and used those and it works correctly. Whats up with that?
I haven't tried Mixamo animations but would like to understand this better. Can you tell me specifically what animation clips you used?
@@ClockworksGames Sure thing, as I cannot link you the exact animation it is called "Rifle Run" and it is the version where the character is not pointing the rile ahead. They are holding it down and it is moving back and forth. It is the 3rd option when I search for "Rifle Run". Let me know if you want to make sure you have the right one.
@@ChadGatling Ok, I think I've found and downloaded the correct Rifle Run animation. What other animation clips are you blending this with? Are you using Mixamo animations for idle / walk / turn ? (You can also email me at info@clockworks-games.com if you prefer to communicate through direct email.)
Hello, great videos. When will you upload the next part?
Hi, thanks for your comment. I am working on the next video and should have it uploaded this week. The topic will be waypoints and having a set of NPCs wondering through the environment.
Are you using animations with root motion for the move state or are they only moving in place?
That is the thing I cannot figure out, wether I should move by animation or by agent.
The animations do use root motion. The position of the NavMeshAgent is decoupled from the animated character using agent.updatePosition = false. The agent's calculated next position is used to adjust the parameters of the animator controller to drive the animation which moves the character. There is a small tweak in OnAnimatorMove() that prevents the character and the NavMeshAgent from drifting apart. Let me know if this isn't clear.
@@ClockworksGames Thanks! It took me a lot to wrap my head around using root motion when every teacher all my life has advised me against it, but the system works, and the solution is simple, elegant and beautiful. So I have no complains here.
I have one question though. You set up animations to rotate on place in the animator, but these never seem to get triggered in your project or mine. I'm guessing the 'velx' animator parameter should be modified while keeping move=false to achieve this. How would you go about implementing these?
Maybe check in update if the angle between desiredDirection and current direction is bigger than 45 degrees, and then allowing velx to be modified while keeping move = false ?
@@twoboxtoofurious That's a good point. I think what happens may be that a NavMeshAgent may never really turn in place, so that case is not triggered when the character is driven by a NavMeshAgent. I think that under manual / keyboard control, the turn-in-place case should work. Just curious, why do your teachers advise against using root motion?
Great video sir. Thanks a lot. It helps a lot.🙏
Thanks for your comment! I'm glad the video is helpful.
I don't understand what animation to put in the blend tree, I added walking and turn left and turn right but it didnt work
Have you tried the recommended animation clips from the Unity Standard Assets?
@@ClockworksGames No , I just got them off Mixamo. Where could I get unity standard assets from? Sorry in advance if this is a trivial question hahaha
@@michaelgeorge4524 I see that Unity has recently removed their Standard Assets from the Asset Store, unfortunately. I will look for a replacement. Unity has a new "Starter Asset - Third Person Character Controller" which has idle, walk, and run animation clips, but no turning. That could be used, but I will look to see whether there are any free clips available that include turning.
@@brionsarachan4500 yea without a proper set of animations, it doesn't work properly at all. Keep me updated if you have found anything that works please tell me
I was able to get my hands on animations that make it work and just mirrored all the animations to generate turning effects. The bigger problem I have now is that I cannot modulate the speed on the NPC at all. The NPC is constantly running, after applying the code which I know is part of Unity's make the agent follow the animation tutorial, changing agent speed does not do anything anymore. I want to force the patrolling of an area to be slower but it seems to not be possible after making the speed of the agent be animation based somehow
Why this video only got 300 views?
Thanks for your comment! I think there is just so much content on TH-cam it can be challenging to find the audience.
My steps are jittering
If you would like to send me an email at info@clockworks-games.com with more details on what you are doing I will be glad to try to help.
too much sliding