Search Unity

InputAction.performed behaves differently [Solved]

Discussion in 'Input System' started by thirdcoke, Jan 22, 2020.

  1. thirdcoke

    thirdcoke

    Joined:
    Nov 2, 2015
    Posts:
    8
    So here are my setup, I have a Vector3 input and a float input.

    Thing is, the Vector3 input performed when I press the key and release the key, so that I'm updated with 0,0,1 and 0,0,0 value.

    But the float input only updates me when I press the button, and I would like it to perform a 0 when I release the key.
    demo.gif

    FYI this is the Vector3 composite class I created.

    Code (CSharp):
    1. [InitializeOnLoad]
    2. public class Vector3Composite : InputBindingComposite<Vector3>
    3. {
    4.     [InputControl(layout = "Button")] public int forward = 0;
    5.     [InputControl(layout = "Button")] public int backward = 0;
    6.     [InputControl(layout = "Button")] public int left = 0;
    7.     [InputControl(layout = "Button")] public int right = 0;
    8.     [InputControl(layout = "Button")] public int up = 0;
    9.     [InputControl(layout = "Button")] public int down = 0;
    10.  
    11.     public override Vector3 ReadValue(ref InputBindingCompositeContext context)
    12.     {
    13.         bool forwardIsPressed = context.ReadValueAsButton(forward);
    14.         bool backwardIsPressed = context.ReadValueAsButton(backward);
    15.         bool leftIsPressed = context.ReadValueAsButton(left);
    16.         bool rightIsPressed = context.ReadValueAsButton(right);
    17.         bool upIsPressed = context.ReadValueAsButton(up);
    18.         bool downIsPressed = context.ReadValueAsButton(down);
    19.  
    20.         Vector3 result = Vector3.zero;
    21.         result.x = rightIsPressed ? 1 : (leftIsPressed ? -1 : 0);
    22.         result.y = upIsPressed ? 1 : (downIsPressed ? -1 : 0);
    23.         result.z = forwardIsPressed ? 1 : (backwardIsPressed ? -1 : 0);
    24.  
    25.         return result;
    26.     }
    27.    
    28.     static Vector3Composite()
    29.     {
    30.         InputSystem.RegisterBindingComposite<Vector3Composite>();
    31.     }
    32.  
    33.     [RuntimeInitializeOnLoadMethod]
    34.     static void Init() {} // Trigger static constructor.
    35.    
    36. }
     
  2. thirdcoke

    thirdcoke

    Joined:
    Nov 2, 2015
    Posts:
    8
    OK so seems I figured this out on my own.

    The first thing I noticed is that the Action(ThreeDCompositeInput) using Vector3Composite - the composite method I write myself - is always at performed phrase, after I "actuated" the action(press the corresponding key once).

    And I read about the Default Interaction to figure out why it's always in performed phrase. It turns out how the bound control is involved here. So I just added the other override method in my Vector3Composite class, to solve the issue.

    Code (CSharp):
    1. public override float EvaluateMagnitude(ref InputBindingCompositeContext context)
    2. {
    3.     return ReadValue(ref context).magnitude;
    4. }
    Now it seems right just like the built-in binding: stays at started stage, and only go to performed whenever I press the key.
     
    Last edited: Jan 23, 2020