Search Unity

"Press" interaction with stick/axis controls?

Discussion in 'Input System' started by Holonet, Jul 29, 2018.

  1. Holonet

    Holonet

    Joined:
    Aug 14, 2017
    Posts:
    84
    I'm not sure if this is simply not in the design or not, but it seems like the Press interaction does not work on an axis/continuous input control.

    My use case:
    I have a menu where the main input is the left control stick--for navigating the buttons, etc... One of those UI buttons selects the # of players, and when the stick is pressed in the x direction, it increases or decreases the value in a simple string list, which is later taken as the # of players. Just vanilla like that, of course it flies through the numbers before you can let off the stick.

    In the old system, I got around this with GetAxisRaw() and keeping track of if the stick was let go of with a bool, which worked great, despite the input system :p.

    I "got around" this with the following:

    Code (CSharp):
    1.  
    2.         var direction = (Vector2)ctx.ReadValue<Vector2>();
    3.         // Debug.Log(direction);
    4.  
    5.         if (Mathf.Abs(direction.x) < 0.15)
    6.             axisPressed = false;
    7.  
    8.         if (!axisPressed)
    9.         {
    10.             if (direction.x > 0.02)
    11.                 IncreasePlayers();
    12.             else if (direction.x < -0.02)
    13.                 DecreasePlayers();
    14.         }
    15.  
    16.         axisPressed = true;
    ...added that to the event of the leftStick action being "performed." So the reason "got around" is in quotes is because it works, but not well. You have to fiddle with it a bit, and that might be my budget function there/tweaking, but the "Press" interaction would be very welcome. I think there are many use cases for it as well, time permitting lol.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    ATM there's indeed not much help there from actions.

    One thing you got with the GetAxisRaw() polling code, I assume, is that this actually executed a value change over time instead of only in response to input changes. Which I think is an important part of the response here. I.e. as long as the stick is actuated on the X axis, you want to increase/decrease the value steadily over time (potentially scaling the rate with the amount the stick is actuated) instead of increasing/decreasing it directly in response to value changes on the stick X axis.

    Eventually, could be that actions will get functionality to drive responses over time. ATM one way to go about it instead would be something along the lines of

    Code (CSharp):
    1. private float m_IncreaseDecreasePlayerCountRate;
    2.  
    3. void OnEnable()
    4. {
    5.     myAction.performed +=
    6.         ctx => m_IncreaseDecreasePlayerCountRate = ctx.ReadValue<Vector2>().x; // Or just bind it to leftStick/x and read float.
    7. }
    8.  
    9. void FixedUpdate()
    10. {
    11.     if (m_IncreaseDecreasePlayersCountRate > 0)
    12.         IncreasePlayers();
    13.     else if (m_IncreaseDecreatePlayersCountRate < 0)
    14.         DecreasePlayers();
    15. }
    16.