- Home /
Sprite doesn't flip correctly.
I have this code:
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
if (angle < 270 && angle > 90)
transform.localScale = new Vector3(0.6f, -0.6f, 0f);
else
transform.localScale = new Vector3(0.6f, 0.6f, 0f);
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
It should flip the sprite in the "y" axis if the angle is in between 90 and 270, but it just seems to be checking if the angle is in between 90 and 180, what could be happening?
screen.png
(5.7 kB)
Comment
Answer by Slev · Sep 21, 2014 at 08:55 PM
Ok based on the picture you're flipping the wrong piece of the vector. Try this:
if (angle < 270 && angle > 90)
transform.localScale = new Vector3(-0.6f, 0.6f, 0f);
else
transform.localScale = new Vector3(0.6f, 0.6f, 0f);
The "rotations" of sprites aren't the same as rotations around an axis.
Nope. I'm flipping the "y" axis so the weapon still looks normal when I'm ai$$anonymous$$g left (it does so in the second image), but for angles over 180° it doesn't flip, so the weapon looks flipped/inverted (third image).