Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

[SOLVED] Need help with local multiplayer

Discussion in 'Input System' started by Nylash, Nov 21, 2019.

  1. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    Hi, I am currently making a game, a Tetris-like in 3D, and I try the new Input System on it.
    In my game each player play one after an another.

    I want to know if there is a way to listen inputs only from one specific device, this way I could associate a player with an device and mute all others when it his turn. Or just listen to a specific device.

    I use event callback for every Press action, and Action.ReadValue<T>() for axis input in Update.

    I think PlayerInputManager is not usefull here since I don't have several object to control.
     
    Last edited: Nov 21, 2019
  2. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    I dont believe there is a way to mute input for devices, but you can stop your scripts from listening for input.

    One way to do this would be for each player to have a simple state machine. You really only need 2 states: the MyTurn state and the NotMyTurn state. The MyTurn state subscribes or listens for inputs, and feeds those inputs into whatever game mechanic you need. Then, when finished, the TurnState unsubscribes from inputs and then transitions into the NotMyTurn state. The NotMyTurn state is basically empty, listens for no inputs, and does nothing.
     
  3. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    I change the way my scripts work.
    I now have a prefab with a PlayerInput and a the following script :
    Code (CSharp):
    1. public class PlayerInputs : MonoBehaviour
    2. {
    3.     Controls controls;
    4.  
    5.     public Vector2 movementDirection;
    6.     public Vector2 cameraMovementPad;
    7.     public Vector2 cameraMovementMouse;
    8.     public float up;
    9.     public float down;
    10.     public bool cameraCanMove;
    11.  
    12.     private void Awake()
    13.     {
    14.         controls = new Controls();
    15.  
    16.         controls.Gameplay.Move.performed += ctx => movementDirection = ctx.ReadValue<Vector2>();
    17.         controls.Gameplay.Move.canceled += ctx => movementDirection = Vector2.zero;
    18.  
    19.         controls.Gameplay.CameraMovementPad.performed += ctx => cameraMovementPad = ctx.ReadValue<Vector2>();
    20.         controls.Gameplay.CameraMovementPad.canceled += ctx => cameraMovementPad = Vector2.zero;
    21.  
    22.         controls.Gameplay.CameraMovementMouse.performed += ctx => cameraMovementMouse = ctx.ReadValue<Vector2>();
    23.         controls.Gameplay.CameraMovementMouse.canceled += ctx => cameraMovementMouse = Vector2.zero;
    24.  
    25.         controls.Gameplay.Up.performed += ctx => up = ctx.ReadValue<float>();
    26.         controls.Gameplay.Up.canceled += ctx => up = 0;
    27.  
    28.         controls.Gameplay.Down.performed += ctx => down = ctx.ReadValue<float>();
    29.         controls.Gameplay.Down.canceled += ctx => down = 0;
    30.  
    31.         controls.Gameplay.CameraCanMove.started += ctx => cameraCanMove = true;
    32.         controls.Gameplay.CameraCanMove.canceled += ctx => cameraCanMove = false;
    33.  
    34.         controls.Gameplay.RotX.started += ctx => RotX();
    35.         controls.Gameplay.RotY.started += ctx => RotY();
    36.         controls.Gameplay.RotZ.started += ctx => RotZ();
    37.         controls.Gameplay.Drop.started += ctx => Drop();
    38.         controls.Gameplay.Restart.started += ctx => Restart();
    39.         controls.Gameplay.SwitchMovementSystem.started += ctx => SwitchMovementSystem();
    40.     }
    41.  
    42.     private void OnEnable()
    43.     {
    44.         controls.Gameplay.Enable();
    45.     }
    46.     private void OnDisable()
    47.     {
    48.         controls.Gameplay.Disable();
    49.     }
    50.  
    51.     private void Update()
    52.     {
    53.         GameManager.instance.movementDirection = movementDirection;
    54.         GameManager.instance.cameraMovementPad = cameraMovementPad;
    55.         GameManager.instance.cameraMovementMouse = cameraMovementMouse;
    56.         GameManager.instance.up = up;
    57.         GameManager.instance.down = down;
    58.         GameManager.instance.cameraCanMove = cameraCanMove;
    59.     }
    60.  
    61.     void RotX()
    62.     {
    63.         GameManager.instance.movingScript.RotX();
    64.     }
    65.  
    66.     void RotY()
    67.     {
    68.         GameManager.instance.movingScript.RotY();
    69.     }
    70.  
    71.     void RotZ()
    72.     {
    73.         GameManager.instance.movingScript.RotZ();
    74.     }
    75.  
    76.     void Drop()
    77.     {
    78.         GameManager.instance.movingScript.Drop();
    79.     }
    80.  
    81.     void Restart()
    82.     {
    83.         GameManager.instance.Restart();
    84.     }
    85.  
    86.     void SwitchMovementSystem()
    87.     {
    88.         GameManager.instance.movingScript.SwitchMovementSystem();
    89.     }
    90. }
    But when I try to use PlayerInputManager to spawn this prefab the devices are not paired with the prefab it just spawns. When I press a button on one device it does the call on both of the prefabs.. I must miss something, I think it's the fact that I use directly my map (Controls.Gameplay) but I am not sure since this is not the same instance of the map on the two PlayerInput.
     
  4. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    Okay, looks like this method doesn't work for multiplayer, so I change and only use messages send by the PlayerInput component and it works.