2D Platformer - Arm rotation / fliping problem

Hello,

I’am creating 2D platformer shooter game as my graduation project. However I came across some bugs which i cannot solve so I would really appreciate someone’s help.

So I’am using this script to rotate players arm when aiming and when the hand exceeds 180° character is supposed to flip :

void Update()
{
    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

    if (Mathf.Abs(rotationZ) > 90f && m_FacingRight)
    {
        Flip();
    }
    else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight)
    {
        Flip();
    }
    if (!m_FacingRight)
    {
        rotationZ += 180;
    }
    transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}

private void Flip()
 {
     m_FacingRight = !m_FacingRight;

     Vector3 theScale = MyPlayer.transform.localScale;
     theScale.x *= -1;
     MyPlayer.transform.localScale = theScale;
 }

}

However I’am also using Flip function in my Character controller script for running left and right. It looks like this :

public void Flip()

{
	m_FacingRight = !m_FacingRight;
    transform.Rotate(0f, 180f, 0f);
}

Well and my problem is that these two Flip functions overlap.

It is creating bugs like this when facing left and rotating arm : 150900-snimka-obrazovky-30.png

This is how i would like it to work : 150901-snimka-obrazovky-32.png

I appreciate any help and advice, thank you.

Hi,
so since an arm rotation >180° is turning your character around, I feel like what you want is the character to just always face the mouse position. Let’s say he’s running to the right, but the mouse is left. This would mean, he’s basically running backwards (might need different animation), pointing the arm to the left, right?
If I understood correctly, I would just determine the body direction on whether the Mouse.x is lesser or greater than the Player.x and the arm can then just point at the mouse position.

Hope this helps.
Cheers