- Home /
Flip the player with arm rotation
video of the problem :
https://www.youtube.com/watch?v=plYY5GvNPrk&feature=youtu.be
need to add a flip
arms script :
public class ArmsRot : MonoBehaviour {
private bool m_FacingRight = true; // For determining which way the player is currently facing.
public GameObject MyPlayer;
void Update ()
{
//Arm Rotation
Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
difference.Normalize ();
float rotationZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (0f, 0f, rotationZ);
if (rotationZ < -90 || rotationZ > 90)
{
if (MyPlayer.transform.eulerAngles.y == 0)
{
transform.localRotation = Quaternion.Euler (180, 0, -rotationZ);
Flip ();
} else if (MyPlayer.transform.eulerAngles.y == 180){
transform.localRotation = Quaternion.Euler (180, 180, - rotationZ);
Flip ();
}
}
}
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = MyPlayer.transform.localScale;
theScale.x *= -1;
MyPlayer.transform.localScale = theScale;
}
}
Comment
Answer by Magso · Feb 15, 2019 at 04:19 PM
Use Mathf.Clamp on rotationZ.
rotationZ = Mathf.Clamp(rotationZ, -90, 90);
Answer by Ymrasu · Feb 15, 2019 at 07:08 PM
First, if your arm is a child of your player then the arm gets flipped as well, so you'll need to account for that. Second, the way you set up your flip checks can cause you to call Flip() repeatedly.
void Update()
{
//Arm Rotation
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
// also check if you are facing the other way
// so you do not call Flip() when you dont need to
if (Mathf.Abs(rotationZ) > 90f && m_FacingRight) {
Flip();
}
else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight) {
Flip();
}
// Since your player's local scale is fliped
// you'll need to adjust the rotation too
if (!m_FacingRight) {
rotationZ += 180;
}
// lastly apply the rotation
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
}
Let me know if this updated Update() is not working out for you.