HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Calculating which direction my enemy should face when moving

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enemydirectioncalculatingfacemovingshouldwhichwhen

Problem

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 > 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?
    }
}

Solution

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;
}

Code Snippets

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;
}

Context

StackExchange Code Review Q#96432, answer score: 4

Revisions (0)

No revisions yet.