Reputation: 63
What i am doing here is rotating the camera by 90 degrees at a time on keypress, using
currentpos = transform.eulerAngles + new Vector3 (0, 90, 0);
and
transform.eulerAngles = Vector3.Lerp (transform.eulerAngles, currentpos, Time.deltaTime * speed);
the problem is when the camera reaches 360 degrees it wants to try change the angle to 450 so gets stuck in an infinite rotating loop. i have to use euler angles because the camera angled 45 degrees and euler angles rotates globally rather than locally
My question is how do i make it stop the infinite loop and go from 360 degrees to 90 degrees as well as not rotating in the opposite for example i tried
if (transform.eulerAngles.y == 360){
currentpos = transform.eulerAngles + new Vector3 (0, 0, 0);
}
however Vector3.Lerp
then rotates in the opposite direction back toward 0 rather than going through 360 degrees to reach 0.
Upvotes: 0
Views: 432
Reputation: 951
I suggest using Mathf.LerpAngle()
Whether you set an angle more than 360 (say 360 + x) or just an angle x, it will interpolate as you would expect without trying to shoot in the opposite direction.
I hope that helps!
EDIT: As @JoeBlow mentioned in the comments above, I also suggest using any one of the number of Rotate methods (on both transforms and quaternions), rather than setting target values directly. In most cases, it will ensure smooth rotation, however quickly or slowly you want it.
Upvotes: 1