Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Player MouseLook does not work in the X axis

Discussion in 'Getting Started' started by Jameson101, May 27, 2021.

  1. Jameson101

    Jameson101

    Joined:
    Dec 25, 2020
    Posts:
    1
    Hello there, I am a newbie in unity. I decided to try to make a first person movement, as a form of exercise to pratice more of first person games. I know a bit of the engine (and when I say it's a bit, it's really a bit).

    I'm following a tutorial made by Brackeys, however, I followed all the steps he made and understanding the science behind the MouseLook script and I'm having a problem. You see, I can move my mouse to the Y axis but not in the X axis, I looked to the code several times and I don't know what the problem is. I tried to change the unity version by the one he's using in the video, but still, it didn't worked. I went on google trying to find the solution for the problem but I got no sucess. If someone could help me with this, I would be very thankful for that.

    Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.  
    8.     public float mouseSensitivity = 100f;
    9.  
    10.     public Transform playerBody;
    11.  
    12.     float xRotation = 0f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.  
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    27.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    28.  
    29.         xRotation -= mouseY;
    30.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    31.  
    32.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    33.         playerBody.Rotate(Vector3.up * mouseX);
    34.  
    35.     }
    36. }
    37.