Multiple joystick input


I want to build a game which supports up to 4 gamepads and keyboard/mouse input. I create the keyboard entries and the gamepad entries for one gamepad in the Input Manager. Is it possible to get the number of the joystick from the Input class while scripting? Is there an better way instead of creating the input entries for each gamepad (4 times + keyboard) in the Input Manager?

I found the Unity-XboxCtrlrInput plugin for Unity. Is there no other way to work with multiple joysticks and Unity in the yearn 2014?

Thanks for reading.

It’s not about the number of joysticks, it’s the number of players. If you want to have different inputs for each player, then you need to have different inputs for each player. So you don’t need to create another set of controls for the keyboard – just apply those to player 1. Users can remap them as they see fit. (Of course, this isn’t as user-friendly as letting players choose which player is using which device, but it gives them full control over remapping through the launcher.)

Just to make sure we’re on the same page, is this the procedure you’re following:

Name your inputs after the player and always use numbered joysticks.

  • Axes: Create “P1_Horizontal” and select “Joystick 1” from the “Joy Num” dropdown.
  • Button: Create “P1_Jump” use “joystick 1 button 1”.
  • Duplicate these for P2 and use “joystick 2”

In your player controller, add a public variable for the prefix (“P1_”) and when calling GetAxes, prepend that prefix:

public string playerPrefix = "P1_";

    bool is_jump = Input.GetButtonDown(playerPrefix + "Jump");

(Or define public variables for all of your inputs so you can pass them directly into GetButtonDown if you’re worried about the performance impact of string concatenation.)

When you add players to your scene, you update their playerPrefix variable (“P2_” etc).

Alternatively, the unity-input third-party input manager sounds kinda like what you want. It boasts:

  • Different device configs for different controller types and operating systems
  • Unplug & Plug new controllers while playing, they get initialized automatically with the right configuration
  • Support for up to 4 gamepads with mixed configurations & multiple keyboard configurations
  • Always get input from the last active device (for singleplayer games)

I am creating a Input detector using the Strategy Pattern at the moment.
It detects the joystick input, and to swap between Joysticks you only have to change the class it uses for Input detection.

For example, in the script where you detect the movement you would have:

InputDetector myInput = new Joystick1ControlType();
for player one…
InputDetector myInput = new Joystick2ControlType();
for player two.

However, regardless of which player/joystick they are, their script for jump, would always be:

//Do jump

I might have it finished tonight…