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