Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Input composite with vector 2 and button input

Discussion in 'Input System' started by Sibz9000, Oct 30, 2019.

  1. Sibz9000

    Sibz9000

    Joined:
    Feb 24, 2018
    Posts:
    149
    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<T>(int partNum) doesn't take a vector2:

    Code (CSharp):
    1.  public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    2.     {
    3.         Vector2 moveValue = context.ReadValue<Vector2>(Move);
    4.         float buttonValue = context.ReadValue<float>(Button);
    Line 3 - Compile error:
    Anyone know how to read in the Vector2?

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. [InitializeOnLoad] // Automatically register in editor.
    3. #endif
    4. public class ClickNMoveBinding : InputBindingComposite<Vector2>
    5. {
    6.  
    7.     [InputControl(layout = "Value")]
    8.     public int Move;
    9.  
    10.     [InputControl(layout = "Button")]
    11.     public int Button;
    12.  
    13.  
    14.  
    15.  
    16.     public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    17.     {
    18.         Vector2 moveValue = context.ReadValue<Vector2>(Move);
    19.         float buttonValue = context.ReadValue<float>(Button);
    20.  
    21.         if (buttonValue>0)
    22.         {
    23.             return moveValue;
    24.         }
    25.  
    26.         return Vector2.zero;
    27.     }
    28.  
    29.  
    30.     static ClickNMoveBinding()
    31.     {
    32.         InputSystem.RegisterBindingComposite<ClickNMoveBinding>();
    33.     }
    34.  
    35.     [RuntimeInitializeOnLoadMethod]
    36.     static void Init() { } // Trigger static constructor.
    37. }
    38.  
     
    jlreymendez and mbussidk like this.
  2. Sibz9000

    Sibz9000

    Joined:
    Feb 24, 2018
    Posts:
    149
    I think this might do it, though have yet to test it:
    Code (CSharp):
    1.         Vector2 moveValue = context.ReadValue<Vector2, Vector2MagnitudeComparer>(Move);
    (Required using UnityEngine.InputSystem.Utilities; for the Vector2MagnitudeComparer).
     
    neorayb and hhoffren like this.
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    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.