- Home /
NavMeshAgent With Mecanim
Hi...What i want to do is to create a Mecanim Controller which is driving by a NavMeshAgent AI... and sending both speed and angle from the AI to the Mecanim Controller.... i have tried the learning project "Stealth" to make the script...but when the player is near the object that i want it to follow.... the player will bounce from x to z random points then stops.... this also happens when the object gets a new longer distance and the player starts bouncing again and move to the object...any idea?
Sounds like a scripting problem, can you post your code?
Answer by infinitypbr · Nov 29, 2013 at 08:00 PM
http://docs.unity3d.com/Documentation/ScriptReference/NavMeshAgent-desiredVelocity.html
Don't let the NavMeshAgent control movement. Instead pass this variable to mecanim to allow root motion to work/move the object.
Here, i use:
void OnAnimator$$anonymous$$ove () {
anim.rootPosition = navAgent.desiredVelocity;
}
Never used OnAnimator$$anonymous$$ove myself, but it looks like that's post-movement, and if I'm not mistaken, you're trying to tell $$anonymous$$ecanim how fast to move based on the Nav$$anonymous$$eshAgent?
So when, for instance, player is within the range of the enemy, enemy's Nav$$anonymous$$esh is turned to active, and then the desiredVelocity is sent to mecanim as a float variable (likely called "velocity" if you follow the standard conventions).
Root motion takes hold from that, moving your character forward.
Here's some code I use -- you'll have to fill in the blanks, but this runs if the enemy is supposed to be moving, and gently increases the velocity.
[Note: desiredDistance
does not refer to physical distance, but rather float based on the desiredVelocity
, which itself is a Vector3 and can't be passed into $$anonymous$$ecanim]
var desiredDistance = Vector3.Distance(charNav.desiredVelocity, Vector3(0,0,0));
currentVelocity = $$anonymous$$athf.Lerp(lastVelocity, desiredDistance, (Time.deltaTime * 2));
lastVelocity = currentVelocity;
charAnimator.SetFloat("velocity", lastVelocity); // Pass currentVelocity to Animator
But i want the Animations to set the velocity of the AI.. what if i have some sort of a zombie walk? when speed is like : 1>>0.1>>0.4>>1>>0 ... all in the root motion... i want the Nav$$anonymous$$esh to be driven by the Root$$anonymous$$otion.
Has anyone figured out a way to do this yet? I have been banging my head for days trying to get animations to control velocity with mechanim and navmesh.
Answer by Aranhawebs · Nov 25, 2013 at 10:09 PM
That happened to me too, i guess is because the animation, that moves forward on the axis, so the character goes straight from the point, and try to go back and goes straight several times.
If you disable the animation, and let the agent handle the movement and rotation, this doesn't happen. I'm trying to figure out the best way to use the agent with the mecanism too, if you know something let me know. =)
that's right.....pm me if you got anything new too
Answer by Robacks · Mar 18, 2014 at 06:17 PM
Or try to let the NavMeshAgend controll the position but dont apply root motion from your animations.
var animator = GetComponent<Animator>();
animator.applyRootMotion = false;
You may also have a look into your AnimationClips you are using and, set each Root Transformation setting to use "Original" positions from your animations. See here.
Doing so for my animations had solved this kind of trouble.
(A little bit late but it might help someone else ;) )
Your answer
![](https://koobas.hobune.stream/wayback/20220613122501im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How would I use Mecanim to holster or draw a weapon? (C#) 1 Answer
Why isn't my enemy pausing in between waypoints? C# 1 Answer
How do I limit the path of a NavMeshAgent? 0 Answers
Animator component prevents NavMeshAgent from disabling/enabling dynamically 0 Answers
My NavMeshAgent doesn't move at all!,Why my NavMeshAgent doesn't move at all? 0 Answers