Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

[Q] Local multiplayer WITHOUT using PlayerInput

Discussion in 'Input System' started by Mars91, Jul 16, 2020.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    575
    EDIT 3:
    Reading here
    https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/UserManagement.html
    it looks like both controller and user management is similar to how Xbox One handle users.

    Base on my previous experience I must assign a controller/actionmap to an user.
    My game support single player or local multiplayer (max 2 user).

    What I can't understand is how to actualluy do that.


    EDIT 2:
    The simple multiplayer demo gave me more confusion.
    I don't really need multiple player but I want player1 to control (this is an example) the camera and player2 to control movement.


    EDIT 1:
    Doing some test I also found that any connected controller can control the game. How can I enable only controller one for user input?


    Hi,
    As the title says is possible to configure local multiplayer WITHOUT using PlayerInput?
    I can't find any documentation or example about what are the best practices in this case.

    How I'm currently handling input

    In Awake
    Code (CSharp):
    1.  
    2. if (controls == null)
    3.     controls = new GameControls();
    4.  
    5. controls.Gameplay.DoPopAction.performed += context =>
    6. {
    7. };
    8.  
    In Update
    Code (CSharp):
    1.  
    2. moveVect = controls.Gameplay.LeftStickOnly.ReadValue<Vector2>();
    3. lookVect = controls.Gameplay.RightStickOnly.ReadValue<Vector2>();
    4.  
     
    Last edited: Jul 16, 2020
  2. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    575
    I came out with the idea of having two object with PlayerInput assigned to it and having my scripts subscribe to one of these object in order to listen for button pressing.

    Now my game is never going to have more than two player while the other we are working on is a single player game.

    I'm now trying to instantiate the playerinput object but it looks like I'm doing something wrong when it comes to controlscheme and device used (even if I can print the device name).


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class SinglePlayerJoinTest : MonoBehaviour
    5. {
    6.     public GameObject playerPrefab;
    7.  
    8.     private GameControls controls;
    9.  
    10.  
    11.     private void Awake()
    12.     {
    13.         if (controls == null)
    14.             controls = new GameControls();
    15.  
    16.         controls.Gameplay.DoAction.performed += DoActionInternal;
    17.         controls.Gameplay.DoAction.Enable();
    18.     }
    19.  
    20.     private void DoActionInternal(InputAction.CallbackContext context)
    21.     {
    22.         InputDevice currentDevice = context.control.device;
    23.  
    24.         PlayerInput playerInput = playerPrefab.GetComponentInChildren<PlayerInput>();
    25.         InputActionAsset actions = playerInput.actions;
    26.         InputControlScheme controlScheme = actions.controlSchemes[0];
    27.         string controlSchemeName = controlScheme.name;
    28.  
    29.         Debug.Log("Device: " + currentDevice.name + " ControlScheme: " + controlSchemeName);
    30.    
    31.         PlayerInput.Instantiate(
    32.             playerPrefab,
    33.             playerIndex: 0,
    34.             splitScreenIndex: -1,
    35.             controlScheme: controlSchemeName,
    36.             pairWithDevice: currentDevice
    37.             );
    38.     }
    39. }
     
    Last edited: Jul 16, 2020