Check if transform is moving backwards

I’m trying to detect when a transform with a nav mesh agent attached without ridged body is moving backwards so I can apply an animation.

When the player with a nav mesh obstacle attached comes close to the enemy transform with a nav mesh agent attached the enemy is pushed backwards, so I wanted to add a walk backwards animation, I tried Here but is that not world space? And I also tried -

transform.localPosition

But not getting solid results, any help please.

Cheers.

You can do this by calculating dot product from transform forward vector and its movement vector.

To get movement vector, you need current transform position and position from previous frame. Forward vector is just transform.forward, but should be taken from previous frame.

If result of Vector3.Dot(forward, movement) is less than 0, it means your transforms moved backward between frames.