Search Unity

Changing the graphics quality changes the sensitivity

Discussion in 'Scripting' started by Sacha_Rolle, Jun 20, 2019.

  1. Sacha_Rolle

    Sacha_Rolle

    Joined:
    Jun 20, 2019
    Posts:
    2
    Hey,
    I'm making a really simple game to learn things about Unity. It's just a first person camera. When I built and ran the game, the controls felt a bit clunky and I noticed that changing graphics quality settings when running the game changes the sensitivity of the mouse in game.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerLook : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] private string mouseXInputName, mouseYInputName;
    9.     [SerializeField] private float mouseSensitivity;
    10.  
    11.     [SerializeField] private Transform playerBody;
    12.  
    13.     private float xAxisClamp;
    14.  
    15.     private void Awake(){
    16.         LockCursor();
    17.         xAxisClamp = 0.0f;
    18.     }// Awake
    19.  
    20.     private void LockCursor(){
    21.         Cursor.lockState = CursorLockMode.Locked;
    22.     }// Lock Cursor
    23.  
    24.     private void FixedUpdate(){
    25.         CameraRotation();
    26.     }// Update
    27.  
    28.     private void CameraRotation(){
    29.         float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
    30.         float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
    31.  
    32.         xAxisClamp += mouseY;
    33.  
    34.         if(xAxisClamp > 90.0f){
    35.             xAxisClamp = 90.0f;
    36.             mouseY = 0.0f;
    37.             ClampXAxisRotationToValue(270.0f);
    38.         }
    39.  
    40.         else if (xAxisClamp < -90.0f){
    41.             xAxisClamp = -90.0f;
    42.             mouseY = 0.0f;
    43.             ClampXAxisRotationToValue(90.0f);
    44.         }
    45.  
    46.         transform.Rotate(Vector3.left * mouseY);
    47.         playerBody.Rotate(Vector3.up * mouseX);
    48.     }// Camera Rotation
    49.  
    50.     private void ClampXAxisRotationToValue(float value){
    51.         Vector3 eulerRotation = transform.eulerAngles;
    52.         eulerRotation.x = value;
    53.         transform.eulerAngles = eulerRotation;
    54.     }// Clamp X Axis
    55.  
    56. }
    I really can't find what makes this problem to occur. Thanks for helping me !
     
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    You're multiplying the input with Time.deltaTime although you call CameraRotation() in FixedUpdate(). This means that the Input is always received within the same interval, no matter what the current framerate is, but you multiply it with deltaTime anyway. And your framerate surely changes depending on the quality setting ;).

    You could either change
    Code (CSharp):
    1. float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
    2. float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
    to
    Code (CSharp):
    1. float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.fixedDeltaTime;
    2. float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.fixedDeltaTime;
    , but I wouldn't recommend it because when getting Input in FixedUpdate(), you can miss some input as it's updated at the beginning of every frame (afaik).

    Personally, I would decouple the input logic and put it into an Update()-loop while still multiplying with Time.deltaTime and leave the movement logic in the FixedUpdate()-loop.
     
  3. Sacha_Rolle

    Sacha_Rolle

    Joined:
    Jun 20, 2019
    Posts:
    2
    Thanks a lot for your answer ! I was pretty sure it had something to do with the time between the frames, so I'm glad I wasn't so wrong. Thanks again