- Home /
If statement being ignored (Apparently)
Well i already know i am missing something basic here, but as i can't find the problem i decided to ask. I am trying to make a simple airship controller script that can fit in any model, and it's working fine. I am making it a little bit more fancy and smooth right now, and i thought it was going to be a cool thing to feature a function to rotate the player's airship back once he rotates it. Now here is the problem:
Look at this code:
if (!Input.GetKey("a"))
{
if (planeBody.transform.localEulerAngles.z >= 0)
{
planeBody.transform.Rotate(0, 0, -8 * Time.deltaTime);
}
}
When the player presses "A", his airship will rotate left. This part of my script was supposed to work like this: IF the player isn't pressing "A", and his ship is at least more than 0 (angled to the left), move it to the right.
But the problem is that it continues going right even if the airship is angled at right, which is the exact reverse of what it was supposed to do. I want it to ONLY rotate to the right if the ship is angled at the left, not if it is angled to the right too. What's the problem? What am i missing?
Answer by UnityCoach · Apr 29, 2017 at 11:17 PM
I'm pretty sure that Transform.localEulerAngles.z
will always return a value between 0 and 360.
Oh really? Dang it -.- So what else could i use to measure the rotation of a GameObject on an If statement?
Well, it's a little tricky with 3d rotations. The Quaternion class has offers several methods to compare angles. I guess you could say :
if (planeBody.transform.localEulerAngles.z <= 180)
Answer by gwnguy · Apr 30, 2017 at 09:51 AM
Would
if (planeBody.transform.localEulerAngles.z - 180 >= 0) work ? This would say angles from 0 to 180 degrees are "negative" while 180 to 360 are positive.