- Home /
Stop camera from stuttering when looking straight up or down
Hey, I'm using the new input system and I'm trying to get the right stick to control where the camera is looking, my games controls should allow the players camera to rotate freely in any direction (except z) including going upside down, but my code isn't working.
Whenever the x rotation of the camera tries to go above the X rotation 90 or below the X rotation -90, the camera rotation tries not to pass that 90 mark and resets itself to 90, and if it could do this without stuttering I would call it good and move on, but the effect it has now is unacceptable.
Here's what I wrote:
void Update()
{
currentRotation = transform.localEulerAngles;
Vector2 rotationInput = ungodlyControlsInputAsset.PlayerControls.RotateCamera.ReadValue<Vector2>();
Debug.Log(rotationInput);
float h = gamepadHorizontalSpeed * rotationInput.x;
float v = gamepadVerticalSpeed * -rotationInput.y;
currentRotation += new Vector3(v, h, 0f);
transform.localEulerAngles = currentRotation;
}
I hope you can help!
fixing this bug would be fine, but not optimise, I recommend you using cinemachine package, for third-person and FPS camera which is customizable and compatible with your input system profile. (Tip: for having new input system with cinemachine add CinemachineInputProvider component to your virtual camera) https://www.youtube.com/watch?v=ImuCx_XVaEQ&ab_channel=samyam
I do want to use cinemachine but i cant understand how this problem is happening or how to fix it, first I would like to get it working, understand why it wasn't working, then after all of my camera controls are working through the new input system, go back and implement cinemachine wherever possible. Thank you for this advice and link to more information.
after implementing the cinemachine tutorial you linked me, I'm still having the same problem when looking straight down. After reading this I believe what I'm experiencing is a Gimbal Lock problem, where my $$anonymous$$d is thinking that the rotations inside the transform in the inspector are reliable enough to run math on but the sorcery within Euler is not property represented by thinking in terms of transform.rotate. I believe the solution will ultimately come down to modifying my code through Quaternions but I don't understand how exactly to communicate my needs to the Quaternion operation yet. I'm going to keep working the problem.
It's not recommended to read the rotation's current value from eulerAngles, editing it and setting back in this context. This is because the eulerAngles are always converted to/from the rotations internal Quaternion representation and these conversion are not always commutative (this is also noted in the Unity Documentation https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html). Reading the value is also actually unnecessary.
You can just use transform.localRotation *= Qaternion.Euler(new Vector3(v, h, 0f))
to have the same effect. This is also gimbal lock proof way.
Answer by ssmas · Dec 30, 2021 at 05:35 AM
I checked your @ChaosRobin code in Unity. Everything worked fine. The only thing I doubted was your input, so I changed it to the old input for myself.
public class CameraTest: MonoBehaviour
{
public float gamepadHorizontalSpeed;
public float gamepadVerticalSpeed;
Vector3 currentRotation = Vector3.zero;
Vector2 rotationInput = Vector3.zero;
void Update()
{
currentRotation = transform.localEulerAngles;
rotationInput.x = Input.GetAxis("Mouse X");
rotationInput.y = -Input.GetAxis("Mouse Y");
Debug.Log(rotationInput);
float h = gamepadHorizontalSpeed * rotationInput.x;
float v = gamepadVerticalSpeed * -rotationInput.y;
currentRotation += new Vector3(v, h, 0f);
transform.localEulerAngles = currentRotation;
}
}
Check your Debog.log() in the console panel and also any error that may cause this malfunction.