- Home /
Managing input for several players using pads in the same computer.
The "tl;dr" question about input, could be: Which is the best way to manage several players (up to 4), each using its own Xbox 360 controller, in Unity?
The long version follows:
As the tl;dr says, I'm trying to manage 4 players in the same screen. I read some time ago about the input manager that Unity uses, but read somewhere that it was really buggy, and that its better to write your own, so thats what I did.
I have my "inputController" which receives a set of pre-configured keys (originally from keyboard) and asks about Input.GetKeyDown(key), Input.getKeyUp(key) and so. The characters moved along right and everything was nice. Then, I "upgraded" it to read also axis from a joystick (Input.getAxis("AxisName"+playerNumber)). Using a custom set of keys (joystickButton) I managed to get one pad working to move a second character, using 4 buttons, the left thumbstick and the triggers. So, the InputManager has now 4 "custom" axis: 2 for the thumbstick (horizontal and vertical) and the other two for both triggers.
My first problem is that I don't know how can I control a second pad plugged in the same computer. For what I've seen here, for the buttons is easy as "joystickButton", where "n" is the number of the player (i.e. "Joystick4Button0"). The real problem comes with the axis. As I'm reading them from the "custom axis" I make in the Input inspector, do I need to set up 4 custom axis for each new player, and make each player "read" the designated axis? can I have several "Inputs", each copy of the other, but with different bindings?
The second problem is that what I've seen in the documentation is puzzling me now: It seems that the idea is to allow the player redefine the controls through the "Input" tab on the start menu, but, of course, what I'm doing kind of overrides it completely. Is Unity's standard Input handling reliable enough to be used, should I convert my class to work with it or I'm better handling everything by myself and not paying attention to this module?
Answer by Neurus_Ex · Mar 19, 2015 at 08:15 AM
Got an answer on a spanish forum where I asked, so here is what's going on:
The InputManager for Unity seems to be something to avoid, except when adding new axes to the game.
For each pad, to handle its buttons, you need to call input.JoystickButton (As my previous example, input.Joystick7Button1).
Also for each pad, you need to create custom axis. As it (kind of) says here, each thumbstick needs 2 axis (vertical and horizontal), and each trigger needs one axis. This is, for each pad, a total of 6, euh, axii? Each one to be defined individually in the InputManager.
- So, to "answer" my question, from the Input inspector, for the pad n to be read, you need to define a specific axis (i.e. "RightTriggerN"), specify its axis (being a trigger, is 9th axis for right trigger, 10th axis for left trigger, for the thumbsticks would be X, Y, 4th and 5th), then mark on the "Joy Num" dropdown, "Joystick N." For the buttons, would be Input.JoystickNButton
I'll add this nice screenshot to illustrate the bold paragraph:
Here, we have how to access to the InputManager (left), and 4 examples of different axes, from the top left to the lower right, the X axis (left thumbstick) for the first and fourth axes, and both axis for the triggers of the second joystick.
Your answer
Follow this Question
Related Questions
Joystick Axis 9&10 with more than 2 gamepads 0 Answers
Quirk in Input.GetAxis 0 Answers
How do I add a value to anywhere Input.GetAxis("Horizontal") is called? 1 Answer
Input Snap option not returning 0? 0 Answers
input resets after scene change,Held Down Input not responding after scene change. 0 Answers