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

Smooth Mouse-Look

Discussion in 'Scripting' started by TheDuples, Jan 26, 2019.

  1. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    I've created a fairly simple mouse-look script for an fps game i'm working on, however I've noticed that while the mouse movement appears smooth, whenever I strafe around objects I get this annoying jittery look on objects that I focus on. Can someone take a look at my code, and tell me how I might improve it?

    Code (CSharp):
    1. public class PlayerLook : MonoBehaviour
    2. {
    3.     public float mouseSensitivity = 100.0f;
    4.     public float clampAngle = 80.0f;
    5.  
    6.     private float mouseX;
    7.     private float mouseY;
    8.  
    9.     private float rotationX = 0.0f;
    10.     private float rotationY = 0.0f;
    11.  
    12.     void Start()
    13.     {
    14.         Vector3 rotation = transform.localRotation.eulerAngles;
    15.         rotationX = rotation.x;
    16.         rotationY = rotation.y;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         mouseX = Input.GetAxis("Mouse X");
    22.         mouseY = Input.GetAxis("Mouse Y");
    23.  
    24.         rotationX += mouseY * mouseSensitivity * Time.deltaTime;
    25.         rotationY += mouseX * mouseSensitivity * Time.deltaTime;  
    26.  
    27.         rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
    28.  
    29.         Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
    30.  
    31.         transform.rotation = Quaternion.Lerp(transform.rotation, localRotation, Time.fixedTime);
    32.     }
    33. }
     
    Last edited: Jan 26, 2019
  2. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    I think I figured it out, my update functions on my rigidbody player and my mouse-look camera were different from each other. I changed them both to FixedUpdate and now it looks much smoother.