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 :
This is how i would like it to work :
I appreciate any help and advice, thank you.