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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Camera clamp causes jitter when reaching max rotation.

Discussion in 'Scripting' started by MikawasakiDigital, Apr 16, 2019.

  1. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hey guys, I was able to clamp my camera rotation for a more realistic FPS effect, although when my player hits the max angle, they are pushed back.

    Does anyone have any insight on how to keep the player facing up/down without an awkward push back?
    Code (CSharp):
    1.     private void RotateCamera()
    2.     {
    3.         Vector2 inputValues = new Vector2 (Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    4.  
    5.         inputValues = Vector2.Scale(inputValues, new Vector2(lookSensitivity * smoothing, lookSensitivity * smoothing));
    6.  
    7.         smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / smoothing);
    8.  
    9.         smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / smoothing);
    10.  
    11.         currentLookingPos += smoothedVelocity;
    12.  
    13.         transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right);
    14.  
    15.         player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.transform.up);
    16.      
    17.         currentLookingAt.y = Mathf.Clamp(currentLookingAt.y, -80f, 80f);
    18.     }
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    Why are you clamping currentLookingAt after you set the rotation of the player? Should you not clamp this before?
     
    MikawasakiDigital likes this.
  3. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Not sure, I am new to coding. What could be done to improve that?

     
  4. Llama_w_2Ls

    Llama_w_2Ls

    Joined:
    May 16, 2020
    Posts:
    7
    Here's an FPS mouse-to-look-around script, from a Brackeys tutorial lol:
    You need to use quaternions to not jitter i guess

    Code (CSharp):
    1. public float MouseSensitivity = 500f;
    2.  
    3.     public Transform PlayerBody;
    4.  
    5.     float Xrotation = 0f;
    6.  
    7.     //Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         Cursor.lockState = CursorLockMode.Locked;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     public void Update()
    15.     {
    16.         float MouseX = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
    17.         float MouseY = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
    18.  
    19.         Xrotation -= MouseY;
    20.         Xrotation = Mathf.Clamp(Xrotation, -90f, 90f);
    21.  
    22.         transform.localRotation = Quaternion.Euler(Xrotation, 0f, 0f);
    23.  
    24.         PlayerBody.Rotate(Vector3.up * MouseX);
    25.     }