- Home /
NavMeshAgent using Root Motion for move speed?
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!
Answer by philwinkel · Nov 30, 2014 at 09:21 PM
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
This make the Nav$$anonymous$$eshAgent speed based in the root motion, but now my character is running in circles. I don't know what is wrong. =(
Answer by indieman · Jul 08, 2016 at 11:27 AM
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.
Turning off updatePosition, also stops updating the rotation. Not sure if bug or intended but it's very annoying!
Answer by str3lok · Apr 29, 2016 at 08:33 AM
In block "OnAnimatorMove" it is necessary to change the speed of movement (instead of motion vector):
void OnAnimatorMove()
{
_agent.speed = _animator.deltaPosition / Time.deltaTime;
}
But actually you must rewrite it as:
void OnAnimator$$anonymous$$ove()
{
_agent.speed = (_animator.deltaPosition / Time.deltaTime).magnitude;
}
Answer by Jordan104 · Oct 11, 2015 at 12:01 PM
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.
You can set the speed to 0, problem is that the nav$$anonymous$$esh "velocity" and "desiredVelocity" will be set to (0,0,0) but you still can read out "nextPosition", "steeringTarget", as well as the Nav$$anonymous$$eshPath will all it's points - which you can then use to adjust your animator parameters.
That's an interesting trick. And it 'kinda' works, but this completely breaks NavAgent collision detection :( Seems like there's no way to make NavAgent to avoid collisions with each other using root motion :(
Your answer
Follow this Question
Related Questions
Mecanim: 2D Blend Trees, Unable to Compute Positions. 0 Answers
Mecanim Root Motion -- Not Moving Forward? 1 Answer
switching between Motion matching and animation files 0 Answers
Navmesh orthogonal movement 0 Answers
Root Motion and Navmesh Agent 0 Answers