Input composite with vector 2 and button input

I want a composite with vector2 and button inputs to produce a vector 2.
The button is a modifier, where it will only return the input vector 2 with pressed, otherwise zero.

Trouble I have is with context.ReadValue(int partNum) doesn’t take a vector2:

 public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    {
        Vector2 moveValue = context.ReadValue<Vector2>(Move);
        float buttonValue = context.ReadValue<float>(Button);

Line 3 - Compile error:

Anyone know how to read in the Vector2?

full file

#if UNITY_EDITOR
[InitializeOnLoad] // Automatically register in editor.
#endif
public class ClickNMoveBinding : InputBindingComposite<Vector2>
{

    [InputControl(layout = "Value")]
    public int Move;

    [InputControl(layout = "Button")]
    public int Button;




    public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    {
        Vector2 moveValue = context.ReadValue<Vector2>(Move);
        float buttonValue = context.ReadValue<float>(Button);

        if (buttonValue>0)
        {
            return moveValue;
        }

        return Vector2.zero;
    }


    static ClickNMoveBinding()
    {
        InputSystem.RegisterBindingComposite<ClickNMoveBinding>();
    }

    [RuntimeInitializeOnLoadMethod]
    static void Init() { } // Trigger static constructor.
}
2 Likes

I think this might do it, though have yet to test it:

        Vector2 moveValue = context.ReadValue<Vector2, Vector2MagnitudeComparer>(Move);

(Required using UnityEngine.InputSystem.Utilities; for the Vector2MagnitudeComparer).

2 Likes

Yup, that’s correct. There may be multiple bindings for each part and if the value type isn’t IComparable, the system needs an IComparer to know how to decide which value to return.