Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Sensitivity in my MouseLook script.

Discussion in 'Scripting' started by JustYourFriendlyNeighbourhoodSlug, Feb 20, 2021.

  1. JustYourFriendlyNeighbourhoodSlug


    Aug 24, 2020
    Code (CSharp):
    1.     public float sensitivity = 50f;
    2.     public float maxYAngle = 80f;
    3.     private Vector2 currentRotation;
    5.     public Transform Player;
    7.     public Vector3 PlayerFacing;
    9.     void Update()
    10.     {
    11.         currentRotation.x += Input.GetAxis("Mouse X") * sensitivity //* Time.deltaTime;
    12.         currentRotation.y -= Input.GetAxis("Mouse Y") * sensitivity //* Time.deltaTime;
    13.         currentRotation.x = Mathf.Repeat(currentRotation.x, 360);
    14.         currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle);
    15.         Camera.main.transform.rotation = Quaternion.Euler(currentRotation.y, currentRotation.x, 0);
    17.         Player.rotation = Quaternion.Euler(PlayerFacing);
    18.         PlayerFacing = new Vector3(0, Camera.main.transform.localRotation.eulerAngles.y, 0);
    19.         Camera.main.transform.position = new Vector3(Player.position.x, Player.position.y +1, Player.position.z);
    20.     }
    This is the MouseLook script for a game I'm working on (been a while since I have done any unity :/). My question about this is: why does the sensitivity variable not change anything? When I try to run the script the mouse feels much too sensitive, and my first instinct was to add in a sensitivity variable (It's turned up to 50 here for testing reasons) and fine-tune it; however, no matter what I change the variable to, the sensitivity doesn't change. I also looked up example scripts that sometimes used deltaTime but that just made the entire thing super slow. Any help greatly appreciated.
  2. Kurt-Dekker


    Mar 16, 2013
    Whatever value you set and save in your scene or prefab will wipe out the field initializer value you change in the code above.

    This is a common source of confusion: the object is created, so at that instant in time it has the value you set in code, but before the object is put into use, if Unity has a saved scene or saved prefab that this is already in, then the value saved is overwritten.

    It's best to remove the initialization value above and instead provide it in a Reset function:

    Code (csharp):
    1. public float sensitivity;  // don't put initializer here! it can be confusing!
    3. // only runs in the editor when this is added to a GameObject.
    4. // can also be invoked from the upper right corner of the inspector (the hamburger)
    5. void Reset()
    6. {
    7.   sensitivity = 80.0f;
    8. }
    That way it is clear when the value will be changed. Once this script instance is used in a scene or prefab, only change the saved value in the scene or prefab.

    OR... if it is just a code-specified value only, make the variable private, not public, and do not decorate it for serialization.
    Last edited: Jul 4, 2021
    aynurin and uotsabchakma like this.
  3. JustYourFriendlyNeighbourhoodSlug


    Aug 24, 2020
    Thank you so much, I set the variable to private and that fixed the whole thing.
    Kurt-Dekker likes this.