三、输入
1、输入管理器
请从Unity的主菜单中转到“编辑”>“项目设置”,然后从右侧的导航中选择“输入管理器”->轴线。 注意:还要开启《使用物理按键》打钩上,非常重要。
->使用物理按键。 意思是强制使用: QWERTY布局:美国、英国等常用布局 (在QWERTY键盘上,W、A、S、D) "Q"键始终代表第一行最左边的字母键 "W"键始终代表第二行第二个键 不管用户使用哪种语言布局的键盘。
类型的控件:
1. Key(键)
定义:物理键盘上的按键
特点:
只能是单个按键(如W、A、S、D、空格键等)
值范围:0(未按下)或 1(按下)
不能同时按下多个键来产生中间值
2. Button(按钮)
定义:物理控制器上的按钮(游戏手柄、摇杆等)
特点:
可以是游戏手柄上的任意按钮(如Xbox的A、B、X、Y按钮)
值范围:0(未按下)或 1(按下)
同样只能产生离散的0/1值
3. Axis(轴)
定义:可以产生连续值的输入(通常用于模拟输入)
特点:
值范围:-1 到 +1(或其他自定义范围)
可以产生中间值(如0.5、-0.3等)
通常用于:
游戏手柄的摇杆
模拟控制器的触发器
鼠标移动
需要精细控制的输入
实际应用示例
// 获取轴的值(连续变化)
float horizontal = Input.GetAxis("Horizontal");
// 可能返回 -1.0f 到 1.0f 之间的任意值
// 获取按键状态(离散)
if (Input.GetKeyDown(KeyCode.Space))
{
// 只有在按下的瞬间触发
}
// 获取按钮状态(离散)
if (Input.GetButton("Jump"))
{
// 按钮被持续按下时一直触发
}
总结:Key和Button是离散输入(只有0/1两种状态),而Axis是连续输入(可以有任意中间值)。
2、移动设备输入(在移动端才有效果,pc上是不能看的)
多点触控屏幕:
以下是一个示例脚本,当用户在屏幕上轻点时,会拍摄光线:
using UnityEngine;
public class TouchInput : MonoBehaviour
{
GameObject particle;
void Update()
{
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)//TouchPhase.Began 表示触摸开始的那一刻
{
// 从当前触摸坐标构造一条射线
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray))
{
// 如果命中,则创建一个粒子
Instantiate(particle, transform.position, transform.rotation);
}
}
}
}
}
fingerId 独特的指数 The unique index for a touch.
position 触摸的屏幕位置 The screen position of the touch.
deltaPosition 屏幕位置自上一帧以来发生变化 The screen position change since the last frame.
deltaTime 自上次州级变更以来,已经过了大量时间 Amount of time that has passed since the last state change.
tapCount iPhone/iPad 屏幕能够区分用户快速的手指轻触。此计数器可让您了解用户在不向两侧移动手指的情况下点击屏幕的次数。Android 设备不计数点击次数,此字段始终为 The iPhone/iPad screen is able to distinguish quick finger taps by the user. This counter will let you know how many times the user has tapped the screen without moving a finger to the sides. Android devices do not count number of taps, this field is always 1.
phase 描述触摸的状态,这有助于判断用户是否刚刚开始触摸屏幕、只是移动手指或只是抬起手指 Describes the state of the touch, which can help you determine whether the user has just started to touch screen, just moved their finger or just lifted their finger.
Began 一根手指轻轻碰了碰屏幕 A finger just touched the screen.
Moved 屏幕上的一根手指移动了 A finger moved on the screen.
Stationary 手指正在触摸屏幕,但自上一帧以来就一直没有移动 A finger is touching the screen but hasn’t moved since the last frame.
Ended 屏幕上抬起了一根手指。这是触碰的最后阶段 A finger was lifted from the screen. This is the final phase of a touch.
Canceled 系统取消了对触摸的追踪,例如当用户将设备贴在面部或同时进行五次以上触摸时。这是触碰的最后阶段 The system cancelled tracking for the touch, as when (for example) the user puts the device to their face or more than five touches happened simultaneously. This is the final phase of a touch.
鼠标模拟:
除了原生触控,Unity iOS/Android 还提供鼠标模拟功能。
Input可以使用鼠标功能。注意:iOS/Android 设备专为支持多个手指触摸而设计,而鼠标功能将仅支持一次手指触摸。
此外,手机上的手指触摸 可以从一个区域移动到另一个区域,且它们之间没有移动。
移动设备上的鼠标模拟:能够提供移动,因此与触摸输入相比有很大不同。 建议在早期开发时使用鼠标模拟,还要尽快用触摸输入。
加速度计
随着移动设备的移动,内置的加速度计会报告线性加速 三维空间中三个主轴的变化。加速 每个轴沿线直接由硬件报告为G-force值。一个值 1.0 表示沿给定轴的约 +1g 负载,而值为 –1.0 表示 –1g。如果你将设备直立着(主键在 底部) 前方,右侧的 X 轴为正轴,Y 轴为 正向上,Z轴正向朝向你。
可以通过访问 Input.acceleration 属性来检索加速度计值。
以下是一个使用加速度计移动对象的示例脚本:
using UnityEngine;
public class Accelerometer : MonoBehaviour
{
float speed = 10.0f;
void Update()
{
Vector3 dir = Vector3.zero;
// 我们假设设备与地面平行,
// 主屏幕按钮位于右侧
// 将设备的加速度轴重新映射到游戏坐标:
// 1) 设备的 XY 平面映射到 XZ 平面
// 2) 绕 Y 轴旋转 90 度
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
// 将加速度矢量限制为单位球体
if (dir.sqrMagnitude > 1)
dir.Normalize();
// 使其每秒移动 10 米而不是每帧 10 米...
dir *= Time.deltaTime;
// 移动对象
transform.Translate(dir * speed);
}
}
发表评论 取消回复