Trying to get Nav Agent to Walk Backwards

Trying to get an animal AI (a Siberian Tiger) to walk toward a cube. But if it sees a sphere come within a certain distance, to stop and take a few steps backwards.

The problem is that when I move the sphere (manually for now) close to the Tiger, it turns and slerps away. I want it to move directly backwards while keeping the forward orientation toward the sphere.

using UnityEngine;
using UnityEngine.AI;

public class TigerMovementTest : MonoBehaviour
{
    public Transform cube;
    public Transform sphere;
    NavMeshAgent agent;

    private void Start()
    {
        agent = this.GetComponent<NavMeshAgent>();
    }

    private void LateUpdate()
    {
        agent.SetDestination(cube.position);

        float distance = Vector3.Distance(transform.position, sphere.transform.position);
        float BackAway = 3.0f;

        if (distance < BackAway)
        {
            Vector3 dirToGoal = transform.position - sphere.transform.position;
            Vector3 newPos = Vector3.back;
            agent.SetDestination(newPos);
        }
   }
}

Hi @jkuehlin ,

If you want to move the agent backwards I think your only option is to take control of the situation and do it manually.

Probably this means something like disabling the agent, then moving your object to the correct position manually.
You could do this by calculating the position to be 2 meters behind the tiger and then lerp the position over time there.
And if your animation is driven by a blend tree, then it’s somewhat easy to get it behave ok, as your backwards walking would be automatically activated when you move the object backwards.

Hi,

After you triggers this event, I would recommend disable agent.updatePosition=false and agent.updateRotation=false, use agent.Move to move tiger backward, and revert all back.

Or just agent.updateRotation=false, and move it by agent.setDestination, than agent.updateRotation=true

Or tiger.Scale(-1,-1,1), agent.setDestination(), tiger.Scale(1,1,1), maybe make some rotation before.

But yes, agent cannot crawl back)

2 Likes

I know this is an old thread, but I found it while trying to solve this myself. I thought I had to do all kinds of trickery to make it work. I was running my backing up animation without nav mesh agent and trying to adjust y position based on sampling the terrain y position. Then I decided to just try an experiment. I Have a simple Animator with Idle and Forward and Backward root animations. Setting the Animator float Speed parameter to 1 will walk forward and -1 to walk backward. Then I simply set the destination for the nav mesh agent and set its speed to 1 for forward or -1 for backward. That’s it. It works correctly and the agent will either walk forward or walk backward to its destination with the proper orientation.

1 Like

OK, so it turns out I had to do one more thing to make this work. When walking straight backwards this works fine, but it will not rotate by itself to adjust for a more complex path. So, in an OnAnimatorMove method, I use the SamplePathPosition method to get a point further along on the path and then rotate the AI toward that point. This allows the agent to navigate complex obstacles backwards.

If you’re using root motion with rotation animations in mixer / blend tree then a neat trick here is to invert the rotation parameter used in the animator. This forces the agent to turn to face away from the destination rather than toward it, and back up towards the destination.