- Home /
Camera rotation on z axis
I am trying to turn my camera on the z axis by 25 degrees. I got it to rotate properly if I turn on the right, but whenever I hit something on the left, my camera just does a 360 and goes back to zero. How do I change it so that my camera will only turn -25 degrees to the left and not do the 360? The issue is with the left impact.
if(leftImpact)
{
Vector3 euler = fpsCam.transform.localEulerAngles;
euler.z = Mathf.Lerp(euler.z, -25, 2f * Time.deltaTime);
fpsCam.transform.localEulerAngles = euler;
}
if(rightImpact)
{
Vector3 euler = fpsCam.transform.localEulerAngles;
euler.z = Mathf.Lerp(euler.z, 25, 2f * Time.deltaTime);
fpsCam.transform.localEulerAngles = euler;
}
Answer by bdubbert · Jun 22, 2021 at 07:34 PM
Use Quaternion.Slerp instead of just Lerping between Euler angles.
Quaternion targetRotation = Quaternion.Euler(transform.localEulerAngles.x, transform.localEulerAngles.y, -25);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, speed * Time.deltaTime);
Answer by ravenclarico · Jun 22, 2021 at 06:57 PM
You're not using Lerp correctly, see the documentation( lerp documentation )you should try doing just "euler.z = euler.z + 25" and "euler.z = euler.z - 25" though you may be faced with the new problem of your camera spinning too fast
This does help! Unfortunately, this isn't what I want to do. I'm trying to achieve something that's similar to what happens when you start a wall run in a game like Titanfall. With this small fix, the camera just snaps to the angle instead of smoothing into it.