0
\$\begingroup\$

I want to create a 2D topdown game with Unity. When moving the player I also want him to rotate.

Most tutorials provide a solution changing the localscale of the transform component on the x axis setting -1 for left movement and 1 for right movement.

The attached SpriteRenderer component also got flipX and flipY checkboxes. I think setting these checkboxes by code might be more efficient.

Using this example code achieves the desired rotation

bool flipX;
bool flipY;

void Update()
{   
    float horizontalInput = movementDirection.x;

    if (horizontalInput > 0)
    {
        flipX = false;
    }
    else if (horizontalInput < 0)
    {
        flipX = true;
    }

    float verticalInput = movementDirection.y;

    if (verticalInput > 0)
    {
        flipY = false;
    }
    else if (verticalInput < 0)
    {
        flipY = true;
    }

    spriteRenderer.flipX = flipX;
    spriteRenderer.flipY = flipY;
}

My question is: Is there a way avoiding these four if-statements? Maybe there is a more optimized way using some math.

\$\endgroup\$
2
\$\begingroup\$

If you need people to review your code you have to give a complete bunch of working code. Here, we can only make assumptions about how what you show interoperate with what you don't show.

Your movementDirection..x and movementDirection..y can only be -1, 0, 1. Correct? So, why using floats instead of ints ? Or even better, an enum, per example:

enum Movement
{
    Backward = -1,
    None = 0,
    Forward = 1      
};

You only need flipping sprite if movements are negatives (for each axis). That's right?

Why just don't test for backwarding movement and set flipX to false otherwise (same for Y).

void Update()
{   
    flipX = (movementDirection.x == Movement.Backward);
    flipY = (movementDirection.y == Movement.Backward);

    spriteRenderer.flipX = flipX;
    spriteRenderer.flipY = flipY;
}

Edit: That's in case of neutral fixed states (Movement.None never flipped). If when no movement you want to keep the last flipped states you still want two statement by axis.

void Update()
{   
    if  (movementDirection.x != Movement.None)
        flipX = (movementDirection.x == Movement.Backward);
    if  (movementDirection.y != Movement.None)
        flipY = (movementDirection.y == Movement.Backward);

    spriteRenderer.flipX = flipX;
    spriteRenderer.flipY = flipY;
}

Edit 2:

For completeness, if you really want to minimize statements, you can use Convert, but i dont think it will improve performances :

void Update()
{   
    if  (movementDirection.x != Movement.None)
        flipX = Convert.ToBoolean(movementDirection.x - 1);
    if  (movementDirection.y != Movement.None)
        flipY = Convert.ToBoolean(movementDirection.y - 1);

    spriteRenderer.flipX = flipX;
    spriteRenderer.flipY = flipY;
}
\$\endgroup\$
  • \$\begingroup\$ The class fields also seem redundant... I'd make everything local to the method. \$\endgroup\$ – t3chb0t Oct 28 '18 at 11:32
  • \$\begingroup\$ Look at my edit. In his original code, he doesnt change FlipX value from the precious in case of no movement. \$\endgroup\$ – Calak Oct 28 '18 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.