Top-down 2D with crosshair - how to play animation if strafing

Hello, I’d like to make my character play a strafing animation when moving sideways, relative to my cross-hair.
The crosshair is controlled via the mouse, much like a cursor, and my character always rotates to face it.

Could someone help me with working out a strategy so that my character knows when they are strafing? (or even moving backwards)

Thanks

I would just sync the animations to the velocity of the character. Take the world-space velocity, transform it into the local space of the character and update the animation with some conditionals.

private Rigidbody rb;

private void Awake() {
    rb = GetComponent<Rigidbody>();
}

private void Update() {
    var relativeVel = transform.InverseTransformDirection(rb.velocity.normalized);
}

In the resulting vector “relativeVel”, if the X is bigger than Z, the player is strafing. If it’s a negative number, the player is moving left, if positive right. Same rules apply for the Z component, negative means going backward, positive forward etc.

If you are using Mecanim, you can actually map the resulting vector directly to a 2D blend tree and it will blend between all the animations beautifully.

@schkorpio Could you post your ending script ? that could help people like me ^^