Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") always returns 0

Viewed 3815

I added main user object for a 3d shooter, attached camera for it and trying to catch mouse moving in the code of the script, attached to the player game object. But can not use Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), because they always zero. Why? What I did wrong? Input.GetAxis("Vertical") and Input.GetAxis("Horizontal") for keys work good.

using UnityEngine;
using System.Collections;

public class MouseLook : MonoBehaviour {

  public enum RotationAxes {
    MouseXAndY = 0,
    MouseX = 1,
    MouseY = 2
  }

  public RotationAxes axes = RotationAxes.MouseXAndY;
  public float sensitivityHor = 9.0f;
  public float sensitivityVert = 9.0f;
  public float minimumVert = -45.0f;
  public float maximumVert = 45.0f;
  private float _rotationX = 0;

  void Start() {
    Rigidbody body = GetComponent<Rigidbody>();
    if (body != null)
      body.freezeRotation = true;
  }

  void Update() {
    Debug.Log(Input.GetAxis("Mouse X"));
    Debug.Log(Input.GetAxis("Mouse Y"));
    if (axes == RotationAxes.MouseX) {
      transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
    } else if (axes == RotationAxes.MouseY) {
      _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
      _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
      float rotationY = transform.localEulerAngles.y;
      transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
    } else {
      _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
      _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
      float delta = Input.GetAxis("Mouse X") * sensitivityHor;
      float rotationY = transform.localEulerAngles.y + delta;
    }

I expect the output of Input.GetAxis("Mouse X") to be from -1 to 1, but the actual output is 0. I see it in Debug.Log.

2 Answers

Check your unity input settings, Mouse X and Mouse Y Should be defined as shown below:

enter image description here

I solved a similar problem I experienced by clearing PlayerPrefs;

Go to Editor Window Menus > "Edit" > "Clear All PlayerPrefs"

It should solve the issue.

Related