Search Unity

Question Mouse Look stopped looking

Discussion in 'Input System' started by pompeyHuss, Jan 12, 2023.

  1. pompeyHuss

    pompeyHuss

    Joined:
    Sep 22, 2021
    Posts:
    1
    Sometimes things happen and you can't figure it out. I'm just hoping I don't sound stupid here.
    I have a had a very simple Mouse Look working fine in my game along side a movement script.
    Today it has stopped working. The file was not even open in V.Studio so I know I did not accidentally erase something. Any idea what could be stopping it working properly? I should add that the game is still picking up the mouse button click allowing my player to fire his gun.
    I have tried with some debug.log code to see what numbers it is returning for the mouse X and Y position. These Horizontal (X) figures do not change once the game is running! I have tried replacing my script with another one (borrowed from this community) and still the mouse did not turn the camera. Strangely the Vertical (Y) position returns changing values and allows the character to move the gun sight up and down.
    I am using a MainCamera and a Cinemachine Virtual Camera. However (before you ask) none of the settings on these have changed recently either.
    The whole code is:
    public class MouseLook : MonoBehaviour
    {
    //NOTE:
    /* Horizontal mouse movement rotates the body around the Y axis
    * Vertical mouse movement rotates the head around the x axis
    */

    //Declerations
    [Tooltip("The speed at which we turn aka sensitivity")]
    [SerializeField] float turnSpeed = 90f;
    [Tooltip("How far up the head can tilt")]
    [SerializeField] float headUpperAngleLimit = 10f;
    [Tooltip("How far down the head can tilt")]
    [SerializeField] float headLowerAngleLimit = -25f;


    //current rotation in relation from start in degrees
    float yaw = 0f;
    float pitch = 0f;

    //orientation of the head and body at start
    Quaternion bodyStartOrientation;
    Quaternion headStartOrientation;
    // Quaternion cameraStartOrientation;
    //head reference reference
    Transform head;


    // Start is called before the first frame update
    void Start()
    {

    head = GameObject.FindGameObjectWithTag("playerWeapon").transform;
    // cache orientations at start
    bodyStartOrientation = transform.localRotation;
    headStartOrientation = head.transform.localRotation;
    // cameraStartOrientation = mainCam.transform.localRotation;
    // lock and hide cursor
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {

    }

    // everytime physics updates our movement we use FixedUpdate to keep pace with physically simulated objects. If no physics involved use Update
    private void FixedUpdate()
    {
    // read current H mpvement and scale based upon time elapsed and movment speed
    var horizontal = Input.GetAxis("Mouse X") * Time.deltaTime * turnSpeed;
    // get V movement
    var vertical = Input.GetAxis("Mouse Y") * Time.deltaTime * turnSpeed;

    //update the yaw and pitch
    yaw += horizontal;
    pitch += vertical;


    //Clamp pitch so we dont look straight up or down
    pitch = Mathf.Clamp(pitch, headLowerAngleLimit, headUpperAngleLimit);

    //compute totation for body by rotating aorund the y axis and head by x-axis
    var bodyRotation = Quaternion.AngleAxis(yaw, Vector3.up);
    var headRotation = Quaternion.AngleAxis(pitch, Vector3.up);

    // set new rotations for head and body
    transform.localRotation = bodyRotation * bodyStartOrientation;
    head.localRotation = headRotation * headStartOrientation;
    // need to angle gun by rotation of model but only a small number eg 0.01;

    }
    }

    Yes I know I like to write a lot of comments to remind myself what I am doing.
    I'm using version 20.21.3.11f1 on Mac.
    I'm hoping there is a setting in the Preferences that I've unticked or something but I cant find it!
    All help appreciated before I scrap the whole thing and start again with the new input system.
     
    Last edited: Jan 12, 2023
  2. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    407
    Hey,

    I don't see anything wrong with your script, and based on your description of the problem, my best guess is something changed within InputManager for the Mouse X and Mouse Y axes, e.g. Name, input Type, etc.

    In the Editor, open InputManager settings window (Edit -> Project Settings -> InputManager). Expand "Axes" and then expand "Mouse X" and/or "Mouse Y". Make sure the Name field matches the axis name in your script, i.e. it's actually named "Mouse X", and the Type is "Mouse Movement". Also make sure the Sensitivity is > 0 and the other fields are empty or 0.

    I hope this helps.