Search Unity

Twin stick style aiming action with gamepad joystick and mouse bindings

Discussion in 'Input System' started by ElnuDev, Feb 22, 2020.

  1. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Is it possible to create a single twin stick style aiming action gamepad joystick and mouse bindings? I'd like to get a
    Vector2
    value in the aiming direction, but I'm not sure how to approach this. Should I have separate actions for joystick and mouse aiming instead? Thanks in advance! :)
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    ATM this does indeed require either separate actions or awkward if() handling in callbacks.

    I'd recommend splitting this into one action meant to deliver screen-space 2D positions and one action delivering normalized direction vectors.
     
    ElnuDev likes this.
  3. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Okay! That's what I ended up doing. Will there be a more elegant way to deal with this kind of thing in future versions of the Input System? Thanks for the help!
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    It's on the list (ISX-344). It'll likely be a processor to apply to screen position bindings and some API to set the reference point to use. It's possible to implement such a thing manually today. For example something like:

    Code (CSharp):
    1.     #if UNITY_EDITOR
    2.     [InitializeOnLoad]
    3.     #endif
    4.     public class MouseDirectionProcessor : InputProcessor<Vector2>
    5.     {
    6.         public static Vector2 relativeToScreenPosition;
    7.  
    8.         static MouseDirectionProcessor()
    9.         {
    10.             InputSystem.RegisterProcessor<MouseDirectionProcessor>();
    11.         }
    12.         [RuntimeInitializeOnLoadMethod]
    13.         private static void Initialize()
    14.         {
    15.         }
    16.         public override Vector2 Process(Vector2 value, InputControl control)
    17.         {
    18.             return (value - s_RelativeToScrenPosition).normalized;
    19.         }
    20.     }