找回密码
 立即注册
楼主: 小米

[脚本] 控制输入系统 - Input 类实例讲解

[复制链接]
发表于 2012-6-2 23:05 | 显示全部楼层 |阅读模式
做游戏少不了控制,但是一个成熟的游戏引擎,是不能简单仅仅获取键盘中或者遥感确定的按键来控制,要考虑到用户更改游戏按键的情况,当然也得考虑到不同设备的不通输入方式,比如U3D是可以运行在iphone上的,iphone可没有键盘和上下左右键,他有平衡控制(简单化解为水平和垂直两个方向度数),如果用户使用飞行或者汽车方向盘手柄,那就不能简单用一个按键来描述了,需要有一个度数的问题(也可以简单分解为水平和垂直两个方向度数)。

U3D提供了一个Input类封装控制输入,不做详细解释,在后面我会举一些常用的例子来说明。
Class Variables
mousePositionThe current mouse position in pixel coordinates. (Read Only)
anyKeyIs any key or mouse button currently held down? (Read Only)
anyKeyDownReturns true the first frame the user hits any key or mouse button (Read Only).
inputStringReturns the keyboard input entered this frame (Read Only).
Class Functions
GetAxisReturns the value of the virtual axis identified by axisName.
GetAxisRawReturns the value of the virtual axis identified by axisName with no smoothing filtering applied.
GetButtonReturns true while the virtual button identified by buttonName is held down.
GetButtonDownReturns true during the frame the user pressed down the virtual button identified by buttonName.
GetButtonUpReturns true the first frame the user releases the virtual button identified by buttonName.
GetKeyReturns true while the user holds down the key identified by name. Think auto fire.
GetKeyDownReturns true during the frame the user starts pressing down the key identified by name.
GetKeyUpReturns true during the frame the user releases the key identified by name.
GetMouseButtonReturns whether the the given mouse button is held down.
GetMouseButtonDownReturns true during the frame the user pressed the given mouse button.
GetMouseButtonUpReturns true during the frame the user releases the given mouse button.
ResetInputAxesResets all input. After ResetInputAxes all axes return to 0 and all buttons return to 0 for one frame.

---------------------------------------------------------------------------------------------------------------------------------
获取键盘某一(这里是空格键)按键状态(bool):Input.GetKeyDown(KeyCode.Space) 这是最不通用的写法,不推荐
获取虚拟按键(这里是Jump)按键状态(bool):Input.GetButton("Jump") 推荐用这种写法,用户可以设置按键Jump为空格键(默认就是空格)
获取遥感(或iphone感应)垂直轴力度(是一个0-1之间的float):Input.GetAxis("Vertical“) 推荐写法,键盘的话默认按w或者up会瞬间提到1,要是遥感的话可以控制一个度,再乘以力的方向向量的话,就可以控制汽车之类的加速了。如果你直接写成键盘的w或者d,那就没办法控制力度了。
获取遥感(或iphone感应)水平轴力度(同上):Input.GetAxis("Horizontal")
获取鼠标中键状态:Input.GetAxis("Mouse ScrollWheel")

注:有些遥感可能水平方向会转超过1或者小于0的值出来(例如汽车遥感),为了避免错误的计算(乘以负数的话向量的方向可是会相反的),可以配合数学函数Mathf.Clamp01()来固定他的值在0和1之间。例如:
motor = Mathf.Clamp01(Input.GetAxis("Vertical")); //设置汽车引擎力度为垂直方向力度



发表于 2014-5-8 14:21 | 显示全部楼层
好东西 学习了 学习了
发表于 2017-4-6 16:42 | 显示全部楼层
楼主是超人
发表于 2017-4-6 16:54 | 显示全部楼层
难得一见的好帖
发表于 2017-4-6 17:08 | 显示全部楼层
说的非常好
发表于 2017-4-6 16:43 | 显示全部楼层
很好哦
发表于 2017-4-6 16:13 | 显示全部楼层
不错不错
发表于 2017-8-9 18:08 | 显示全部楼层
楼主是超人
发表于 2017-8-9 18:10 | 显示全部楼层
好帖就是要顶
发表于 2017-8-9 18:13 | 显示全部楼层
说的非常好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-5 20:24 , Processed in 0.148588 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表