How can i tell which joystick input came from?

Old question, still not answered sufficiently.

I have a gaming station that has a steering wheel controller, flight stick (hotas), light gun, and xbox 360 controller connected to it. I have written games for each of those, and a menu to select which game to play. But I need to ensure the input for the right controller is used with the right game, and no other input is accepted. For example, you can’t use the steering wheel or light gun to fly the helicopter, and you can’t use the hotas or light gun to steer the car.

I was going to make a separate input for each controller, but the problem there is, I don’t know which controller will be which number each time. Basically, I can’t set up an Axis called “Steering Wheel Accelerator” = Joystick3.Axis1, because the next time I play the game, the Steering Wheel could be Joystick0 or Joystick1.

So, simple question, how can I tell which controller the input came from? Or, how can I ignore input from all but one controller? Or, how can I be sure that a specific controller is always JoystickN?

(Hint: this would all be so simple if, instead of Joystick# we could feed GetAxis(JoystickName). Seems like a simple fix.)

As always in software engineering the answer is a layer of indirection…

This code should work for buttons, it’s more or less what I do in my library code to fix the issue you’re experiencing (I flung this code together for this answer, so apologies if it doesn’t work - the idea does :wink: )

	public class JoystickButton
	{
		public string		_strJoystickName;
		public int			_iButtonNumber;			// 0 indexed
		public int			_iJoystickNumber;		// 1 indexed
		public string		_strButtonIdentifier;	

		public void InitialiseButtonIdentifier( string strJoystickName, int iButtonNumber )
		{
			_strJoystickName	= strJoystickName;
			_iButtonNumber		= iButtonNumber;

			string[] astrJoysticks = Input.GetJoystickNames();

			// find the named joystick in the array
			// array position corresponds to joystick number
			for( int i = 0; i < astrJoysticks.Length; ++ i )
			{
				if( _strJoystickName == astrJoysticks[ i ] )
				{
					_iJoystickNumber = ( i + 1 );
					_strButtonIdentifier = string.Format(	"joystick{0}button{1}", 
															_iJoystickNumber, 
															_iButtonNumber );
				}
			}
		}

		public bool ButtonIsPressed()
		{
			// N.B. you could use Input.KeyDown( KeyCode ) if you parse the string 
			// into a value of the KeyCode enum http://stackoverflow.com/a/16104
			return Input.GetButtonDown( _strButtonIdentifier );
		}
	}

You can extend it to work with axes too, but you need to manually add one to the Input manager for every axis you want to use on every joystick and use a naming convention for them so you can use string.Format() to generate the string for the axis programmatically.

So, the only thing to say now is that you’d have to re-generate any instances of JoystickButton you have
if the array returned by Input.GetJoystickNames() changes.

Hope that helps!