Search Unity

Question How can I fix this first-person camera jitter?

Discussion in 'Scripting' started by imaginereadingthis, Jan 9, 2023.

  1. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Hello everyone!

    I've recently gotten back into game dev after a long break, and I'm having some trouble setting up a good first-person Rigidbody-based movement system. The problem that I'm facing is that when the player simultaneously moves and looks around, nearby objects seem to be jittering a lot. For example, if you would be walking in a circular path around a cube and be constantly looking at it, that cube would jitter a lot.

    This is what the movement script looks like:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour
    6. {
    7.     // references
    8.     Transform orientation;
    9.     Rigidbody rb;
    10.  
    11.     // movement
    12.     float vertical, horizontal;
    13.     public float speed, multiplier;
    14.     Vector3 moveDir;
    15.  
    16.     // ground & jumping
    17.     float playerHeight;
    18.     public LayerMask whatIsGround;
    19.     bool grounded;
    20.     public float jumpForce;
    21.  
    22.     // drag
    23.     public float groundDrag, airDrag;
    24.  
    25.     void Awake() {
    26.         rb = GetComponent<Rigidbody>();
    27.         orientation = transform.GetChild(0);
    28.         playerHeight = transform.localScale.y; // Height of player
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update() {
    33.         GetInput();
    34.         ControlDrag();
    35.         Jump();
    36.     }
    37.  
    38.     void FixedUpdate() {
    39.         MovePlayer();
    40.     }
    41.  
    42.     void GetInput() {
    43.         // Get horizontal and vertical inputs (WASD & Arrow Keys).
    44.         vertical = Input.GetAxisRaw("Vertical");
    45.         horizontal = Input.GetAxisRaw("Horizontal");
    46.         // Determine the direction the player will move based on horizontal and vertical input.
    47.         moveDir = orientation.forward * vertical + orientation.right * horizontal;
    48.  
    49.         // Check if the player is on the ground.
    50.         grounded = Physics.CheckSphere(transform.position - new Vector3(0f, playerHeight, 0f), playerHeight * 0.1f, whatIsGround);
    51.     }
    52.  
    53.     // Move the player
    54.     void MovePlayer() {
    55.         rb.AddForce(moveDir.normalized * speed * multiplier * Time.deltaTime, ForceMode.Acceleration);
    56.     }
    57.  
    58.     // Set different drag forces depending on where the player is.
    59.     void ControlDrag() {
    60.         if (grounded) {
    61.             rb.drag = groundDrag;
    62.         } else {
    63.             rb.drag = airDrag;
    64.         }
    65.     }
    66.  
    67.     // Reset y velocity and apply a vertical force on the player.
    68.     void Jump() {
    69.         if (Input.GetButtonDown("Jump") && grounded) {
    70.             rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    71.             rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    72.         }
    73.     }
    74. }
    75.  
    And this is what the first-person camera script looks like:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Look : MonoBehaviour
    6. {
    7.     public float sensX;
    8.     public float sensY;
    9.  
    10.     Transform orientation, camPos;
    11.  
    12.     float xRotation;
    13.     float yRotation;
    14.  
    15.     void Awake() {
    16.         orientation = FindObjectOfType<Move>().transform.GetChild(0);
    17.         camPos = FindObjectOfType<Move>().transform.GetChild(1);
    18.     }
    19.  
    20.     private void Start() {
    21.         Cursor.lockState = CursorLockMode.Locked;
    22.         Cursor.visible = false;
    23.     }
    24.  
    25.     private void Update() {
    26.         // get x and y mouse inputs
    27.         float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
    28.         float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;
    29.  
    30.         // set rotations
    31.         yRotation += mouseX;
    32.         xRotation -= mouseY;
    33.  
    34.         // clamp x rotation
    35.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    36.  
    37.         // rotate cam and orientation
    38.         transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
    39.         orientation.rotation = Quaternion.Euler(0, yRotation, 0);
    40.     }
    41.  
    42.     void FixedUpdate() {
    43.         // set position of camholder to campos object
    44.         transform.position = camPos.position;
    45.     }
    46. }
    47.  
    Also, this is what the hierarchy looks like:
    upload_2023-1-8_18-19-1.png

    I've been scratching my head for the past few hours. I can't figure out where the camera jitter is coming from. Do you guys have any ideas?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
  3. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    I guess also make sure your Rigidbody's interpolation setting is set to Interpolate.
     
    imaginereadingthis likes this.
  5. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    I just left my computer for 10 minutes, came back, and everything works perfectly.

    ヽ(ಠ_ಠ)ノ
     
    spiney199 likes this.