Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Weird camera controller behaviour

Discussion in 'Scripting' started by joxthebest314, Mar 21, 2021.

  1. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I have a strange problem with my camera controller and I can't understand why. When I look in certain directions, the camera starts to shake. The rotation of the camera seems to change very quickly at this moment, but I don't understand why.

    Here is the code I use (this is called in update).
    Code (CSharp):
    1. void PhotoMode()
    2.     {
    3.         Vector3 newPos = transform.position;
    4.         //déplacement avant
    5.         if(Input.GetKey(InputManager.IM.forward))
    6.         {
    7.             newPos += cam.transform.forward  * moveSpeedPhoto * Time.unscaledDeltaTime;
    8.         }
    9.  
    10.         //déplacement arrière
    11.         if(Input.GetKey(InputManager.IM.backward))
    12.         {
    13.             newPos -= cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
    14.         }
    15.  
    16.         //déplacement gauche
    17.         if(Input.GetKey(InputManager.IM.left))
    18.         {
    19.             newPos += -transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
    20.         }
    21.  
    22.         //déplacement droite
    23.         if(Input.GetKey(InputManager.IM.right))
    24.         {  
    25.             newPos += transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
    26.         }
    27.  
    28.         float x = Input.GetAxis("Mouse X");
    29.         float y = Input.GetAxis("Mouse Y");
    30.  
    31.         x *= photoRotSpeed * Time.unscaledDeltaTime;
    32.         y *= photoRotSpeed * Time.unscaledDeltaTime;
    33.  
    34.         currentX = Mathf.Lerp(currentX, x, smoothTime * Time.unscaledDeltaTime);
    35.         currentY = Mathf.Lerp(currentY, y, smoothTime * Time.unscaledDeltaTime);
    36.  
    37.         float rotationX = transform.localEulerAngles.y + currentX * photoRotSpeed;
    38.  
    39.         rotationY += currentY * photoRotSpeed;
    40.         rotationY = Mathf.Clamp(rotationY, -90+65, 90+65);
    41.  
    42.         transform.localRotation = Quaternion.Euler(new Vector3(-rotationY, rotationX, 0));
    43.  
    44.         transform.position = newPos;
    45.     }
    Bug.gif
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Based on your Y rotation being near 180 degrees, it's probably gimbal lock.

    Camera code can get quite tricky.

    Have you considered just installing the Cinemachine package and using that for your camera control?
     
    Callum_Coombs and joxthebest314 like this.
  3. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I would still like to understand why it does this... It's such a simple code.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Fair enough...

    I'm going to guess that your problem begins with reading from .localEulerAngles (or .eulerAngles), and you can use Debug.Log() and prove me correct or wrong.

    Neither such property should be read from on an ongoing basis. They may be used in certain circumstances when you are sure the rotation involved is otherwise "clean," such as in Start() or perhaps when only rotated on a single axis.

    Once you start combining axes of rotation, reading from .eulerAngles is a strict no-no.
     
    joxthebest314 likes this.
  5. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    Okay thanks a lot, I will try that !