开头总结没见过吧:
GetKey传入参数为按键名称或KeyCode,而GetButton、GetAxis传入参数只能为InputManager中定义的轴键。
GetKey,GetKeyDown,GetKeyUp可以通过传入String类型的按键名称来获得按键。
按键名称如下:
- 普通键:“a”,“b”,“c”......
- 数字键:“1”,“2”,“3”,......
- 箭头键:“up”, “down”, “left”, “right”
- 键盘键:“[1]”, “[2]”, “[3]”, “[+]”, “[equals]”
- 修改键:“right shift”, “left shift”, “right ctrl”, “left ctrl”, “right alt”, “left alt”, “right cmd”, “left cmd”
- 鼠标按钮:“mouse 0”, “mouse 1”, “mouse 2”, …
- 操纵杆按钮(来自任意操纵杆):“joystick button 0”, “joystick button 1”, “joystick button 2”, …
- 操纵杆按钮(来自特定操纵杆):“joystick 1 button 0”, “joystick 1 button 1”, “joystick 2 button 0”, …
- 特殊键: “backspace”, “tab”, “return”, “escape”, “space”, “delete”, “enter”, “insert”, “home”, “end”, “page up”, “page down”
- 功能键:“f1”,“f2”,“f3”,...
除此之外,GetKey,GetKeyDown,GetKeyUp除了可以传入上述按键名称之外还可以直接通过KeyCode直接获得按键,这里有一段代码,顺道提了GetKey的用法(这是很简单的):
bool held = Input.GetKey(KeyCode.Space);
按住空格键时,GetKey会当按键被按住时持续返回true值,held被持续赋值为true,直到空格键松开为止。
GetKeyDown在按键按下瞬间返回true,GetKeyUp则在按键松开时返回true;
KeyCode.R表示R键,KeyCode.T表示T键这样,特殊键请自行查询api :KeyCode
而GetButton,GetButtonDown,GetButtonUp的返回情况跟上面一样,不过这里传入的参数只能是InputManager中定义的轴键。
bool down = Input.GetButtonDown("Jump");
打开下面看到InputManager:
Edit->project settings->Input打开input manager
GetAxis返回的是一个正1到负1的float值(1 ~ -1)(这里注意:这是对于键盘和操纵杆输入,该值将在-1 ... 1的范围内。如果轴设置为增量鼠标移动,则鼠标增量乘以轴灵敏度(见InputManager属性)),传入参数是InputManager中定义的轴。
public float speed = 10.0f; //行驶速度
public float rotationSpeed = 100.0f; //转向速度
void Update()
{
//使用上下箭头或者W、S键来控制前进后退
float translation = Input.GetAxis("Vertical") * speed;
//使用左右箭头或者A、D键来控制左右旋转
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
//在x-z平面上移动
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
float deltaX = Input.GetAxis("Mouse X");
GetAxis从0变到+1或-1,每一步增加/减少0.05f。 GetAxisRaw立即从0更改为1或-1。
InputManager属性(这里如果有错漏请斧正):
每个项目在创建时都具有以下默认输入轴(15个):
- Horizontal和Vertical映射到w,a,s,d和箭头键。
- Fire1,Fire2,Fire3分别映射到Control,Option(Alt)和Command。
- MouseX和MouseY映射到鼠标移动的增量。
Axes | Contains all the defined input axes for the current project: Size is the number of different input axes in this project, Element 0, 1, … are the particular axes to modify.。(输入轴数量) |
Property: | Function: | |
---|---|---|
Name | The name of the string used to check this axis from a script. (轴名,该String将可作为 Input.GetAxis() 的传入参数) | |
Descriptive Name | Positive value name displayed in the input tab of the Configuration dialog for standalone builds .(见下图,Descriptive Name会显示在游戏build and run后打开时会进入的configuration中input中) | |
Descriptive Negative Name | Negative value name displayed in the Input tab of the Configuration dialog for standalone builds.(configuration->input中负向按钮描述) | |
Negative Button | The button used to push the axis in the negative direction.(负向按钮对应的键(见本文开头按键名称)) | |
Positive Button | The button used to push the axis in the positive direction.(正向按钮对应的键) | |
Alt Negative Button | Alternative button used to push the axis in the negative direction.(正向等效替代按钮) | |
Alt Positive Button | Alternative button used to push the axis in the positive direction.(反向等效替代按钮) | |
Gravity | Speed in units per second that the axis falls toward neutral when no buttons are pressed.(回弹,回到中心的速度) | |
Dead | Size of the analog dead zone. All analog device values within this range result map to neutral.(此范围内的值都将返回为无效值) | |
Sensitivity | Speed in units per second that the axis will move toward the target value. This is for digital devices only.(灵敏度) | |
Snap | If enabled, the axis value will reset to zero when pressing a button of the opposite direction.(启用,则按下相反方向的按钮时,轴值将重置为零。) | |
Invert | If enabled, the Negative Buttons provide a positive value, and vice-versa.(启用,则输出反转) | |
Type | The type of inputs that will control this axis.(键盘/鼠标按钮 (Key/Mouse Button)用于任何类型的按钮,鼠标移动 (Mouse Movement) 用于鼠标增量和滚轮,游戏杆轴 (Joystick Axis) 用于模拟游戏杆轴) | |
Axis | The axis of a connected device that will control this axis.(设备轴将控制此轴) | |
Joy Num | The connected Joystick that will control this axis.(将控制第几joystick
|
(unity项目 run后打开时就会弹出此设置框)