Search Unity

New input system - How do I access Up/Down/Left/Right?

Discussion in 'Scripting' started by fgbg, May 30, 2020.

  1. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Why do you want the bindings? You have WSAD mapped to a vector2 so a value of (0,1) would be W, (0,-1) would be S, and so on. You would read the vector value to determine what is being pressed.
     
  3. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Well, because I want to conceptually follow the idea of the new input system - to abstract away from keys or buttons, let alone Vector2's. I don't want to look up W/A/S/D or a damn Vector2... I want to use the binding (and label as you say?) that I literally just created. These are supposed to abstract away from the literal keys in the same way I make a binding for "Jump" and look for "Jump" events instead of SpaceBar on the keyboard or X on the p24 controller being pressed. To look for hard coded Vector2's is defeating the purpose.

    If I'm looking for a "Jump" action, why would I not be able to looked for "Move:Up" action as well? Does this make sense? To lose the second example is to not fully comprehend the concept that the new input system is trying to provide.

    To give a clear example, I want to know when the character is moving "Up", just like I know when the character "Jumps". I thought that's what the flexibility and abstraction that this new input system was giving me.

    So I could have both of these:

    Code (CSharp):
    1.         private void Jump_performed(InputAction.CallbackContext obj)
    2.         {
    3.             Debug.Log("Just Jumped");
    4.         }
    5.  
    6.  
    7.         private void Up_performed(InputAction.CallbackContext obj)
    8.         {
    9.             Debug.Log("Just Moved Up");
    10.         }
     
    Last edited: Jun 1, 2020
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    There isn't an up, down, left, or right action. The name of the action is Move which is a Vector2. The keys are assigned to a part of the vector. So pressing W adds 1 to the x value of the vector and pressing D adds -1 to the x value. It is the same for A and D but on the y value of the vector.

    So you would read the vector of the move action and move your object based on that. You would do something like this

    Code (CSharp):
    1.     private _controls = new Controls();
    2.  
    3.     private Vector2 _move;
    4.  
    5.     private void Start()
    6.     {
    7.         _controls.Enable();
    8.  
    9.         _controls.PlayerControls.Move.performed += ctx => movePlayer = ctx.ReadValue<Vector2>();
    10.     }
    11.  
    12.  
    13.     private void Update()
    14.     {
    15.         //Move your player based on the vaule of the _move Vector
    16.         transform.Translate(new Vector3(_move.x, 0, _move.y));      
    17.     }
     
  5. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    I appreciate that explanation as I will have to use it due to the lack of functionality I assumed would be here.

    To further my case though, I'd like to point out that whatever Unity calls these "Up/Down/Left/Right" labels do exist, just not in the object passed in the event. Notice them here in the class created by my Input Actions:

    Code (CSharp):
    1. ""bindings"": [
    2.                 {
    3.                     ""name"": ""WASD Move"",
    4.                     ""id"": ""a8184b5c-c0ab-452d-a665-1e17a6ce788a"",
    5.                     ""path"": ""2DVector"",
    6.                     ""interactions"": """",
    7.                     ""processors"": """",
    8.                     ""groups"": """",
    9.                     ""action"": ""Move"",
    10.                     ""isComposite"": true,
    11.                     ""isPartOfComposite"": false
    12.                 },
    13.                 {
    14.                     ""name"": ""up"",
    15.                     ""id"": ""66576d58-92e7-44fd-9ff5-c8cb2c712229"",
    16.                     ""path"": ""<Keyboard>/w"",
    17.                     ""interactions"": """",
    18.                     ""processors"": """",
    19.                     ""groups"": """",
    20.                     ""action"": ""Move"",
    21.                     ""isComposite"": false,
    22.                     ""isPartOfComposite"": true
    23.                 },
    24.                 {
    25.                     ""name"": ""down"",
    26.                     ""id"": ""50396557-0277-4767-9f53-d03f4e7a8926"",
    27.                     ""path"": ""<Keyboard>/s"",
    28.                     ""interactions"": """",
    29.                     ""processors"": """",
    30.                     ""groups"": """",
    31.                     ""action"": ""Move"",
    32.                     ""isComposite"": false,
    33.                     ""isPartOfComposite"": true
    34.                 },
    35.                 {
    36.                     ""name"": ""left"",
    37.                     ""id"": ""d5081255-17ba-413c-a2ee-3480f7ba11c8"",
    38.                     ""path"": ""<Keyboard>/a"",
    39.                     ""interactions"": """",
    40.                     ""processors"": """",
    41.                     ""groups"": """",
    42.                     ""action"": ""Move"",
    43.                     ""isComposite"": false,
    44.                     ""isPartOfComposite"": true
    45.                 },
    46.                 {
    47.                     ""name"": ""right"",
    48.                     ""id"": ""786c166f-f809-4515-9c65-8082a170df8c"",
    49.                     ""path"": ""<Keyboard>/d"",
    50.                     ""interactions"": """",
    51.                     ""processors"": """",
    52.                     ""groups"": """",
    53.                     ""action"": ""Move"",
    54.                     ""isComposite"": false,
    55.                     ""isPartOfComposite"": true
    56.                 },
    57.                 {
    58.                     ""name"": """",
    59.                     ""id"": ""7edfed20-78c0-44ba-8206-17366b546349"",
    60.                     ""path"": ""<Keyboard>/space"",
    61.                     ""interactions"": """",
    62.                     ""processors"": """",
    63.                     ""groups"": """",
    64.                     ""action"": ""Jump"",
    65.                     ""isComposite"": false,
    66.                     ""isPartOfComposite"": false
    67.                 }
    Would it not be fairly straight forward to allow for such functionality to exist? To just add these as a property so that when I poll I can check if they are "pressed"? Maybe this abstraction is just one step too far, but I would have appreciated it considering how nice the simpler button abstractions work, like for "Jump".

    This may be naive but part of my desire for this is readability. Your example is great, but it's not obvious how the player is moving from that alone. It's one or two jumps until it clicks for me. I don't prefer the logic to kind of "always be moving, but only be moving if a move key is pressed". I'd rather explicitly call it out like "if UP, then transform.y + 1".

    Just thinking out loud, maybe using the Vector2 concept here was my mistake in the first place. Maybe I should just use them as buttons instead of directly accessing the Vector2 values.
     
    Last edited: Jun 1, 2020
    Deleted User likes this.
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    There is a difference between a binding and an action. If you look, all of those bindings are tied to the same action which is move.

    Move is the event that fires when those bindings are pressed. When the move action is triggered, it takes the vaule of each binding in the current control scheme and puts them into a vector 2. If you want an up, down, left and right action, you can just create those. However, the way you have it now is that you can read the value of all 4 inputs in one line rather than have four different events fired just to get the direction of user inputs.