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

New Input System: How do I switch between controlling a menu and a player?

Discussion in 'Input System' started by Marscaleb, Apr 10, 2021.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    With the new input system, how do I switch between my input working with the menu to working with a player character in the game world?

    With the old system I had actually set up a input script that I could attach to any game object that wanted to read player input, and switching between a menu and gameplay was simply a matter of having the object's script ignore most of the input depending on if the menu was open. (For which I just checked the current time scale.)

    I thought it would be a similar plan with the new input system. I add my player input to my player game object, and I add my player input to my game manager that controls menus and stuff.
    I even made a separate action map for the player than for the UI within my input actions to help keep things organized.

    But when I have two player inputs in my scene, only one works.
    Now the new input system is designed to work with multiple inputs for multiple characters, so I'm guessing this is trying to lock out duplicates trying to use the same controller, so if I add a second character into my game I'm supposed to use a second controller for the second player. But I'm not making a multiplayer section; if the player is using a keyboard they are going to use that same keyboard for both the player and the menu.

    So how am I supposed to switch active control between these player inputs in my scene? How do I dictate if the player's input is trying to work the menu or if they are working the player character?
     
  2. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    575
    Bump.

    I have the same question. I’m doing it by disabling the character controller as well the cinemamachine brain components while the players responds to menus and then re-enabling them afterward. But I figure there must be a more straightforward way to do this.

    Any thoughts from anyone?
     
  3. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    575


    Found this YouTube video. Really very helpful.
     
  4. Simple answer is: have separate ActionMap for menu and for in-play input and disable one or another depending if you're in menu or in-play.
     
  5. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    575
    Thank you for the simple answer.
     
  6. donjuanjavier

    donjuanjavier

    Joined:
    Nov 7, 2020
    Posts:
    3
    This also worked for me! I had run into an issue where the PlayerInput no longer received input when showing a pause menu UI that used an EventSystem.

    Leaving a code sample in case it helps someone in the future.

    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2.  
    3. class PlayerInputHandler {
    4.     PlayerInput input;
    5.     bool isPaused;
    6.  
    7.     void Start() {
    8.         input = GetComponent<PlayerInput>();
    9.     }
    10.  
    11.     void OnPause() {
    12.         isPaused = !isPaused;
    13.         if (isPaused) {
    14.             input.SwitchCurrentActionMap("UI");
    15.         } else {
    16.             input.SwitchCurrentActionMap("Player");
    17.         }
    18.     }
    19. }
    20.  
    Relevant docs: https://docs.unity3d.com/Packages/c...erInput_SwitchCurrentActionMap_System_String_
     
    DW79 and vincurekf like this.
  7. MarkHelsinki

    MarkHelsinki

    Joined:
    Jul 14, 2021
    Posts:
    23
    If you are using GetComponent, you have to inherit from Monobehaviour.