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;
}
}
Just wanted to add that the check here has to be in the OnGUI function, if you change it to an Update() call you'll have issues finding the Event.current variable.
So far I created a PlayMode test script. Usually I believe this creates some new temporary script for testing, which I have been able to create objects within. But what I would like to do is load one of my prebuilt scenes. I tried doing this using LoadSceneMode.Single, and the scene did display (which I checked simply by waiting for 3 seconds) but no objects could then be found
private int Xbox_One_Controller = 0;
private int PS4_Controller = 0;
void Update()
{
string names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
print(names.Length);
if (names.Length == 19)
{
print(“PS4 CONTROLLER IS CONNECTED”);
PS4_Controller = 1;
Xbox_One_Controller = 0;
}
if (names.Length == 33)
{
print(“XBOX ONE CONTROLLER IS CONNECTED”);
//set a controller bool to true
PS4_Controller = 0;
Xbox_One_Controller = 1;
}
}
if(Xbox_One_Controller == 1)
{
//do something
}
else if(PS4_Controller == 1)
{
//do something
}
else
{
// there is no controllers
}
}
LoadSceneMode.Single will close all other scenes except the one being loaded with that code. Additive will keep existing scenes open and load the new scene on top. Then which ever scene on top (I beleive) will be the "Active" scene. Not sure if that's what you're looking for but, hope it helps :)
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;
}
Note http://answers.unity3d.com/questions/465202/trying-to-find-a-way-to-detect-if-a-mouse-is-plugg.html
– FattieDid you load the second scene asynchronously?
– RocketFriday