I’m trying to detect the device the player is using when playing the game. if it is (PS controller, X box Controller, or playing with mouse and keyboard(no controller is connected)…
void Start()
{
var gamepad = Gamepad.current;
if (gamepad == null)
return;
if (gamepad is DualShockGamepad)
{
print("Playstation gamepad");
}
else if (gamepad is XInputController)
{
print("Xbox gamepad");
}
}
I was able to detect the controllers, but how can I add a condition for the keyboard/mouse situation? please help.
>Solution :
trying below my code example
using UnityEngine;
using UnityEngine.InputSystem;
public class InputDetection : MonoBehaviour
{
void Start()
{
var gamepad = Gamepad.current;
if (gamepad != null)
{
if (gamepad is DualShockGamepad)
{
print("PlayStation gamepad");
}
else if (gamepad is XInputController)
{
print("Xbox gamepad");
}
}
else if (Keyboard.current != null || Mouse.current != null)
{
print("Mouse and keyboard");
}
}
}