c# - Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") 总是返回 0
问题描述
我为 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
.
解决方案
推荐阅读
- colors - 需要帮助重写“Transp 0 : 100”
- node.js - npx 不再进行无安装运行了吗?
- python - 找不到名为 blog.eg 的模块 django
- python - 根据用户消息编写 Twiml
- javascript - 如果它们是 for 循环的一部分,你怎么不把图片放在一起呢?
- r - 如何以编程方式将值传递给 R 提示符(创建密钥环)
- node.js - 如何更新一个大项目的续集?
- c - C函数指针和结构
- jenkins - 在 Jenkins 中创建构建时出现 FileAlreadyExistsException 错误
- javascript - onClick如何切换类