I am making tutorial prompts for our game that will show textures of the input buttons that you need to press in order to execute certain moves. But, since players can use different joysticks and/or controllers, I need a way to check what joystick has been used last. (xbox controller/ps3 controller/keyboard/mouse) etc…
I know I can use Input.GetJoystickNames to see what joysticks are connected, but I am wondering how I can use this to figure out excactly which one of them are being used at any given moment.
I’ve been having the same problem as yourself today, I’ve written the below code to solve the problem, its pretty inefficiency but until there is a better way …
So if you want to tell which joystick is being used you could manipulate this code to detect the other joystick input and add more states …
using UnityEngine;
using System.Collections;
public class InputControl : MonoBehaviour
{
//*********************//
// Public member data //
//*********************//
//*********************//
// Private member data //
//*********************//
public enum eInputState
{
MouseKeyboard,
Controler
};
private eInputState m_State = eInputState.MouseKeyboard;
//*************************//
// Unity member methods //
//*************************//
void OnGUI()
{
switch( m_State )
{
case eInputState.MouseKeyboard:
if(isControlerInput())
{
m_State = eInputState.Controler;
Debug.Log("DREAM - JoyStick being used");
}
break;
case eInputState.Controler:
if (isMouseKeyboard())
{
m_State = eInputState.MouseKeyboard;
Debug.Log("DREAM - Mouse & Keyboard being used");
}
break;
}
}
//***************************//
// Public member methods //
//***************************//
public eInputState GetInputState()
{
return m_State;
}
//****************************//
// Private member methods //
//****************************//
private bool isMouseKeyboard()
{
// mouse & keyboard buttons
if (Event.current.isKey ||
Event.current.isMouse)
{
return true;
}
// mouse movement
if( Input.GetAxis("Mouse X") != 0.0f ||
Input.GetAxis("Mouse Y") != 0.0f )
{
return true;
}
return false;
}
private bool isControlerInput()
{
// joystick buttons
if(Input.GetKey(KeyCode.Joystick1Button0) ||
Input.GetKey(KeyCode.Joystick1Button1) ||
Input.GetKey(KeyCode.Joystick1Button2) ||
Input.GetKey(KeyCode.Joystick1Button3) ||
Input.GetKey(KeyCode.Joystick1Button4) ||
Input.GetKey(KeyCode.Joystick1Button5) ||
Input.GetKey(KeyCode.Joystick1Button6) ||
Input.GetKey(KeyCode.Joystick1Button7) ||
Input.GetKey(KeyCode.Joystick1Button8) ||
Input.GetKey(KeyCode.Joystick1Button9) ||
Input.GetKey(KeyCode.Joystick1Button10) ||
Input.GetKey(KeyCode.Joystick1Button11) ||
Input.GetKey(KeyCode.Joystick1Button12) ||
Input.GetKey(KeyCode.Joystick1Button13) ||
Input.GetKey(KeyCode.Joystick1Button14) ||
Input.GetKey(KeyCode.Joystick1Button15) ||
Input.GetKey(KeyCode.Joystick1Button16) ||
Input.GetKey(KeyCode.Joystick1Button17) ||
Input.GetKey(KeyCode.Joystick1Button18) ||
Input.GetKey(KeyCode.Joystick1Button19) )
{
return true;
}
// joystick axis
if(Input.GetAxis("XC Left Stick X") != 0.0f ||
Input.GetAxis("XC Left Stick Y") != 0.0f ||
Input.GetAxis("XC Triggers") != 0.0f ||
Input.GetAxis("XC Right Stick X") != 0.0f ||
Input.GetAxis("XC Right Stick Y") != 0.0f )
{
return true;
}
return false;
}
}
I expanded off hursty90’s here and basically made it a bit more efficient.
It’s in update so it should only happen once a frame as opposed to potentially more than once with OnGUI (which is supposedly slow to begin with). I mainly swapped out the Event.current calls as they only work in OnGUI. I also checked how the mouse buttons are checked (because there isn’t an Input.anyMouseButton function) and added the mouse scroll wheel as one of the checks.
EDIT/IMPORTANT: I just realized there’s a slight bug in this by calling Input.anyKey as it will catch controller events. You can bypass this by checking for whatever keys you want to check for on the keyboard being pressed instead of Input.anyKey
public class InputChecker : MonoBehaviour
{
private static InputChecker _instance;
public static InputChecker Instance { get { return _instance; } }
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(this.gameObject);
} else {
_instance = this;
}
}
public enum InputType
{
MouseKeyboard,
Controller
};
private InputType InputState = InputType.MouseKeyboard;
void Update()
{
switch(InputState)
{
case InputType.MouseKeyboard:
if(isControllerInput())
{
InputState = InputType.Controller;
Debug.Log("Switched Input to Controller");
}
break;
case InputType.Controller:
if (isMouseKeyboard())
{
InputState = InputType.MouseKeyboard;
Debug.Log("Switched Input to Mouse/Keyboard");
}
break;
}
}
public InputType GetInputType()
{
return InputState;
}
public bool UsingController(){
return InputState == InputType.Controller;
}
private bool isMouseKeyboard()
{
// mouse & keyboard buttons and mouse movement
if (Input.anyKey ||
Input.GetMouseButton(0) ||
Input.GetMouseButton(1) ||
Input.GetMouseButton(2) ||
Input.GetAxis("Mouse ScrollWheel") != 0.0f)
{
return true;
}
return false;
}
private bool isControllerInput()
{
// joystick buttons
// check if we're not using a key for the axis' at the end
if(Input.GetKey(KeyCode.Joystick1Button0) ||
Input.GetKey(KeyCode.Joystick1Button1) ||
Input.GetKey(KeyCode.Joystick1Button2) ||
Input.GetKey(KeyCode.Joystick1Button3) ||
Input.GetKey(KeyCode.Joystick1Button4) ||
Input.GetKey(KeyCode.Joystick1Button5) ||
Input.GetKey(KeyCode.Joystick1Button6) ||
Input.GetKey(KeyCode.Joystick1Button7) ||
Input.GetKey(KeyCode.Joystick1Button8) ||
Input.GetKey(KeyCode.Joystick1Button9) ||
Input.GetKey(KeyCode.Joystick1Button10) ||
Input.GetKey(KeyCode.Joystick1Button11) ||
Input.GetKey(KeyCode.Joystick1Button12) ||
Input.GetKey(KeyCode.Joystick1Button13) ||
Input.GetKey(KeyCode.Joystick1Button14) ||
Input.GetKey(KeyCode.Joystick1Button15) ||
Input.GetKey(KeyCode.Joystick1Button16) ||
Input.GetKey(KeyCode.Joystick1Button17) ||
Input.GetKey(KeyCode.Joystick1Button18) ||
Input.GetKey(KeyCode.Joystick1Button19) ||
Input.GetAxis("Horizontal") != 0.0f ||
Input.GetAxis("Vertical") != 0.0f)
{
return true;
}
return false;
}