提要
本文主要介紹了怎樣在Unity中接入Xbox360的手柄。
當我們調Input.GetAxis("Horizontal"),我們調了什么
Unity中全部關於輸入的設置都在Edit -> Project Setting -> Input里面,點開之后,Inspector面板中就會顯示當前的輸入設置。
幾個重要選項的含義有:
Gravity | How fast will the input recenter. Only used when the Type is key / mouse button. |
Dead | Any positive or negative values that are less than this number will register as zero. Useful for joysticks. |
Sensitivity | For keyboard input, a larger value will result in faster response time. A lower value will be more smooth. For Mouse delta the value will scale the actual mouse delta. |
Snap | If enabled, the axis value will be immediately reset to zero after it receives opposite inputs. Only used when the Type is key / mouse button. |
Invert | If enabled, the positive buttons will send negative values to the axis, and vice versa. |
Type | Use Key / Mouse Button for any kind of buttons, Mouse Movement for mouse delta and scrollwheels, Joystick Axis for analog joystick axes and Window Movement for when the user shakes the window. |
這里定義了一個 Joystick Axis類型的輸入,輸入的軸是X。 從全部手柄獲取輸入。
由於有了這個,Input.GetAxis("Horizontal")就能夠直接獲取手柄的值了。
接下來看怎么擴展到手柄的按鍵和其它的搖桿。
擴展到手柄的按鍵和其它的搖桿
首先看一下手柄按鍵 的分布圖。
有了這個就能夠自己定義手柄的輸入了。
比方要 通過 Input.GetButtonDown("Action"); 來判定是否攻擊,關聯的是手柄上的A鍵, 我們僅僅須要例如以下圖設置
再比方右邊的搖桿,橫向是這樣
縱向是這種
取值的話是這樣
Input.GetAxis("RightHorizontal")) Input.GetAxis("RightVertical"))
其它的按鍵或者搖桿用相同的方式設置就能夠了。
參考
Input Manager - http://docs.unity3d.com/Manual/class-InputManager.html
Xbox 360 Joystick Controller + - Unity http://bobstudios-en.blogspot.com/2012/09/xbox-360-joystick-controller-unity.html
附錄:sony ps4 joystick map
Buttons
Square = joystick button 0
X = joystick button 1
Circle = joystick button 2
Triangle= joystick button 3
L1 = joystick button 4
R1 = joystick button 5
L2 = joystick button 6
R2 = joystick button 7
Share = joystick button 8
Options = joystick button 9
L3 = joystick button 10
R3 = joystick button 11
PS = joystick button 12
PadPress= joystick button 13
Axes:
LeftStickX = X-Axis
LeftStickY = Y-Axis (Inverted?)
RightStickX = 3rd Axis
RightStickY = 4th Axis (Inverted?
)
L2 = 5th Axis (-1.0f to 1.0f range, unpressed is -1.0f)
R2 = 6th Axis (-1.0f to 1.0f range, unpressed is -1.0f)
DPadX = 7th Axis
DPadY = 8th Axis (Inverted?)