Camera movement stutters

Hello!

My camera movement is stuttering really hard. For some reason, it does this only sometimes when i start the game. or interact with anything, even tho the script from the card has no impact on the camera script.

Putting it in LateUpdate Update or FixedUpdate does not seem to make any diffrence, and only stops this Problem temporarily.

public float Sens;
[SerializeField] float xRotation = 0f;
[SerializeField] float yRotation = 0f;


void Start()
{
    xRotation = 0f;
    yRotation = 0f;
    Sens = 250f;
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

void LateUpdate()
{

    float mouseX = Input.GetAxisRaw("Mouse X") * Sens * Time.deltaTime;
    float mouseY = Input.GetAxisRaw("Mouse Y") * Sens * Time.deltaTime;

    xRotation += mouseX;
    yRotation -= mouseY;

    xRotation = Mathf.Clamp(xRotation, 130, 290);
    yRotation = Mathf.Clamp(yRotation, -40, 58);

    transform.localRotation = Quaternion.Euler(yRotation , xRotation, 0f);

}

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

If you think you’re already doing it with the above code, that just means you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

1 Like