How to map multiple different types of controllers at the same time

Hi everyone.


I want to make it so I have support for both: my keyboard, my xbox controller, my dualshock 4 and any other controller I might want to ad in the future.

I tried to just add the button mappings to Unity’s input list but I found out that it read some dualshock inputs as inputs from the xbox controller.

That won’t do becouse it will mess up my controls when I try to use ether one.


I want it to reconise every controller automatically when I press a button and don’t want to have to switch it in the menu.

I know there are plugins for this, but I want to do my project without them.


So does anyone know how I can map this in Unity?


PS: I already have some code that I wanted to use as an input combiner but I probably have to rewrite everything once I have the solution, also it’s not finished because I stopped writing when I realised that dualshock 4 used some of the xbox controllers inputs.


Here is my code:

using UnityEngine;
using System.Collections;

public class ControlMapper : MonoBehaviour {

    //*Camera inputs*
    private float camXinput = 0.0f;
    private float camYinput = 0.0f;
    //Keyboard and mouse
    private float mouseXinput = 0.0f;
    private float mouseYinput = 0.0f;
    //Dualshock 4
    private float DS4JoyRXinput = 0.0f;
    private float DS4JoyRYinput = 0.0f;
    //Windows/XBOX controller
    private float XBOXYoyRXinput = 0.0f;
    private float XBOXYoyRYinput = 0.0f;

    //*Move inputs*
    private float moveXinput = 0;
    private float moveYinput = 0;
    //Keyboard and mouse
    private float ADKeyXinput = 0;
    private float WSKeyYinput = 0;
    //Dualshock 4
    private float DS4JoyLXinput = 0;
    private float DS4JoyLYinput = 0;
    //Windows/XBOX controller
    private float XBOXYoyLXinput = 0.0f;
    private float XBOXYoyLYinput = 0.0f;

    //*Jump inputs*
    private bool jumpInputPress = false;
    private bool jumpInputHold = false;
    private bool jumpInputRelease = false;
    //Keyboard and mouse
    private bool spaceInputPress = false;
    private bool spaceInputHold = false;
    private bool spaceInputRelease = false;
    //Dualshock 4
    private bool DS4CrossInputPress = false;
    private bool DS4CrossInputHold = false;
    private bool DS4CrossInputRelease = false;
    //Windows/XBOX controller
    private bool XBOXAInputPress = false;
    private bool XBOXAInputHold = false;
    private bool XBOXAInputRelease = false;


    void Update ()
    {
        //*Find camera inputs*
        //Find mouse inputs
        mouseXinput = Input.GetAxis("Mouse X");
        mouseYinput = Input.GetAxis("Mouse Y");
        //Find DS4JoyR inputs
        DS4JoyRXinput = Input.GetAxis("DS4JoyRX");
        DS4JoyRYinput = Input.GetAxis("DS4JoyRY");
        //Find XBOXJoyR inputs

        //*Find move inputs*
        //Find mouse inputs
        ADKeyXinput = Input.GetAxis("ADKey's X");
        WSKeyYinput = Input.GetAxis("WSKey's Y");
        //Find DS4JoyL inputs
        DS4JoyLXinput = Input.GetAxis("DS4JoyLX");
        DS4JoyLYinput = Input.GetAxis("DS4JoyLY");

    }
}

Hah, I’ll give an attempt at an explanation to what I’ve done in the past but I don’t know if I’ve got code to provide. So, here’s what I did: whenever you plug in a controller, getjoysticknames() is updated and shows a new joystick name in the array. After gathering the names that Unity uses for the different controllers (by looking at the getjoysticknames array), I made scriptable objects that have stored mappings for different controllers. So, one of the scriptableobjects might have a name variable being “joycon” with the proper mappings for the cute little controller. After having the set of scriptableobjects, one for each controller type, I then dumped those scriptableobjects into a custom script of mine and asked it to generate a bunch of inputs for every one of those controllers. So, THE SCRIPT would add to Unity’s input manager list. One of them might be like 1 joycon A_Button (something more complex than that, but that’s the idea). So, I used the script to populate Unity’s input manager with all of these different controller types. I then made a controller manager script that would detect which controller is plugged in using the getjoysticknames array, and then look up the correct mappings in the input manager to reference for in-game controls.


Sorry, about the best I can give is the explanation. For the controller manager script, I slightly referenced this YouTube video. Maybe this will give you some ideas or something.

Ok someone gave me a good perspective of controller mapping, but can someone tell me if his advice works with local multiplayer and swamping controllers real time?


@Stratosome

Does it work with local multiplayer and realtime controller swamping?

Sorry, I am still learning.

And the better I can piece these things together the better I can get a clear head.