Input.GetAxis(“ Mouse X”),Input.GetAxis(“ Mouse Y”)始终返回0

时间:2019-06-13 10:36:51

标签: c# unity3d

我为3d射击者添加了主要用户对象,为其附加了相机,并试图捕获脚本代码中的鼠标移动,并附加到玩家游戏对象上。但是不能使用Input.GetAxis("Mouse X")Input.GetAxis("Mouse Y"),因为它们始终为零。为什么?我做错了什么? Input.GetAxis("Vertical")Input.GetAxis("Horizontal")的键效果很好。

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;
    }

我希望Input.GetAxis("Mouse X")的输出为-1到1,但实际输出为0。我在Debug.Log中看到它。

2 个答案:

答案 0 :(得分:1)

检查您的单位输入设置,Mouse XMouse Y的定义应如下所示:

enter image description here

答案 1 :(得分:0)

我通过清除PlayerPrefs解决了我遇到的类似问题;

转到Editor Window Menus > "Edit" > "Clear All PlayerPrefs"

应该可以解决问题。