首页 > 解决方案 > Input.getaxis("mouse Y") 总是返回 0

问题描述

我试图制作一个播放器控制器,您可以在其中通过 WSAD 或箭头键控制它,它工作正常,但是当我尝试添加在 XY 轴上注册鼠标移动的部分时,它出错了......

Input.GetAxis ("Mouse X") 工作正常,但 Input.GetAxis ("Mouse Y") 无论我做什么都只返回零...我尝试在输入管理器内部进行检查,没有任何问题,也没有任何问题重新丢失,我试图清除所有 PlayerPrefs,但没有奏效,我也试图重新启动我的整个 PC,但它也没有工作......

我不知道该怎么做,我已经尝试了一切,但没有运气如果你们中的任何人可以帮助我,那将是一个很大的帮助谢谢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
private Rigidbody RB;
public int Speed = 250, JumpPawer = 250, RotationSpeed = 20, MaxAngle = 130;
private Animator A;
public GameObject Came;

// Start is called before the first frame update
void Start()
{
    RB = GetComponent<Rigidbody>();
    A = GetComponent<Animator>();

}

// Update is called once per frame
void Update()
{
    //rotation
    transform.eulerAngles += new Vector3(0, Input.GetAxis("Mouse X"), 0) * Time.deltaTime * RotationSpeed;
    if (Came.transform.eulerAngles.x + (Input.GetAxis("Mouse Y") * Time.deltaTime * RotationSpeed) < MaxAngle) {
        print(Input.GetAxis("Mouse Y"));
        Came.transform.eulerAngles += new Vector3(Input.GetAxis("Mouse Y"), 0, 0) * Time.deltaTime * RotationSpeed;
    }

    //controls
    if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
        RB.velocity = transform.forward * Time.deltaTime * Speed;
        A.SetInteger("A", 1);
    }
    else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
        RB.velocity = -transform.forward * Time.deltaTime * Speed;
        A.SetInteger("A", 1);
    }
    else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
        RB.velocity = -transform.right * Time.deltaTime * Speed;
        A.SetInteger("A", 2);
    }
    else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
        RB.velocity = transform.right * Time.deltaTime * Speed;
        A.SetInteger("A", 3);
    }
    else if (Input.GetKeyDown(KeyCode.Space)) {
        RB.velocity = Vector3.up * Time.deltaTime * JumpPawer;
    }
    else
        A.SetInteger("A", 0);
}

}

标签: c#unity3d

解决方案


除非您更改了输入管理器的设置,否则实际输入不会有问题。您可以通过键入以下内容来确保问题不在于输入系统(确保它不在您之前编写的 if 语句中):

 print(Input.GetAxis(“Mouse Y”);

如果你这样做并测试游戏,上下移动鼠标,然后如果你得到一个值,那就告诉我们,问题出在 if 语句上,如果它仍然返回 0,那么统一有问题. 这是 if 语句的内容:

if (Came.transform.eulerAngles.x + (Input.GetAxis("Mouse  Y") * Time.deltaTime * RotationSpeed) < MaxAngle) {
    print(Input.GetAxis("Mouse Y"));
    Came.transform.eulerAngles += new Vector3(Input.GetAxis("Mouse Y"), 0, 0) * Time.deltaTime * RotationSpeed;
}

这里的一切都是按照我的风格写的(这很好,只是更难理解),除了括号内的代码之外,没有明显的错误。他们说:

(Came.transform.eulerAngles.x + (Input.GetAxis("Mouse  Y") * Time.deltaTime * RotationSpeed) < MaxAngle)

我唯一能发现的是,您只检查 x 旋转是否结束。这不会按照您想要的方式工作,因为当它进入时它不会被牛夹住。由于我们找不到错误,(有人可能)我们应该重写它。

无需使用 if 语句,只需使用 Mathf.Clamp(v, a, b); Mathf.Clamp 钳制一个数字 v,因此它不会低于 a 或高于 b。这对于相机脚本很有用,因为您不希望它在播放器后面旋转。你应该试试这个方法:

void Camera()
{
    float MouseY = Input.GetAxis(“Mouse Y”) * Time.deltaTime * RotationSpeed;
    Mathf.Clamp(MouseY, -89.5f, 89.5f);
    Came.transform.localEulerAngles = new Vector3(MouseY, 0, 0);
    print(MouseY);
}

我完全删除了 if 语句,因为我们使用的是 Mathf.Clamp。现在我们不必测试它是否超过或低于,因为如果这样。您应该更早地将 Time.deltaTime 和 RotationSpeed 相乘,因为如果稍后相乘,它将高于/低于钳位。然后我使用本地欧拉角,因为如果玩家装备旋转,那么相机也会旋转,本地意味着https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html。然后我使用 print 语句来测试所有内容。

我希望这会有所帮助,如果有些东西不起作用,我一定会编辑这篇文章。


推荐阅读