- Home /
Multiple joystick input
Hi,
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.
Answer by idbrii · Sep 13, 2014 at 05:59 PM
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)
Answer by Milo_del_mal · Nov 12, 2014 at 06:51 AM
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... or InputDetector myInput = new Joystick2ControlType(); for player two.
However, regardless of which player/joystick they are, their script for jump, would always be:
if(myInput.A_isPressed()) //Do jump
I might have it finished tonight...
Have you finished it yet? $$anonymous$$ost likely answer is yes since this has been 4 years since you posted this.
I really wanted to see what it would look like in script