这篇文章研究一下通过键盘和鼠标对游戏物体进行移动或是其他操作。
先来看看简单的按键输入。
一、键盘按键
创建一个脚本,挂载到一个 Cube 上,在 Update 函数监听键盘按键。
使用的接口就是 Input.GetKeyDown,这个接口只会在按下按键的时候,返回一次 true。也就是说,长按键盘按键时,后续只会返回 false。
如果想要按住键盘的按键,持续不断地返回 true,就可以使用 Input.GetKey 接口。
(注:Input.GetKey 在按下的瞬间,可能会调用好几次,因为 Update 是每帧执行一次,每帧 0.016 秒左右)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| void Update() { if (Input.GetKeyDown(KeyCode.W)) { transform.position += Vector3.up; } else if (Input.GetKeyDown(KeyCode.S)) { transform.position += Vector3.down; } else if (Input.GetKeyDown(KeyCode.A)) { transform.position += Vector3.left; } else if (Input.GetKeyDown(KeyCode.D)) { transform.position += Vector3.right; } }
|
当然,在移动游戏物体时,不会逐个按键去监听,而是使用 Input.GetAxis 去监听水平或垂直方向上的偏移量。
这里声明了一个速度和一个方向向量,通过获取水平和垂直方向的偏移量,修改方向向量,计算出每帧的移动距离,修改物体位置,达到通过键盘方向键移动游戏物体的目的。
上面通过监听每个键盘按键的例子,只对 WSAD 有效。而下面这个例子,对 WSAD 和 方向键 都有效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public float speed = 5f; public Vector3 direction;
void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); direction.x = h; direction.y = v; transform.position += speed * Time.deltaTime * direction; }
|
为什么是 Horizontal 和 Vertical 呢?有没有其他的参数呢?
答案是有的。
打开 Edit -> Project Settings -> Input Manager,可以看到 Horizontal 和 Vertical 两个元素,里面都包含了一系列的参数设置。
可以添加、修改、删除一些元素,填好 Name(按键名)、Positive Button(正向)、Negative Button(反向)就可以通过 Input.GetAxis 获取按键输入啦。

此外,还有 Input.GetAxisRaw,它和 Input.GetAxis 的区别是,它的返回值是 -1 0 1 三个整数,而 Input.GetAxis 会逐渐从 0 到 1 或 -1 进行变化,有一个缓慢增速的过程。
除了 Input.GetKeyDown,还有 Input.GetButtonDown,它们都可以获取 Input Manager 设置的轴键,而 GetKeyDown 可以额外通过 KeyCode 枚举变量获取按键,GetButtonDown 则只能获取轴键(GetAxis 同理)。
二、鼠标按键
鼠标的三个按键也都是通过 Input 类的接口来监听。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| void Update() { if (Input.GetMouseButtonDown(0)) { Debug.Log("按下鼠标左键"); } else if (Input.GetMouseButtonDown(1)) { Debug.Log("按下鼠标右键"); } else if (Input.GetMouseButtonDown(2)) { Debug.Log("按下鼠标中键"); } }
|
鼠标在屏幕上移动时,也同样可以获取到移动的坐标偏移量。
1 2 3 4 5 6
| void Update() { float x = Input.GetAxis("Mouse X"); float y = Input.GetAxis("Mouse Y"); Debug.Log($"mouse x = {x} y = {y}"); }
|
三、总结
下面列出了一些常用的按键接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| Input.GetKey(KeyCode.W) Input.GetKeyDown(KeyCode.W) Input.GetKeyUp(KeyCode.W)
Input.GetButton("Jump") Input.GetButtonDown("Jump") Input.GetButtonUp("Jump")
Input.GetAxis("Horizontal") Input.GetAxis("Vertical") Input.GetAxisRaw("Horizontal") Input.GetAxisRaw("Vertical")
Input.GetMouseButton(0) Input.GetMouseButtonDown(1) Input.GetMouseButtonUp(2)
Input.GetAxis("Mouse X") Input.GetAxis("Mouse Y")
|