Search Unity

Local Multiplayer

Discussion in 'New Input System' started by Mat13109, May 21, 2019.

  1. Mat13109

    Mat13109

    Joined:
    Apr 16, 2019
    Posts:
    3
    Hi,

    I'm trying to do a simple scene where I can control 4 players.

    I need help setting up this because every bit of explanation is out of date today.

    This script below is attached to a simple cube prefab.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private InputMaster controls;
    8.  
    9.     [SerializeField] private float speed = 10;
    10.  
    11.     private void Awake()
    12.     {
    13.         controls = new InputMaster();
    14.  
    15.         controls.Player.Shoot.performed += _ => Shoot();
    16.         controls.Player.Move.performed += ctx => Move(ctx.ReadValue<Vector2>());
    17.     }
    18.     private void OnEnable()
    19.     {
    20.         controls.Enable();
    21.      
    22.     }
    23.     private void OnDisable()
    24.     {
    25.         controls.Disable();
    26.     }
    27.     private void Shoot()
    28.     {
    29.         Debug.Log("Shoot");
    30.     }
    31.     private void Move(Vector2 direction)
    32.     {
    33.         transform.position += new Vector3(
    34.                                 direction.x * speed * Time.deltaTime,
    35.                                 0,
    36.                                 direction.y * speed * Time.deltaTime);
    37.     }
    38. }
    39.  
    I have a Player Input Manager in my scene with the Player prefab (the cube) attached to it.
    I have set up my Input Actions to take inputs from the keyboard and from game pads.

    Okay, so now I have one keyboard and two controllers. When I touch the keyboard, one player (cube) appears and respond to the controls. Now if I touch a controller, another player appears, but every player is controlled by every controller & keyboard.

    Can someone try and help me please ? I'm completely lost in this new Input System, but I want it to work so bad :)
     
    Last edited: May 21, 2019
  2. Kaiz0ku

    Kaiz0ku

    Joined:
    Oct 28, 2014
    Posts:
    6
    I am on the same boat with you my friend :(
     
  3. dougpunity3d

    dougpunity3d

    Unity Technologies

    Joined:
    Jul 11, 2018
    Posts:
    16
    yes there is a bit of confusion around this. What is happening in your script is the PlayerInput is being created multiple times and that is why each controller controls each cube. The easiest way to do this. Create your cube prefab. Add a Player Input component on the prefab. Add a script for movement. Now in the script the only input code you want to add is something like

    private void OnMove(InputValue value)
    {
    m_MovementInputValue = value.Get<Vector2>();
    }

    then you can write the rest to move the cube. Your not going to write any of this below code get rid of all this below

    1. private void Awake()
    2. {
    3. controls = new InputMaster();

    4. controls.Player.Shoot.performed += _ => Shoot();
    5. controls.Player.Move.performed += ctx => Move(ctx.ReadValue<Vector2>());
    6. }
    7. private void OnEnable()
    8. {
    9. controls.Enable();
    10. }
    11. private void OnDisable()
    12. {
    13. controls.Disable();
    14. }
    This will all be taken care of under the hood. Now in your scene create an empty game object and add a Player Input manager component. Set the Player prefab to your cube. Now it should just work. hit a button on gamepad 1 and a cube will spawn etc. only one input device will be mapped to each spawn of the cube.

    If you can't get it working I will attach a small sample project.
     
  4. Mat13109

    Mat13109

    Joined:
    Apr 16, 2019
    Posts:
    3
    Yes, thank you so much for your reply, but now I have this error :

    error CS0246: The type or namespace name 'InputValue' could not be found (are you missing a using directive or an assembly reference?)

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     private Vector2 m_MovementInputValue;
    4.  
    5.     private void OnMove(InputValue value)
    6.     {
    7.         m_MovementInputValue = value.Get<Vector2>();
    8.         Debug.Log(m_MovementInputValue);
    9.     }
    10. }
    11.  
    Is that a problem with Visual or something ? Where does the InputValue type come from ?
     
  5. dougpunity3d

    dougpunity3d

    Unity Technologies

    Joined:
    Jul 11, 2018
    Posts:
    16
    Add this to your script to pick up the InputValue using UnityEngine.InputSystem.Plugins.PlayerInput;
    (I am assuming your using the latest package 0.2.10-preview)
     
  6. Mat13109

    Mat13109

    Joined:
    Apr 16, 2019
    Posts:
    3
    Okay, sorry about this one...
    Thank you again your your quick answers, that's cool :cool:

    Now I have no build errors and my players are in fact appearing when button is pressed on keyboard or gamepad.

    How can I call my OnMove method ? In an Update ? And what parameter does it take ? I have created an Input Actions manager.

    You said you have a small sample project ? That is gold at this point in time ! May I have it ?

    Oh, and thank you again
     
  7. Kaiz0ku

    Kaiz0ku

    Joined:
    Oct 28, 2014
    Posts:
    6
    The same you did with your Move() function in your first post. You add it as a listener to an event.
    Example project would also be pretty helpful :D
     
  8. dougpunity3d

    dougpunity3d

    Unity Technologies

    Joined:
    Jul 11, 2018
    Posts:
    16
    Take a look at this project. Very little script code. 1. Create your prefab. 2. Add a Player Input component to the prefab. 3. Looking at your actions asset react to the event in your script, ie if you have a Move in your action map, have an OnMove. 4. Globally add a Player Input Manager to a game object. 5. In the Player Input Manager have the prefab as the Player Input. Let me know if you have any questions on this project.
     

    Attached Files:

    • Cube.zip
      File size:
      308.7 KB
      Views:
      89
    GilbertoBitt likes this.
  9. Mikael-H

    Mikael-H

    Joined:
    Apr 26, 2013
    Posts:
    278
    @dougpunity3d so you're not supposed to use the file with the generated code if you want to do local multiplayer? I tried that all weekend but could not get it to work :) Any plans when that will be possible?
     
  10. crazy_asian

    crazy_asian

    Joined:
    Jul 14, 2019
    Posts:
    4
    I know this is kind of late, but I created a quick YouTube tutorial here after looking at this thread:


    I hope someone finds this helpful!
     
    kpgbrink, WafflesX, Jichaels and 2 others like this.