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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing Action Map makes next button press not trigger

Discussion in 'Input System' started by Hender, Nov 17, 2019.

  1. Hender

    Hender

    Joined:
    Dec 15, 2015
    Posts:
    9
    Hi there,

    As the title says, if you change between Action maps on the fly via code, the next button press just simply won't register. However, if you create an Action in the new ActionMap you are changing to on the same binding, it will trigger.

    Recreation:
    • Create 2 ActionMaps: ActionMap1, ActionMap2.
    • Create a button press Action for ActionMap1 and bind it on keyboard Q.
    • Create a button press Action for ActionMap2 and bind it on keyboard E.
    • Create a gameobject and add the PlayerInput script, set the Actions to the one you just created.
    • Set the default map to ActionMap1.
    • Set the behaviour to SendMessages.
    • Create a script which changes to ActionMap2 when pressing Q on ActionMap1, and changes to ActionMap1 when pressing E.

    After pressing Q, you have to press E twice, then press Q twice to make them trigger.

    I have uploaded a Test.rar which has a SampleScene in the Scenes folder. Start it, then start pressing Q and E to change between maps.
     

    Attached Files:

    • Test.rar
      File size:
      325.8 KB
      Views:
      362
    viatech and philippelabalette like this.
  2. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    Same issue for me here https://forum.unity.com/threads/how-to-reset-action-states.790229/
    Also opened an issue: 1205285

    In my opinion, this problem has a very high severity. The enabling/disabling of action maps is a basic functionality of the new input system.

    Sorry developers of input system but it is a shame that you don't give feedback about this problem. This post is already more than 1 month old.
     
  3. Le-Capitaine

    Le-Capitaine

    Joined:
    Jan 23, 2014
    Posts:
    33
    Confirmed. My way around it was multiple input action assets.
     
  4. viatech

    viatech

    Joined:
    Dec 6, 2017
    Posts:
    55
    I tried your sample and modified the code to add an actions.Enabled() method call as follows:

    Code (CSharp):
    1.  
    2. public void OnActionMap1Action()
    3.     {
    4.         Debug.Log($"OnActionMap1Action invoked. {_playerInput.currentActionMap.name} to MAP2");
    5.  
    6.         _playerInput.SwitchCurrentActionMap("ActionMap2");
    7.         _playerInput.actions.Enable();
    8.  
    9.     }
    10.  
    11.     public void OnActionMap2Action()
    12.     {
    13.         Debug.Log($"OnActionMap2Action invoked. {_playerInput.currentActionMap.name} TO MAP1");
    14.  
    15.         _playerInput.SwitchCurrentActionMap("ActionMap1");
    16.         _playerInput.actions.Enable();
    17.     }
    This seems to give the correct behaviour. Which, is better than I'm getting at the moment with some code that switches Action Maps and after the switch I get no input at all from the second map!
     
  5. viatech

    viatech

    Joined:
    Dec 6, 2017
    Posts:
    55
    Looking at this again it's perhaps not what you want to call
    Code (CSharp):
    1. _player.actions.Enable();
    as that will enable all action maps and you (we) want to swap action maps on some code trigger. So this modification does that. It also works for my action switching code as well!

    Code (CSharp):
    1.  
    2. public class InputManager : MonoBehaviour
    3. {
    4.     [SerializeField] private PlayerInput _playerInput;
    5.  
    6.     public void OnActionMap1Action()
    7.     {
    8.         Debug.Log($"OnActionMap1Action invoked. {_playerInput.currentActionMap.name} to MAP2");
    9.  
    10.         //_playerInput.SwitchCurrentActionMap("ActionMap2");
    11.         //_playerInput.actions.Enable();
    12.         _playerInput.currentActionMap.Disable();
    13.         _playerInput.SwitchCurrentActionMap("ActionMap2");
    14.         _playerInput.currentActionMap.Enable();
    15.  
    16.     }
    17.  
    18.     public void OnActionMap2Action()
    19.     {
    20.         Debug.Log($"OnActionMap2Action invoked. {_playerInput.currentActionMap.name} TO MAP1");
    21.  
    22.         //_playerInput.SwitchCurrentActionMap("ActionMap1");
    23.         //_playerInput.actions.Enable();
    24.         _playerInput.currentActionMap.Disable();
    25.         _playerInput.SwitchCurrentActionMap("ActionMap1");
    26.         _playerInput.currentActionMap.Enable();
    27.  
    28.     }
    29. }
    In may case the problem was that I had an action called "Jump" in both action maps and although they were bound to different gamepad buttons that seems to have banjaxed the input system. Smells like a bug to me - you should be able to have two input maps which have some action names in common.
     
    Last edited: Dec 23, 2019
  6. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    It works only if you enable only one action map at a time.
     
  7. viatech

    viatech

    Joined:
    Dec 6, 2017
    Posts:
    55
    What only works if you enable one action map at a time?
     
  8. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    My answer was way too short and not very understandable, sorry for that. I mean that your workaround can work if you have only one Action map active at a time. (SwitchCurrentActionMap("<newMap>").

    If you use actionMap1.enable/actionMap2.disable, you still have the unwanted behavior: 2 times button/key pressing is needed to trigger.
     
  9. Koen-Matthijs

    Koen-Matthijs

    Joined:
    Aug 1, 2015
    Posts:
    5
    Had the same issue using TAB-Key.

    Then I switched the Interaction on that Action to "Release Only" and it started working.
    Seems the preliminary analysis of others in this thread is correctly : if you call SwitchCurrentActionMap() during the Completed-phase of a "Press Only" Interaction, the control-state becomes unstable.
     
  10. philippelabalette

    philippelabalette

    Joined:
    Mar 23, 2019
    Posts:
    14
    Issue solved in the 1.0.0-preview 6. Thanks!!
     
    Rene-Damm likes this.