Search Unity

Resolved Flickering camera movement in editor but not in build

Discussion in 'Scripting' started by LeMecEnPyjama, May 23, 2023.

  1. LeMecEnPyjama

    LeMecEnPyjama

    Joined:
    Jun 1, 2021
    Posts:
    13
    Hello,

    As the title says, when I'm playing in editor, my camera movement flickers every few seconds. It's hard to explain it without having the project in hand, but imagine every few seconds the screen stop for some frames and then your camera move ewtremely fast in the direction you were going.
    In build however, the camera movement is very smooth.

    Here is the code I use to move the camera. The function is called in Update. I tried calling it in FixedUpdate but did not help. I also tried to enable Vsync without much success.

    Code (CSharp):
    1.     private void MoveCamera()
    2.     {
    3.         float rotationX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * mouseSensitivity;
    4.         float rotationY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * -mouseSensitivity;
    5.  
    6.         transform.Rotate(transform.up * rotationX);
    7.  
    8.         float cameraAngle = cameraObj.transform.localEulerAngles.x;
    9.         if (cameraAngle > 180)
    10.             cameraAngle -= 360;
    11.  
    12.         if (cameraAngle + rotationY < -headTiltMax)
    13.         {
    14.             cameraObj.transform.localEulerAngles = new Vector3(-headTiltMax, 0f, 0f);
    15.         }
    16.         else if (cameraAngle + rotationY > headTiltMax)
    17.         {
    18.             cameraObj.transform.localEulerAngles = new Vector3(headTiltMax, 0f, 0f);
    19.         }
    20.         else
    21.         {
    22.             cameraObj.transform.Rotate(rotationY, 0f, 0f);
    23.         }
    24.     }
    The transform belong to the character and the cameraObj is child to the character.
    I suppose it come from a settings I don't know, but it might be a code issue, I don't really know.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    If you insist on debugging what you have, look through some existing tutorials. The general case is to move camera in LateUpdate() but there may be other concerns too.
     
  3. LeMecEnPyjama

    LeMecEnPyjama

    Joined:
    Jun 1, 2021
    Posts:
    13
    Update on what I've tried to correct the issue.

    Putting the MoveCamera() function in LateUpdate did not solve the issue.
    Using the new Input Manager system did not solve the issue.
    Using Cinemachine and letting it handle the camera movement did not solve the issue.

    Some more context : on my computer it's visible but not too much, but on my friend computer it render the game unplayable. Also, the jitter is not present in build but only in editor.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    How is the player itself being moved? Is it a rigidbody, and have you turned on interpolation for it?
     
  5. LeMecEnPyjama

    LeMecEnPyjama

    Joined:
    Jun 1, 2021
    Posts:
    13
    Thanks Spiney, activating interpolation on the player did smooth the camera a bit !
    However it appears that the biggest cause of my friend was... That he used an old sh*tty mouse with very poor detection.

    Everything is smooth now, thanks.
     
    spiney199 likes this.