'NavmeshAgent doesnt update position with the Root animation

I was looking if you can help me with this... I have a nav agent that follows and attacks the player from a distance, and I set animation on the animator controller, that when it takes a certain amount of damage, it plays a strap/dodge animation, as you can see in the gif, it does the animation correctly, but the position, and everything attached to the game object, don't follow the root position.

enter image description here

I use animations from mixamo

This is the code that I use to use the animations properly:

{
Animator anim;
NavMeshAgent agent;
AlienGunnerController alienGunnerController;
Vector2 smoothDeltaPosition = Vector2.zero;
Vector2 velocity = Vector2.zero;
bool alreadystrafe;
bool strafing;
Vector3 moveDirection;

void Start()
{      
    anim = GetComponent<Animator>();
    agent = GetComponent<NavMeshAgent>();
    alienGunnerController = GetComponent<AlienGunnerController>();
    anim.applyRootMotion = true;
    // Don’t update position automatically
    agent.updatePosition = false;
    alreadystrafe = false;
}

void Update()
{
    Vector3 worldDeltaPosition = agent.nextPosition - transform.position;

    // Map 'worldDeltaPosition' to local space
    float dx = Vector3.Dot(transform.right, worldDeltaPosition);
    float dy = Vector3.Dot(transform.forward, worldDeltaPosition);
    Vector2 deltaPosition = new Vector2(dx, dy);

    // Low-pass filter the deltaMove
    float smooth = Mathf.Min(1.0f, Time.deltaTime / 0.15f);
    smoothDeltaPosition = Vector2.Lerp(smoothDeltaPosition, deltaPosition, smooth);

    // Update velocity if time advances
    if (Time.deltaTime > 1e-5f)
        velocity = smoothDeltaPosition / Time.deltaTime;

    bool shouldMove = velocity.magnitude > 2f;

    // Update animation parameters
    anim.SetBool("AIwalking", shouldMove);
    anim.SetFloat("Velocity XAl", velocity.x, 0.15f, Time.deltaTime * 2);
    anim.SetFloat("Velocity ZAl", velocity.y, 0.15f, Time.deltaTime * 2);
    AlienStrafing();
}

void OnAnimatorMove()
{
    // Update position to agent position
    transform.position = agent.nextPosition;
   
}


private void AlienStrafing()
{
    if(alienGunnerController.Health <= 110 && alreadystrafe == false)
    {        
        anim.SetTrigger("Strafing");
        alreadystrafe = true;
    }
}

}

Here is the configuration of the animation enter image description here



Solution 1:[1]

In this mixamo animation, you can see body parts move freely the way you think like it is moving but, if you try to move an entire object, you should freeze the root motion and make dodge movement himself. The character will perform dodge animation still but, you have to move the object yourself by adding movement to the parent game object or with the script.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mustafa ?PEK