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

Resolved When a 2nd player joins, they are only able to move the character horizontally

Discussion in 'Input System' started by mtrest, Oct 25, 2021.

  1. mtrest

    mtrest

    Joined:
    Mar 27, 2020
    Posts:
    10
    I’m using Unity’s new input system (input package) and would like to be able to have it where two controllers can control the same character/player. Is this possible? If so, how would I approach doing this? This may seem like an odd question but I would like to have this functionality for a game I'm working on.

    Thanks in advance,

    Max

    EDIT: I want one player to control horizontal movement while the other player controls vertical movement of the player.
     
    Last edited: Oct 26, 2021
  2. DiegoSynth

    DiegoSynth

    Joined:
    May 12, 2019
    Posts:
    8
    Hi Max,

    I'm actually looking for the opposite, as I'm able to control one player with many controllers (using C# Events with the New Input System) :)

    The steps to get what you want:
    - add an "InputActions" object and map the controllers
    - add an "empty" object and add the component PlayerInputManager to it.
    - make your player object as a Prefab
    - add a "PlayerInput" component to your player prefab
    - to the recently added "PlayerInput" set the InputActions and as behaviour set C# Events
    - create a Script called "PlayerInput", add it to the PlayerInput object as a component
    - in PlayerInput script, add a code like this such as:
    Code (CSharp):
    1. private void Awake()
    2. {
    3.   PlayerInputActions playerInputActions = new PlayerInputActions();
    4.   playerInputActions.Player.jump.performed += OnJump;
    5.   // repeat for other actions (move, climb, etc)
    6. }
    7.  
    8. private void Jump_performed(InputAction.CallbackContext obj)
    9. {
    10.   // make it jump
    11. }
    12.  
    I hope it helps, otherwise let me know! (Keep in mind that I don't know if this is a bug or a feature :D)
    Diego
     
  3. mtrest

    mtrest

    Joined:
    Mar 27, 2020
    Posts:
    10

    Hi Diego,

    Thank you so much for your response! This is very helpful. I’m already using prefabs, player input, etc., so this should be easy to add in. I'll tell you if it works once I have time to check.

    Thanks again,

    Max
     
    DiegoSynth likes this.
  4. mtrest

    mtrest

    Joined:
    Mar 27, 2020
    Posts:
    10
    Hi Diego,

    I just realized that what your suggesting is what I already have! I don't think my question was very clear, I wanted to say that I want to have one player control horizontal movement while the other player controls vertical movement.

    Thanks,

    Max
     
  5. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    558
    Easiest approach would probably be to use UnityEngine.Input and use the Gamepad class to just detect the first two controllers connected. At least for testing purposes, this is what I would start with.
     
  6. mtrest

    mtrest

    Joined:
    Mar 27, 2020
    Posts:
    10
    Any ideas for a permanent solution? I was thinking of using the player input manager, would that help me solve the problem?
     
  7. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    558
    You essentially just want to be able to assign controllers to 2 separate Input Assets. The same concept for a local co-op game. You'd want your player script to be able to reference these Input Assets. Then you could assign their Inputs to different values in script. Something like

    PlayerInputManager - Detects controller input to spawn additional "Players." You can use their OnJoin and Leave properties however you choose.
    PlayerInputManager/Player Prefab - This won't actually be a player prefab at all, but an empty gameobject with a PlayerInput component.
    Controller Script - The script that will reference the newly spawned objects and take their input.

    I can't exactly write out the whole system, but something close to this is what I use for my own multiplayer solutions (With the exception of that I don't use PlayerInput and PlayerInputManager).
     
    mtrest likes this.
  8. mtrest

    mtrest

    Joined:
    Mar 27, 2020
    Posts:
    10
    Thanks for the reply! I just wanted to mention that the game is local co-op.
     
  9. mtrest

    mtrest

    Joined:
    Mar 27, 2020
    Posts:
    10
    Hello everyone! I just wanted to mention that Rin-Dev’s solution worked. Thanks to everyone for trying to get this to work!

    Cheers,

    Max
     
    Rin-Dev likes this.