Search Unity

Question Choosing Scheme and Map On Player Start

Discussion in 'Input System' started by hinklejoe, Jan 23, 2023.

  1. hinklejoe

    hinklejoe

    Joined:
    Apr 21, 2021
    Posts:
    21
    I have a working Action Map and I can go into the UI and manually change the default Action Map and Default Scheme and everything works fine. However when I try to modify the controller choice from c# I can't seem to get it to work right. Even if I change the default values and verify it in the editor it still won't run right.

    I've tried a few different methods I found online and am including my code below. I'm also including some commented out items for reference. This code appears to update the correct action map, but I can't get the scheme to update correctly. Can anyone assist in showing me how this is supposed to work?

    For context I've been learning Unity and am trying to finalize my controller setup for Keybard & Controller before I try to implement local 2 player co-op. I ran into issues with using twin-stick with settings for both keyboard and controller so I split the maps into separate Controller (CON) and Keyboard and Mouse (KAM) settings.

    Code (CSharp):
    1.    
    2.     private PlayerInput myPlayerInput;                    
    3.     private UserInputController refUserInputController;    
    4.    
    5. void Start()
    6.     {
    7.         // Setup the Controllers
    8.         refUserInputController = new UserInputController();
    9.         myPlayerInput = gameObject.GetComponent<PlayerInput>();
    10.         SetupControllerOne();
    11.     }  
    12.  
    13. public void SetupControllerOne()
    14.     {
    15.         string myConChoice = PlayerPrefs.GetString("playerOneCon");
    16.  
    17.         if (myConChoice == "CON")
    18.         {
    19.             Debug.Log("Found Controller: " + myConChoice);
    20.  
    21.             refUserInputController.AttackShipCON.Enable();
    22.  
    23.             // myPlayerInput.actions.FindActionMap("AttackShipCON").Enable();
    24.             myPlayerInput.SwitchCurrentActionMap("AttackShipCON");
    25.             // myPlayerInput.SwitchCurrentControlScheme("Controller");
    26.  
    27.             myPlayerInput.defaultActionMap = "AttackShipCON";
    28.             myPlayerInput.defaultControlScheme = "Controller";
    29.  
    30.             // refUserInputController.bindingMask = new InputBinding { groups = "Controller" };
    31.  
    32.  
    33.         }
    34.         else if (myConChoice == "KAM")
    35.         {
    36.  
    37.         }
    38. }
    39.  
     
  2. hinklejoe

    hinklejoe

    Joined:
    Apr 21, 2021
    Posts:
    21
    After further testing I found that going into the UI and into Player Input on my player I can check a box for Auto-Switch. With that box checked I don't have to call the scheme, I just need to use the code to change the Action Map.

    For now its working but if anyone knows how to do this from c# I'd like to know.