提要本文主要介紹了如何在Unity中接入Xbox360的手柄。當我們調(diào)Input GetAxis( "Horizontal ")漱抓,我們調(diào)了什么Unity中所有關(guān)于輸入的設(shè)置都在Edit -> Project Setting -> Input里面恕齐,點開之后,Inspector面板
提要
本文主要介紹了如何在Unity中接入Xbox360的手柄显歧。
當我們調(diào)Input.GetAxis("Horizontal")追迟,我們調(diào)了什么
Unity中所有關(guān)于輸入的設(shè)置都在Edit -> Project Setting -> Input里面,點開之后敦间,Inspector面板中就會顯示當前的輸入設(shè)置。
![](http://upload-images.jianshu.io/upload_images/1771334-f4bf1f8b6ba44044.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
幾個重要選項的含義有:
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.
上圖中厢绝,定義了鍵盤上的a鍵昔汉,d鍵,小鍵盤上的左和右控制了 "Horizontal" 的輸入靶病。而且實際情況中直接插上360的手柄,通過左邊的搖桿涕侈, Input.GetAxis("Horizontal")也能夠獲得輸入值煤辨,原因是后面還定義了一個 "Horizontal" 的輸入。
![](http://upload-images.jianshu.io/upload_images/1771334-1c93f5594e9e0111.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
這里定義了一個 Joystick Axis類型的輸入众辨,輸入的軸是X, 從所有手柄獲取輸入技肩。因為有了這個浮声,Input.GetAxis("Horizontal")就可以直接獲取手柄的值了旋奢。接下來看怎么擴展到手柄的按鍵和其他的搖桿。
擴展到手柄的按鍵和其他的搖桿
首先看一下手柄按鍵 的分布圖屉符。
![](http://upload-images.jianshu.io/upload_images/1771334-2168aef5c045bcc9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
有了這個就可以自定義手柄的輸入了锹引。
比如要 通過 Input.GetButtonDown("Action"); 來判定是否攻擊,關(guān)聯(lián)的是手柄上的A鍵嫌变, 我們只需要如下圖設(shè)置
![](http://upload-images.jianshu.io/upload_images/1771334-2c465d8164f070c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
再比如右邊的搖桿腾啥,橫向是這樣
![](http://upload-images.jianshu.io/upload_images/1771334-931ee41e79f87769.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
取值的話是這樣
Input.GetAxis("RightHorizontal")) Input.GetAxis("RightVertical"))
其他的按鍵或者搖桿用同樣的方式設(shè)置就可以了。
參考
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
ButtonsSquare = joystick button 0X = joystick button 1Circle = joystick button 2Triangle= joystick button 3L1 = joystick button 4R1 = joystick button 5L2 = joystick button 6R2 = joystick button 7Share = joystick button 8Options = joystick button 9L3 = joystick button 10R3 = joystick button 11PS = joystick button 12PadPress= joystick button 13Axes:LeftStickX = X-AxisLeftStickY = Y-Axis (Inverted?)RightStickX = 3rd AxisRightStickY = 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 AxisDPadY = 8th Axis (Inverted?)