Calculating which direction my enemy should face when moving I have just implemented an algorithm which calculates the direction an enemy in my game should face when moving to a location.

This is a 2D game where characters can face in one of four directions: (Up, Down, Left, Right). 

When navigating to a point, if the new point is further away on the X axis than the Y axis, the enemy should show it's left or right animation.  However, if the movement would take the enemy more along the Y axis than the X axis, the enemy should show it's up or down animations.  

To put it visually, the center of the X in the following image illustrates the position of my enemy on an coordinate plane.  Depending on which quadrant his movement point is placed in, the respective animation for that quadrant should be shown.  


Here is my implementation.  My questions are:


Does this algorithm accomplish what I am thinking it does? From watching it on my enemy, it looks good so far, however I could just be getting lucky!
Can this algorithm be improved in any way?




protected FacingDirections CalculateFacingDirection(Vector2 navPos)
{
    Vector2 curPos = transform.position;

    float xDiff = Mathf.Abs(navPos.x - curPos.x);
    float yDiff = Mathf.Abs(navPos.y - curPos.y);

    if(navPos.x &gt; curPos.x) //we are moving left
    {
        if(navPos.y &gt; curPos.y) //we are moving up
        {
            return xDiff &gt; yDiff ? FacingDirections.Left : FacingDirections.Up; //are we moving more left or up?
        }

        return xDiff &gt; yDiff ? FacingDirections.Left : FacingDirections.Down; //are we moving more left or down?
    }
    else //we are moving right
    {
        if (navPos.y &gt; curPos.y) //we are moving up
        {
            return xDiff &gt; yDiff ? FacingDirections.Right : FacingDirections.Up; //are we moving more left or up?
        }

        return xDiff &gt; yDiff ? FacingDirections.Right : FacingDirections.Down; //are we moving more left or down?
    }
}
In my opinion you should first get the axis of the maximum direction via comparison of xDiff with yDiff:

protected FacingDirections CalculateFacingDirection(Vector2 navPos)
{
    Vector2 curPos = transform.position;

    float xDiff = Mathf.Abs(navPos.x - curPos.x);
    float yDiff = Mathf.Abs(navPos.y - curPos.y);

    if (xDiff &gt; yDiff) // we are moving more along X axis
    {
        return navPos.x &gt; curPos.x ? FacingDirections.Left : FacingDirections.Right;
    }
    // we are moving more along Y axis
    return navPos.y &gt; curPos.y ? FacingDirections.Up : FacingDirections.Down;
}