I'm using this for an orbiting camera script and it works great except for one problem.
When the userInput > 360 the camera jerks and it's not nice at all. I know what the problem is, I just can't find a solution.
Here's the code:
userInput.x = Input.GetAxis("Mouse X");
userInput.y = Input.GetAxis("Mouse Y");
regularRotation.y += userInput.x * inputSpeed;
regularRotation.x -= userInput.y * inputSpeed;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(regularRotation), 2 * Time.deltaTime);
As I said, I know the problem happens when the regularRotation values exceed 360 degrees...I've seen this http://unity3d.com/support/documentation/ScriptReference/Transform-eulerAngles.html and I know you're not supposed to set eulerAngles seperately, but does that apply to this case?
I thought Quaternion.Euler(regularRotation) would take care of big values...but apparently not.
I've also tried clamping the values and that doesn't seem to help.
Any other suggestions?
Thanks.
There is nothing wrong with what you're doing as such, but I can see how it might cause jerking for very fast rotations.
If shouldn't matter if `userInput` - or `userInput.x * inputSpeed` - is larger than 360. `Slerp` will always take the shortest route, so if your regularRotation is more than 180 degrees away from the current `transform.rotation`, `Slerp` will pick the shorter way around instead.
What can cause a problem is that Slerp is basically undefined when interpolating two rotations that are exactly 180 degrees apart. Because it could choose any way around the "sphere" to go from one pole to the opposite. Also, when the difference is close to 180 degrees, tiny differences or noise in the `regularRotation` can make the `Slerp` function jerky, because it picks a different direction to go in for each frame based on those small differences.
You may want to try to do the interpolation while still in Euler Angles space, rather than doing it in Quaternion space (`lerpedRotation` should be a member variable just like `regularRotation`):
userInput.x = Input.GetAxis("Mouse X");
userInput.y = Input.GetAxis("Mouse Y");
regularRotation.y += userInput.x * inputSpeed;
regularRotation.x -= userInput.y * inputSpeed;
lerpedRotation.y = Mathf.Lerp(lerpedRotation.y, regularRotation.y, 2 * Time.deltaTime);
lerpedRotation.x = Mathf.Lerp(lerpedRotation.x, regularRotation.x, 2 * Time.deltaTime);
transform.rotation = Quaternion.Euler(lerpedRotation);