Weird rotation with Transform.eulerAngle

Hey guyz,

I want a sun go around my level and this is gonna be done with a constant incrementing of it’s rotation.x using the Transform.Rotate function.

But I want to set the time at any moment ingame and the sun’s rotation has to change, too For those who don’t understand me: at 0am rotation shall be 270° so the sun shines from down to up and no light is on the terrain. At 12am the rotation shall be 90°, becouse then the sun shines from above and lights everything.

I tried this: sun.eulerAngle = new Vector3(360 * currentTime / maxTime, 0, 0); but at ~90° the sun acts completely weird!! it flips from the negative of it’s current x-rotation to the positive, BUT: this bug only appears for the x-axis >_<

what am i doing wrong?

you just encounter what is called a gimbal lock, when one axis coincides with another one (Gimbal lock - Wikipedia).

To avoid such a case, you should use quaternions (Unity - Scripting API: Quaternion)

This can be done transparently by creating quaternion using Quaternion.LookRotation() or directly changing a gameObject’s rotation with transform.LookAt().


The point is that i CANNOT do this (what I actually did)

sun.eulerAngle = new Vector3(360 * (currentTime / maxTime), sun.eulerAngle.y, sun.eulerAngle.z);


sun.eulerAngle = new Vector3(360 * (currentTime / maxTime), 0, 0);

which I cannot understand, but anyways I got it now…