Search Unity

Local Multiplayer

Discussion in '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 :(
     
    ColderMoon likes this.
  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:
      658
    GilbertoBitt likes this.
  9. Mikael-H

    Mikael-H

    Joined:
    Apr 26, 2013
    Posts:
    309
    @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!
     
    huhuo_o, SOLOLVLGD, Astucia and 10 others like this.
  11. gregmax17

    gregmax17

    Joined:
    Jan 23, 2011
    Posts:
    186
    How do you get 2 Gamepads to share the same GUI? Think Super Smash Bros character selection.
     
  12. TheEthanHub1

    TheEthanHub1

    Joined:
    Nov 5, 2019
    Posts:
    2
    How would I change the color of the other player?
     
  13. spunkyschild

    spunkyschild

    Joined:
    Oct 3, 2022
    Posts:
    2
    I've got the movement between the players split up but the jump is still connected.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private float moveInput;
    9.     public float speed;
    10.     public float jumpForce;
    11.  
    12.     private bool isGrounded;
    13.     public float checkRadius;
    14.     public LayerMask groundCheck;
    15.     public Transform playerPos;
    16.  
    17.     private Rigidbody2D rb;
    18.  
    19.     private bool facingRight = true;
    20.  
    21.     void Start()
    22.     {
    23.         rb = GetComponent<Rigidbody2D>();
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         Move();
    29.     }
    30.  
    31.     void Flip()
    32.     {
    33.         facingRight = !facingRight;
    34.         Vector3 Scaler = transform.localScale;
    35.         Scaler.x *= -1;
    36.         transform.localScale = Scaler;
    37.     }
    38.  
    39.     private void Move()
    40.     {
    41.         //moveInput = Input.GetAxis("Horizontal");
    42.  
    43.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    44.  
    45.         isGrounded = Physics2D.OverlapCircle(playerPos.position, checkRadius, groundCheck);
    46.  
    47.         if (isGrounded && Input.GetButtonDown("X") || isGrounded && Input.GetKeyDown(KeyCode.UpArrow))
    48.         {
    49.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    50.         }
    51.  
    52.         if (!facingRight && moveInput > 0)
    53.         {
    54.             Flip();
    55.         }
    56.         if (facingRight && moveInput < 0)
    57.         {
    58.             Flip();
    59.         }
    60.     }
    61.  
    62.     private void OnMove(InputValue value)
    63.     {
    64.         moveInput = value.Get<Vector2>().x;
    65.     }
    66. }
     
    RealvirtualGG likes this.