6

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.

4
  • 1
    Have you read the Input.GetAxis documentation? Are Mouse X/Y valid inputs and axes?
    – A Friend
    Commented Jun 13, 2019 at 10:49
  • 1
    What version of unity are you on? Is Mouse X and Y valid in the version you have? as for example the 2 debugs work fine in 2019.1.6
    – BugFinder
    Commented Jun 13, 2019 at 11:02
  • Please share a screenshot of your Input settings with the Mouse X and the Mouse Y axes expanded so we can see the settings.
    – Ruzihm
    Commented Jun 13, 2019 at 16:04
  • I'l try to install now 2019.1.6. I use unity on Windows in Virtualbox on Centos. I use 2019.3.0a4 version. Commented Jun 16, 2019 at 11:48

2 Answers 2

2

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

enter image description here

4
0

I solved a similar problem I experienced by clearing PlayerPrefs;

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

It should solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.