# 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:

1. 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!
2. 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 > curPos.x) //we are moving left
{
if(navPos.y > curPos.y) //we are moving up
{
return xDiff > yDiff ? FacingDirections.Left : FacingDirections.Up; //are we moving more left or up?
}

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

return xDiff > yDiff ? FacingDirections.Right : FacingDirections.Down; //are we moving more left or down?
}
}

• Personally, I would make a Vector2 or Vector2F that you drop the difference between the points into, or the force being applied, and then use a Theta from that to determine the angle the enemy faces. – Der Kommissar Jul 10 '15 at 12:50

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 > yDiff) // we are moving more along X axis
{
return navPos.x > curPos.x ? FacingDirections.Left : FacingDirections.Right;
}
// we are moving more along Y axis
return navPos.y > curPos.y ? FacingDirections.Up : FacingDirections.Down;
}


float xDiff = Mathf.Abs(navPos.x - curPos.x);
float yDiff = Mathf.Abs(navPos.y - curPos.y);
bool movingRight = navPos.x > curPos.x;
bool movingUp = navPos.y > curPos.y;

if(xDiff>yDiff)
{
return movingRight ? FacingDirections.Left :  FacingDirections.Right;
}
else
{
return movingUp ? FacingDirections.Up :  FacingDirections.Down;
}

• Welcome to CodeReview, Alex. This isn't much of an answer, perhaps more fitting as a comment. – Legato Jul 10 '15 at 7:16