I’m using an animation which I haven’t created.
As you can see here, the animation makes the model drift to the right over time:
In my game, the model is steered by a MeshNavAgent and root motion. I’ve set the MeshNavAgent’s speed to 0.001 so that the motion comes to 99.99% from the root motion of the animation.
It works fine, but the NavMeshAgent seems to take the rotation (and - to my observations - also the z position) from the animation, so it rotates my model more and more to the right.
Here is a video:
I was thinking that I should just disable the z-rotation and z-position of the animation.
However, I haven’t found any working code that would let me do that.
My code approach is this (it’s a script that is attached to the NavMeshAgent):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Agent : MonoBehaviour
{
public Transform target;
private NavMeshAgent _agent;
private Animator _animator;
// Use this for initialization
void Start()
{
_animator = GetComponent<Animator>();
_agent = GetComponent<NavMeshAgent>();
_agent.updateRotation = true;
}
// Update is called once per frame
void Update()
{
_agent.SetDestination(target.position);
}
}
You could disable the root motion on the character in the animator for the character and then control it’s root motion with its transform via code. Or you could take the FBX out into a proper animation program and edit the curves of the root joint animation to just have it transform directly forward in Z.
@twda You know what, now that I look at that animation I see that it has an uneven, lurching movement. So there are a few ways that you may be able to deal with that. What I said before about turning off root motion is wrong. Sorry, instead keep root motion on but in the inspector for the asset that has the animation, go to the animation tab and at the bottom you will see a menu item called “motion”. Under that will be the joint you are deriving root motion from called whatever the root motion bone in that rig is called (probably root). Switch that dropdown to “none”. Then apply. Once you do that it will populate a more complex system for extracting just the movement you want. You can specify which root motion channels you want to be factored. Turn off X and just get root motion from Z. That should fix it.