- Home /
if statements with transform.rotation not working?
I'm making a game with a day/night cycle and I'm trying to make the sun rotate on the x axis. I wanted the sun to move faster during the night to make it shorter, so I did this:
public float daySpeed = 1f;
public float daySpeedActual = 1f;
public float nightMultiplier = 2.5f;
public GameObject sunAnchor;
void Update() {
transform.RotateAround(sunAnchor.transform.position, Vector3.right,
daySpeedActual * Time.deltaTime); // Rotate around the anchor
if (transform.rotation.x > -160 && transform.rotation.x < -25) { // When the sun is down
daySpeedActual = daySpeed * nightMultiplier; // Change cycle speed
} else {
daySpeedActual = daySpeed;
}
}
But when I run the game, night does not affect the cycle speed! Help please!
Answer by Bunny83 · Dec 16, 2018 at 11:58 PM
rotation is a unit Quaterion and does not represent euler angles. A Quaternion is a 4 dimensional complex number. Its 4 components are in the range -1 to 1. You may want to use transform.eulerAngles instead. However note that Unity internally works with Quaternions. When you read the eulerAngles property Unity actually calculates the euler angles from the quaternion. Since eulerangles do not have a unique representation for a certain rotation the result may not be what you expect. The eulerAngles 0,0,0 is the same as 180,180,180. or 30,20,0 is the same as 210,160,180. So you can't be sure which eulerangles representation you get.
It's usually better to use vectors and work with either Vector3.Angle or Vector3.Dot. In your case this probably should work:
if (Vector3.Angle(Vector3.up, transform.up) >115)
daySpeedActual = daySpeed * nightMultiplier;
else
daySpeedActual = daySpeed;
Answer by Volkyzero · Dec 16, 2018 at 11:50 PM
Instead of reading it through "rotation" you should use eulerAngles.