- Home /
Sprite Direction using transform.localRotation probelm
So I am using a little bit of code here which is supposed to flip the sprite upside down when the enemy or player goes upside down just to keep it looking upright. The code doesn't seem to pass the if statements because the Boolean doesn't change and the sprites don't flip. If you could help me out I'd appreciate it, thanks!
public bool FacingRight = true;
void Update ()
{
if (transform.localRotation.z > 90 && FacingRight == true) {
if (transform.localRotation.z < 270 && FacingRight == true) {
transform.localScale += new Vector3 (0, -1f, 0);
FacingRight = false;
}
} else if (transform.localRotation.z < 90 && FacingRight == false) {
if (transform.localRotation.z > 270 && FacingRight == false) {
transform.localScale += new Vector3 (0, -1f, 0);
FacingRight = true;
}
}
}
not an answer to your question, but the fact that you have
if (transform.localRotation.z > 90 && FacingRight == true) {
if (transform.localRotation.z < 270 && FacingRight == true) {
ins$$anonymous$$d of
if (
transform.localRotation.z > 90
&& transform.localRotation.z < 270
&& FacingRight == true
){
for no reason kind of bothers me xD (Sorry, just really jumped out at me, will try to answer your question now)
Answer by maccabbe · Feb 24, 2015 at 01:11 AM
You want to use transform.localrotation.eulerAngles.z or transform.rotation.eulerAngles.z instead of transform.localrotation.z.
Rotations are stored as quaternions but most times you want to use them like Vector3 (as it is in the unity inspector) so use .eulerAngles to easily convert them to Vector3.
Answer by siaran · Feb 23, 2015 at 11:18 PM
Ok, number of problems:
1) .localRotation is a quaternion, you cannot use it like you are doing here - quaternion x,y,z are NOT euler angles.
2) .localRotation is relative to an object's parent. Does your object even have a parent? If not, there is no point in using localRotation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613184146im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shooting script C# 1 Answer
How to rotate an object to face the direction it's going? 1 Answer
Sprite is not shown moving 1 Answer
Distribute terrain in zones 3 Answers