Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Reading Mouse Delta/Joystick on custom binding

Discussion in 'Input System' started by Reddevildragg, Oct 10, 2019.

  1. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    42
    Hi

    Trying to look to find a way (if possible) to read a vector2 (from mouse delta for example) in a custom binding. currently have the below but are unabel to read into the vector2 as it does not have a icomparable and is throwing an error, have tried to make a custom vector2 struct with one and use that but getting an error. Looking to make it only track this input if the right mouse is down as a modifier as part of this input (for now ill just have a new binding for this but if anyone knows how to read mouse delta in a custom input binding that would be brilliant)


    Code (CSharp):
    1.  [InputControl(layout = "Vector2")]
    2.     public int binding;
    3.    
    4.     [InputControl(layout = "Button")] public int modifier;
    5.    
    6.     static Vector2BindingWithModifierComposite()
    7.     {
    8.         InputSystem.RegisterBindingComposite<Vector2BindingWithModifierComposite>();
    9.     }
    10.     [RuntimeInitializeOnLoadMethod]
    11.     static void Init() {} // Trigger static constructor.
    12.  
    13.     public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    14.     {
    15. //check modifier is on here
    16.         return context.ReadValue<Vector2>(binding);
    17.     }
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,387
    Passing an IComparer to InputBindingComposite.ReadValue should solve the problem. There is a predefined Vector2MagnitudeComparer in the input system.

    Code (CSharp):
    1. return context.ReadValue<Vector2, Vector2MagnitudeComparer>(binding);
    (uses default instance above)
     
  3. Reddevildragg

    Reddevildragg

    Joined:
    May 19, 2014
    Posts:
    42
    That looks like it did the job

    Thanks :)
     
unityunity