- Home /
Rotating camera with Quaternion
I am trying to rotate my camera smoothly from one direction to another.
I have been setting my camera rotation with
Cam.newCamRotation = Quaternion.Euler (2, 152,349);
So I am trying to rotate from one rotation to another smoothly. So I made an effort with this adding it to the LateUpdate.
if (rotatesmooth){
newCamRotation = Quaternion.Lerp (Quaternion.Euler(0, 0,180),Quaternion.Euler(2, 152,349), 3f * Time.deltaTime );
}
It doesn't seem to do anything except jitter a little bit. The rest of my code is pretty extensive and the mistake may be somewhere else, but I think the issue is the part I am asking about. Anyone have any suggestions?
thanks
Generally simply use .Rotate or maybe .LookAt, There is little reason to ever touch quaternions.
@Fattie - I know your stance on Quaternions, but smooth rotation over time is much more cumbersome without them.
Answer by robertbu · Sep 22, 2013 at 12:56 AM
What you have here won't work (Slerp() or Lerp()). There are a couple of different ways to solve your problem depending on what you want.
1) You can implement a timer. When you fix both the start and end of the rotation, you need the values for the third parameter of Slerp to go from 0 to 1. Values outside this range do nothing. Your current code uses a small, but fluctuating value for the third parameter.
newCamRotation = Quaternion.Slerp (Quaternion.Euler(0, 0,180),Quaternion.Euler(2, 152,349), fraction );
2) Use the current rotation as the first parameter. This produces an eased movement:
newCamRotation = Quaternion.Slerp (newCamRotation,Quaternion.Euler(2, 152,349), speed * Time.deltaTime);
3) Use the current rotation and change to using Quaternion.RotateTowards(). This produces a movement at a fixed speed.
newCamRotation = Quaternion.RotateTowards (newCamRotation,Quaternion.Euler(2, 152,349), speed * Time.deltaTime);
Thanks for the response. What I have seems the same as your solution number two, but it doesn't seem to have an effect.
The code you have above is not #2. Please post your code and I'll take a look. Note I need to see how newCamRotation is set and handled.
Your right. I fixed it with your number two solution. Thank you very much.
Answer by DaveA · Sep 22, 2013 at 12:06 AM
Try Slerp
That didn't seem to work. Is it likely then, my mistake is somewhere else in the code?