Search Unity

[SOLVED] Input System: Second Action Map beeing ignored

Discussion in 'Input System' started by EduardoBastos, Apr 25, 2020.

  1. EduardoBastos

    EduardoBastos

    Joined:
    Oct 11, 2017
    Posts:
    7
    Hello everyone,

    So, I am using the new input system, and I have one InputActions asset with two Action Maps.
    The first action map (Figure 1) works properly, but the second one (Figure 2) gets ignored when I change to it.

    My player has a PlayerInput component, and some code to move around in response to the commands.
    I am currently using the following code to test the change in Action Map:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. public class ChangeActionMap : MonoBehaviour
    6. {
    7.     // TESTING CONTROL SCHEME CHANGE
    8.     PlayerInput playerInput;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         playerInput = GetComponent<PlayerInput>();
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         var _keyboard = Keyboard.current;
    18.         if (_keyboard.nKey.wasPressedThisFrame)
    19.         {
    20.             playerInput.SwitchCurrentActionMap("Gameplay2");
    21.             Debug.Log(playerInput.currentActionMap.ToString());
    22.         }
    23.         if (_keyboard.mKey.wasPressedThisFrame)
    24.         {
    25.             playerInput.SwitchCurrentActionMap("Gameplay");
    26.             Debug.Log(playerInput.currentActionMap.ToString());
    27.         }    
    28.     }
    29. }
    So when I press "n", PlayerInput changes to the second action maps, and no command moves the player. I get no error messages. And when I press "m", controls work again.

    Has anyone experienced such a problem, or can identify the error?

    Thanks in avance!!
     

    Attached Files:

  2. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    618
    It looks like you have two action maps that both define a "Move" action. Because they are in different maps - even though they have the same name, they actually produce two distinct InputActions (which each have their own callbacks!)

    How are you reading/polling/listening to the Inputs? With PlayerInput SendMessage, Unity Events, C# Events, or manually through the InputActionAsset?
     
  3. EduardoBastos

    EduardoBastos

    Joined:
    Oct 11, 2017
    Posts:
    7
    Yess!! This solved my problem!! Thank you so much!
    I was reading the values with the following code (for Move, as an example):
    Code (CSharp):
    1. playerInput.actions.FindAction("Move").performed += ctx => i_movement = ctx.ReadValue<Vector2>();
    2. playerInput.actions.FindAction("Move").canceled += ctx => i_movement = Vector2.zero;
    I assumed that changing the Action Map would make this "FindAction()" to automatically search on the current action map, but apparantly it found the one on the first ActionMap, then ignored it for not beeing the active one.
    So giving my actions different names, and telling the code to listen to all of them did the trick.

    I was struggling with this for days already. Thanks again for the help!
     
    Fenrisul likes this.
  4. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    618
    Nice! :) Glad to help heh.

    Also the "Gamepad and Keyboard Simultaneous Support" is kinda what Control Schemes are for.

    Additionally it looks like you're trying to split the keyboard into a Left Half Right Half type thing.

    https://forum.unity.com/threads/keyboard-splitter-local-multiplayer-keyboard.874135/
    If thats the case - take a look at this thread / utility I kicked out the other day heh - will save you a lot of pain with PlayerInput.


    @Rene-Damm
    Does it make sense to allow same-name InputActions existing seeing as FindAction and the [actionName] indexer exist? Seems weird to have a dictionary-like indexer without guaranteed unique indexes. Maybe a LogWarning at least in the editor when you try to FindAction and it sees more than one valid hit?
     
    valentin56610 likes this.