- Home /
Why is the Camera bouncing on the X-Axis?,Why does the Camera jump around on the X-Axis?
Hello,
I am new at Unity and after watching multiple tutorials, I created this script to make a smooth FPS-Cam:
public class CamMovement : MonoBehaviour
{
private float MouseSensivity;
public float MaxY;
public float MinY;
public Transform Bodytrans;
float xRotation;
float yRotation;
public Slider SensivitySlider;
public float SpinSpeed;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
SensivitySlider.value = 100;
}
void SetSliderValues(){...}
void CursorLock(){...}
void CamRotate()
{
float mouseX = Input.GetAxis("Mouse X") * MouseSensivity *
Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * MouseSensivity *
Time.deltaTime;
xRotation -= mouseY;
yRotation += mouseX;
xRotation = Mathf.Clamp(xRotation, MinY, MaxY);
Quaternion RotationY = Quaternion.Euler(xRotation,0, 0);
Quaternion RotationX = Quaternion.Euler(0, yRotation, 0);
transform.localRotation = Quaternion.Lerp(transform.localRotation, RotationY, SpinSpeed * Time.deltaTime);
Bodytrans.rotation = Quaternion.Lerp(Bodytrans.rotation, RotationX, SpinSpeed * Time.deltaTime);
}
void Update()
{
CamRotate();
CursorLock();
SetSliderValues();
}
}
The Problem: The Cam keeps bouncing on the X-Axis when moving at fast speed. Example: I move the Cam to the right with high MouseSensivity. The mouse now bounces back to the left. This only occurs at high speed.
I have tried using Bodytrans.Rotate() instead of Quaternion.Lerp() first; This does remove the bouncing, but it also causes a weird stuttering, like the Camera is skipping frames. If Bodytrans.Rotate() is the right choice, how do you remove the stuttering?
Thanks in advance!
Answer by AbbottMedia · Aug 30, 2021 at 05:42 PM
Hey, sorry to bring up an old post but I was having the same problem and decided to reply in case others do too. I believe the bouncing happens because when x input value goes over 1 with a high multiplier (effectively sensitivity setting) unity takes it as a high rotation input (e.g. 390) and when it converts it into a quaternion it ends up ignoring the full rotation and trying instead to Lerp/Slerp to 30 (390 - 360). You end up getting it attempting and accelerating to lerp a full rotation then hitting it's target and bouncing off in the following frame.
As a solution I ended up clamping the amount of input you can have on the x axis. It makes it more sluggish to respond, and may not work well for an FPS, but keeps the camera from ping ponging.
if (Input.GetMouseButton(0)) {
xDeg += Mathf.Clamp(Input.GetAxis("Mouse X"), -0.99f, 0.99f) * xSpeed * moveSpeed;
yDeg -= Input.GetAxis("Mouse Y") * ySpeed * moveSpeed;
//Clamp the horizontal axis for the orbit
xDeg = ClampAngle(xDeg, xMinLimit, xMaxLimit);
//Clamp the vertical axis for the orbit
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
OrbitCamera(xDeg, yDeg);
} else {
OrbitCamera(xDeg, yDeg);
}
If someone else knows a better way to handle this, or if it's caused by something other than the vector3/quaternion conversion please let me know.
Your answer
Follow this Question
Related Questions
Problems with Dani's Karlson FPS Movement Controller 1 Answer
how to make my bones follow the camera(FPS) 0 Answers
First Person Camera looking down 1 Answer
Camera moving independent of players rotation 1 Answer
FPS Leaning movement! 0 Answers