Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How can I effectively assign input devices to objects (New Input System)

Discussion in 'Input System' started by zgoniea, Apr 12, 2023.

  1. zgoniea

    zgoniea

    Joined:
    Sep 10, 2022
    Posts:
    1
    Hello all!
    I am currently attempting to make a local-multiplayer platform fighter similar to smash bros. I'm new to this somewhat, so bear with me!

    At the moment, I am doing what I’m sure most people do when they initially look into implementing local multiplayer using Unity's new input system. Upon pressing a button on a new input device, a new player prefab is instantiated in the scene with that new PlayerInput. You can see below how I've implemented some of this.

    Code (CSharp):
    1.  [Header("Other")]
    2.     private InputAction _move;
    3.     private InputAction _jump;
    4.     public SlapFighter playerMovement;
    5.     private InputActionAsset inputActions;
    6.     private InputActionMap player;
    7.     public Transform playerTransform;
    8.     Rigidbody rbody;
    9.  
    10.  
    11.     private void Awake()
    12.     {
    13.         inputActions = GetComponent<PlayerInput>().actions;
    14.         player = inputActions.FindActionMap("Player");    
    15.     }
    16.     private void OnEnable()
    17.     {
    18.         _jump = player.FindAction("Jump");
    19.         _move = player.FindAction("Move");
    20.         player.Enable();
    21.     }

    My issue with the system is that spawning in a new player prefab upon pressing a new button is extremely clunky and looks terrible in the game. Ultimately, I would hope that there could be a character selection screen where players can select their character and then both are automatically instantiated at the start of the next scene. But right now, I simply want to have two players already instantiated in the scene, and for them to each be assigned a player input.

    How can I assign a player input of my choosing?
    I have looked through dozens of threads, and viewed a similar number of videos and I haven't seemed to find one that was able to help me in the way that I need to be. To put it another way, right now, if I connect a controller, my keyboard is assigned to the first player, and my controller ends up creating a new player object in the scene to take that new input. My desired behavior is for me to potentially be able to connect two controllers and assign both of those inputs to either player object in the scene.
    This could be through either some sort of selection screen, or even just manually in a script on scene load assigning the two controllers.

    I should mention, my implementation of taking in player inputs involves utilizing different scripts to call actions. They refer back to the player movement script.


    upload_2023-4-12_18-55-55.jpeg


    Thanks in advance!
     
  2. docgonzzo

    docgonzzo

    Joined:
    Dec 10, 2013
    Posts:
    19
    I am searching for an answer to this one also. I don't want to use the PlayerInputManager to auto-instantiate my players. I want full control over this to manually do my associations at runtime.

    Anyone have any hints?
     
  3. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    103
    Code (CSharp):
    1. // With PlayerInput.
    2. playerInput.SwitchCurrentControlScheme(gamepadToUse);
    3.  
    4. // Without PlayerInput (InputActionAsset or InputActionMap).
    5. actions.devices = new[] { gamepadToUse };