I'm just trying a simple rotation of a camera starting at 180 degrees and want it to stop at exactly 0 degrees. I can't seem to get it to work properly.
Here is what I have so far that sort of works but it never stops at 0 it is always a degree or two off.
void Update()
{
// Start camera at a point and zoom it back
if (Camera.main.orthographicSize != orthographicSize)
{
Camera.main.orthographicSize += 1.0f;
}
// Rotate camera counter clockwise
if (Camera.main.transform.rotation.z > rotationEnd)
{
Camera.main.transform.Rotate(0, 0, -2.0f);
}
}
If I try this other way the camera never stops spinning since it never hits exactly 0... so I have cheated for now and done it the way I have above.
// Rotate camera counter clockwise
if (Camera.main.transform.rotation.z != rotationEnd)
{
Camera.main.transform.Rotate(0, 0, -2.0f);
}
There's at least a couple of problems here. The first is that you appear to be expecting the 'rotation' property to return Euler angles, but 'rotation' is a quaternion. If you want to access the angles in Euler-angle form, you can use 'eulerAngles' or 'localEulerAngles'.
Now, it just so happens that in this particular case the code will still produce results that are sort of correct. When you initialize the orientation to a 180-degree rotation about the z axis, the 'z' element of the quaternion is set to 1, or sin(180/2). As the angle of rotation gets smaller, this value decreases. Presumably, the z value eventually becomes negative and rotation stops; however, in most cases you probably will have overshot the target rotation by some small amount.
The other part of the problem is that when working with floating-point numbers, you can't always expect a sequence of operations to produce the expected 'real number' result. This is especially true when transcendental functions are involved (as is the case here).
You can accept a certain degree of inaccuracy in your result, or you can use a deterministic method to ensure that the rotation ends where you want it to. For example, instead of rotating incrementally, you could use Quaternion.Slerp() to interpolate between the starting and ending orientations over time. Or, you could use Mathf.LerpAngle() or SmoothDampAngle() to accomplish the same thing. Other options include detecting when the object has 'passed' the target orientation and 'snapping' to the target orientation at that point. (There are various ways you could determine that the target orientation has been passed, but note that you'll need to take Unity's conventions for representing Euler angles into account when performing a direct comparison using angles.)
Also the code is framerate-dependent...you can't use values like 1.0 or -2.0 per frame and have it behave properly.
@Jesse This really helps me understand what the heck is going on with the angles... I appreciate the info. @Eric5h5 I tried using it with Time.DeltaTime a couple of days ago and it just made the over shoot worse but I did have it in my code at one point.
Also the code is framerate-dependent...you can't use values like 1.0 or -2.0 per frame and have it behave properly.
– Eric5h5@Jesse This really helps me understand what the heck is going on with the angles... I appreciate the info. @Eric5h5 I tried using it with Time.DeltaTime a couple of days ago and it just made the over shoot worse but I did have it in my code at one point.
– joedrigon