Nothing ever works out of the box. True Story. Here’s how to make Mixamo animations work when you import them into Unity.
TL:DW here’s what you need to do:
- Download and import the FBX animations you want to work with.
- Highlight the lot of them.
- In the inspector, click Rig and then set the Animation Type to Humanoid.
That will be enough to make the animations work. In my opinion, they all need to be renamed as well and for animations that should loop, you need to check that box as well.
Once those animations are in Unity, we still have some work to do with Root Motion vs the Nav Mesh Agent. These guys do not love each other.
Animators and Nav Mesh Agents do not play nicely. Fortunately, it’s not too hard to get them to work together. You need to choose which is going to follow the other. Your artist is going to want you to choose Root Motion. Probably. I don’t know any artists who do animation personally, but you could probably send one of them on the Asset Store an email and find out.
Ok, anyway, here’s what you need to do:
- Make sure you’ve got your Nav Mesh Agent and your Animator on the same Game Object. I had a weird bug earlier today that was solved by making this a rule.
- Make sure your animator is using your model’s Avatar and the Animation Controller you made for it.
- Check the Root Motion box.
The next part is handled by the Zombie Animator script which links up to the Follow Target Script.
In order to keep the Follow Target decoupled from the animator, I created two events that the designer can use to communicate with whatever other components might be interested.
In this case, we want to trigger the Walk animation when we start following a target, and the Idle animation when we stop.
Here’s the code behind the Zombie Animator script:
The red box highlights the line of code that stops the Nav Mesh Agent from moving our character, which prevents the ice skating effect that I showed off in the video. Under it you see that we do want the agent to continue to handle rotating the character.
The blue box highlights a line of code that is important because the Nav Mesh Agent does still need to move. Since we stopped it from moving on its own, we get to do it ourselves.
There’s one last detail. When we stop a Root Motion animation, it will often create a dissonance that causes the Nav Mesh Agent to rotate a bit. This is caused by the Stopping Distance setting of the Nav Mesh Agent which defaults to 0.
Set the Stopping Distance to a value like 0.5 and what this means is that the Agent will be content to call a journey finished as soon as it is within 0.5 units of its target destination.
The value you need to set for the Stopping Distance will vary with your Root Motion. The faster it is, the bigger the value you may need. Now, there is another solution which involves stopping the Nav Mesh Agent.
The Is Stopped property of the Nav Mesh Agent is a Big Red Button. Setting it true stops all function of the Agent cold. No further calls to the Agent will work (allegedly, I haven’t tested this out) until Is Stopped is set false again.