I am using NavMeshAgent to control AI character movement. I’d like to configure it in such a way that the NavMeshAgent controls rotation and turning the character, but the root motion of my movement animations should control the speed of the character moving forward.
Is this possible? Are there any examples anywhere that I could take a look at?
Thanks!
Going to answer my own question in case anyone else runs into this issue, since i’m such a nice person…
Apparently mecanim root motion + NavMeshAgent can be done, there is an example in the Mecanim Examples asset that is on the asset store. In a nutshell you place a script on the same GameObject as the Animator, and that script should have the following method:
void OnAnimatorMove
Inside OnAnimatorMove, you can get the root movement from mecanim root motion and then apply it to the NavMeshAgent’s velocity like this:
void OnAnimatorMove()
{
_agent.velocity = _anim.deltaPosition / Time.deltaTime;
}
So, between Mecanim and NavMeshAgent, a lot is possible, although it takes some scripting and clever use of Mecanim parameters (and StateMachineBehaviours in Unity 5 help a lot).
Look inside the “NavMeshExample” scene of these assets for more examples:
https://www.assetstore.unity3d.com/en/#!/content/5328
In block “OnAnimatorMove” it is necessary to change the speed of movement (instead of motion vector):
void OnAnimatorMove()
{
_agent.speed = _animator.deltaPosition / Time.deltaTime;
}
Assuming you want the movement of your character to be controlled by the animator (root motion), the simplest way is to update the NavMeshAgent’s position to match the character’s position:
void Update()
{
NavMeshAgent.updatePosition = false;
NavMeshAgent.updateRotation = true;
NavMeshAgent.nextPosition = transform.position;
}
The result is that every time your character changes position, the NavMeshAgent is being pulled onto the character’s new position, regardless of the NavMeshAgent’s speed (which should be set to exceed the maximum velocity.magnitude of the animation).
I hope this helps.
I’ve found that if you set the nav mesh agent speed to 0.1 and set apply root motion to true on the animator component that it does the trick.