GetKey、GetButton,GetAxis及Input Manager

开头总结没见过吧:

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个):

  • HorizontalVertical映射到w,a,s,d和箭头键。
  • Fire1Fire2Fire3分别映射到Control,Option(Alt)和Command。
  • MouseXMouseY映射到鼠标移动的增量。

AxesContains 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:
NameThe name of the string used to check this axis from a script. (轴名,该String将可作为 Input.GetAxis() 的传入参数) 
Descriptive NamePositive value name displayed in the input tab of the Configuration dialog for standalone builds
.(见下图,Descriptive Name会显示在游戏build and run后打开时会进入的configuration中input中)
Descriptive Negative NameNegative value name displayed in the Input tab of the Configuration dialog for standalone builds.(configuration->input中负向按钮描述)
Negative ButtonThe button used to push the axis in the negative direction.(负向按钮对应的键(见本文开头按键名称))
Positive ButtonThe button used to push the axis in the positive direction.(正向按钮对应的键)
Alt Negative ButtonAlternative button used to push the axis in the negative direction.(正向等效替代按钮)
Alt Positive ButtonAlternative button used to push the axis in the positive direction.(反向等效替代按钮)
GravitySpeed in units per second that the axis falls toward neutral when no buttons are pressed.(回弹,回到中心的速度)
DeadSize of the analog dead zone. All analog device values within this range result map to neutral.(此范围内的值都将返回为无效值)
SensitivitySpeed in units per second that the axis will move toward the target value. This is for digital devices only.(灵敏度)
SnapIf enabled, the axis value will reset to zero when pressing a button of the opposite direction.(启用,则按下相反方向的按钮时,轴值将重置为零。)
InvertIf enabled, the Negative Buttons provide a positive value, and vice-versa.(启用,则输出反转)
TypeThe type of inputs that will control this axis.(键盘/鼠标按钮 (Key/Mouse Button)用于任何类型的按钮,鼠标移动 (Mouse Movement) 用于鼠标增量和滚轮,游戏杆轴 (Joystick Axis) 用于模拟游戏杆轴)
AxisThe axis of a connected device that will control this axis.(设备轴将控制此轴)
Joy NumThe connected Joystick that will control this axis.(将控制第几joystick
默认情况下设置为检索来自所有游戏杆的输入。仅用于输入轴,不包括按钮。

 

(unity项目 run后打开时就会弹出此设置框)

 

  • 29
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值