unity 5.6.1 Oculus手柄输入问题
unity文档中提到 轴的 ID 是5和6,但是测试后发现,ID是6和7,很坑
1 void Update () { 2 if (Input.GetKeyDown(KeyCode.JoystickButton0)) { 3 Debug.Log ("确定"); 4 } 5 if (Input.GetKeyDown(KeyCode.JoystickButton1)) { 6 Debug.Log ("返回"); 7 } 8 Debug.Log (Input.GetAxis("OculusRL")); 9 Debug.Log (Input.GetAxis("OculusUD")); 10 }
附:https://ritchielozada.com/2016/01/16/part-11-using-an-xbox-one-controller-with-unity-on-windows-10/
Part 11: Using an Xbox One Controller with Unity on Windows 10
This post follows: Part 10: Creating the CameraRig Control Script
Unity supports several of input devices that simplify into mouse, joystick or keyboard behavior, it gets a bit challenging as the same input device will have varying input configurations across devices (PC, Mobile) and platforms (Windows 10, previous Windows, Linux, Mac, iOS, Android etc.)
There are several dual-stick/controllers available across platforms — unfortunately we are still not at the stage of having a single controller solution. I use the Xbox One Controller for PC and Xbox One, the NVidia Shield, Nexus and Fire Controllers over Bluetooth for Android Apps and when I get the time to test it out the SteelSeries Nimbus for iOS. The programming concept is very similar across the controllers, the key differences are with button and axis id assignments. I’ll be using the Xbox One controller as the development reference – might as well use the same controller platform that is included with the Oculus Rift package.
The Xbox One controller has slight changes in the button naming convention compared to the Xbox 360 version (which shares setup and usage conventions with other controllers since it has been out for quite some time.) The controller layout is the same but the Xbox 360 Back and Start buttons are now referred to as View and Menu buttons respectively.
There are several guides, blogs and sites that cover the Xbox 360 controller setup for Unity. The setup and programming concept remains the same, we have to be aware that the axis setup actually vary across platforms and in the case of Windows 10 vs. previous versions of Windows — we need to make sure that the axis assignments are updated as there is a difference in the configuration.
http://wiki.unity3d.com/index.php?title=Xbox360Controller
The Xbox One Controller is automatically recognized by Windows on a wired connection over USB or through a wireless connection using the Xbox Wireless Adapter for Windows
This is the setup I have for the Xbox One Controller on Windows 10:
(Updated November 25, 2016 to include return values and DPAD Axis Correction)
![](https://images2015.cnblogs.com/blog/1030317/201707/1030317-20170724152229778-1391296886.png)
To use the controller, we go back to the InputManager — similar to the setup covered in Part 9: Configure Input Settings for Camera Controls
Make sure to use the configuration for Buttons and Joystick Axes for each corresponding entry. There are 10 buttons and 8 axes to configure.
Button Configuration
Joystick Axis Setup
We’ll be using “Horizontal”, “Vertical”, “HorizontalTurn”, “VerticalTurn” as the entry names to match the pre-existing keyboard setup for the Left and Right Sticks.
After completing the InputManager to include the Xbox One Controller setup, running the scene should automatically work with the controller since we are using the same input names for the dual sticks.
To aid in testing our setup, create another text object in the canvas to display the controller status.
Edit CameraRig.cs, add a public UnityEngine.UI.Text entry “DebugText” so we can access the new Text object on the Canvas, Uncheck the “Raycast Target” so it does not block the Gaze. Include the following method in the CameraRig.cs script and call it from the Update() method, this will display the status of the controller buttons and axes at runtime.
1 void ControllerCheck() 2 { 3 float ltaxis = Input.GetAxis("XboxLeftTrigger"); 4 float rtaxis = Input.GetAxis("XboxRightTrigger"); 5 float dhaxis = Input.GetAxis("XboxDpadHorizontal"); 6 float dvaxis = Input.GetAxis("XboxDpadVertical"); 7 8 bool xbox_a = Input.GetButton("XboxA"); 9 bool xbox_b = Input.GetButton("XboxB"); 10 bool xbox_x = Input.GetButton("XboxX"); 11 bool xbox_y = Input.GetButton("XboxY"); 12 bool xbox_lb = Input.GetButton("XboxLB"); 13 bool xbox_rb = Input.GetButton("XboxRB"); 14 bool xbox_ls = Input.GetButton("XboxLS"); 15 bool xbox_rs = Input.GetButton("XboxRS"); 16 bool xbox_view = Input.GetButton("XboxView"); 17 bool xbox_menu = Input.GetButton("XboxMenu"); 18 19 DebugText.text = 20 string.Format( 21 "Horizontal: {14:0.000} Vertical: {15:0.000}\n" + 22 "HorizontalTurn: {16:0.000} VerticalTurn: {17:0.000}\n" + 23 "LTrigger: {0:0.000} RTrigger: {1:0.000}\n" + 24 "A: {2} B: {3} X: {4} Y:{5}\n" + 25 "LB: {6} RB: {7} LS: {8} RS:{9}\n" + 26 "View: {10} Menu: {11}\n" + 27 "Dpad-H: {12:0.000} Dpad-V: {13:0.000}\n", 28 ltaxis, rtaxis, 29 xbox_a, xbox_b, xbox_x, xbox_y, 30 xbox_lb, xbox_rb, xbox_ls, xbox_rs, 31 xbox_view, xbox_menu, 32 dhaxis, dvaxis, 33 hAxis, vAxis, 34 htAxis, vtAxis); 35 } 36 37 void Update() 38 { 39 // Camera Rig Movement Control 40 hAxis = Input.GetAxis("Horizontal"); 41 vAxis = Input.GetAxis("Vertical"); 42 aAxis = Input.GetAxis("Altitude"); 43 htAxis = Input.GetAxis("HorizontalTurn"); 44 vtAxis = Input.GetAxis("VerticalTurn"); 45 46 ControllerCheck(); 47 48 ... 49 ... 50 }
博客园Jason_c微信打赏码
如果本篇文档对你有帮助,打赏Jason_c根华子吧,他的私房钱被老婆没收了,呜呜!
![](https://files-cdn.cnblogs.com/files/Jason-c/%E5%BE%AE%E4%BF%A1%E6%89%93%E8%B5%8F%E7%A0%81.bmp)