- Home /
Inspector OnValidation rotation issue
I have a script 'DayNightCycle' which is attached to the main directional light which over the course of 20 minutes rotates the light to give a simple day night cycle, this works great for me.
However I wanted to be able to set the light rotation for each scene, so I added the following code:
[Range(0.0f, 360f)]
public int LevelStartTime = 90;
private void OnValidate()
{
transform.eulerAngles = new Vector3(
LevelStartTime,
transform.eulerAngles.y,
transform.eulerAngles.z
);
}
This gives me a 0-360 slider which allows me to set the start light rotation and see it in the editor. However when the value is slid between 90 and 270 the light keeps 'flipping' on the 'y' rotation. For example when the slider is on 177 the transform rotation value shows 3, and when its on 234 transform shows -54. However if you manually type in those same numbers in my LevelStartTime the transform correctly has the same number!
Any ideas whats going on?
Answer by davidcox70 · Aug 03, 2020 at 08:48 PM
Rather than set transform.eulerAngles, try;
transform.rotation = Quaternion.Euler(LevelStartTime, transform.eulerAngles.y, transform.eulerAngles.z);
See if that works better.
Unfortunately this has the same issue! What is also strange is that when this 'flipping' effect happens occasionally the Y & Z axis change. Y sometimes changes to 0 and Z changes to -1.525879e-05!! Very confused about this problem. Especially as manually entering any X value always works!
The problem is that Euler angles do flip when "going around the back". If you are only interested in a rotation in one axis, then it should work if you do not refer to the Euler angles of the axis you are not rotating in your calculation (eg Vector3(LevelStartTime,0,0)). If you do need to set the Y and Z axis, then do this from a variable that doesn't refer back to the current rotation, as otherwise this value will flip and upset your result.
Your answer
Follow this Question
Related Questions
Issues with AnimationCurves 0 Answers
Lightmap Baking Cross-Platform: iOS vs Mac vs Windows 1 Answer
Game Froze, No Inputs 0 Answers
Cant find issue tracker's resource 0 Answers
I can't open my Unity Project 1 Answer