Unity3D 基础系列(第二期)
创建脚本
首先,新建一个场景,创建一个 Cube。
在 Assets 文件夹中创建一个 Scripts 文件夹,用来存放代码文件。
然后创建一个 PlayerController.cs 代码文件,拖拽挂载到 Cube 身上。

物体移动
打开 PlayerController.cs 编辑代码。
添加 speed 和 direction 两个变量,在 Update 方法中,通过 Input.GetAxis 获取键盘 WSAD 或 方向键 的输入,修改移动方向,并计算改变 transform.position 的数值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public float speed = 5f; public Vector3 direction;
void Start() { }
void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); direction.x = h; direction.y = v; transform.position += speed * Time.deltaTime * direction; } }
|
移动效果:

注意到,这里计算移动距离时,乘了一个 Time.deltaTime 变量,可以尝试去掉这个变量,看看效果。
Time.deltaTime 表示一帧的间隔时间,根据设备配置和运行状况,这个变量的数值会不断变化。
假设运行帧率是 30 FPS,Time.deltaTime 的数值是 1/30。
假设运行帧率是 60 FPS,Time.deltaTime 的数值是 1/60。
Update 方法是每帧调用一次,假设 Cube 每帧移动的距离为 5,在每秒 30 帧的情况下,每秒移动的距离是 150。
而在每秒 60 帧的情况下,每秒移动的距离是 300。
也就是说,不同帧率的效果表现是不同的。
为了解决这个问题,乘上一个 Time.deltaTime,使得每秒移动的距离都变为常量 5,得到相同的表现效果。
物体旋转
除了通过 Input.GetAxis 获取水平和垂直方向上的偏移量,还可以通过 Input.GetKey 获取键盘的其他按键。
例如,使用枚举 KeyCode.Q 获取键盘 Q 键,当 Q 键被持续按下时,持续修改 Y 轴 旋转角度,并使用 Quaternion.Euler 方法进行转换,使物体的旋转属性保持在一个正常的范围内,然后赋值给 transform.rotation。
反向同理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float rotateSpeed = 100f; public Vector3 rotation;
void Start() { }
void Update() {
if (Input.GetKey(KeyCode.Q)) { rotation.y += rotateSpeed * Time.deltaTime; transform.rotation = Quaternion.Euler(rotation); } else if (Input.GetKey(KeyCode.E)) { rotation.y -= rotateSpeed * Time.deltaTime; transform.rotation = Quaternion.Euler(rotation); } } }
|
旋转效果:

物体缩放
了解了键盘按键,可以再看看鼠标按键。
可以通过 Input.GetMouseButtonDown 方法获取鼠标按键按下的状态,传入 0 则是鼠标左键,传入 1 则是鼠标右键。
这里修改了 transform.localScale,让 Cube 放大或变回原样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float bigger = 2f; public float normal = 1f;
void Start() { }
void Update() {
if (Input.GetMouseButtonDown(0)) { transform.localScale = Vector3.one * bigger; } else if (Input.GetMouseButtonDown(1)) { transform.localScale = Vector3.one * normal; } } }
|
缩放效果:

按键总结
下面列举了一系列按键输入。
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")
|
Input Manager 可以自定义按键名称和对应的键盘按键。
