Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to reproduce the standard Input.GetAxis("Horizontal") in the new input system?

Discussion in 'New Input System' started by FeastSC2, Jul 7, 2019.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    637
    How can I have the exact same values given by the Input.GetAxis("Horizontal") that every project has set up by default in Unity in the new Input system?

    I can't seem to reproduce the same values. :(
     
  2. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    20
    Code (CSharp):
    1. var myInputAction = inputMap.AddAction("Horizontal");
    2.  
    3. myInputAction.AddCompositeBinding("Axis")
    4. .With("Negative", <Keyboard>/a)
    5. .With("Positive", <Keyboard>/d);
    6.  
    7. myInputAction.passThrough = true;
    Or, do the same with object in inspector
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    637
    I should have specified that I want this for a gamepad my bad. Do you know how to replicate the gamepad's horizontal/vertical axis?
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    637
    I set the action type to Pass through and the control type to Axis with the left stick[gamepad].
    However the behaviour is just not the same as the Input.GetAxis("horizontal")/("vertical") standard to Unity.

    This is because the input returned by the new input system when I stop holding the joystick is not zero: Sometimes, there is some small value left over like 0.0043.

    I tried to add a processor with Axis Deadzone with a min of 0.1 but it doesn't change anything.

    Here's how I read it in code:
    Code (CSharp):
    1.     public void OnMove(InputAction.CallbackContext context)
    2.     {
    3.         MoveAxis = context.ReadValue<Vector2>();
    4.         Debug.Log("OnMove: " + MoveAxis.ToString("#.#####"));
    5.     }
    And here's the debug line it gives me when I stop holding the joystick.
    OnMove: (.02975, -.03459) ----> Non-zeroed.
     
  5. foshe_5

    foshe_5

    Joined:
    May 9, 2019
    Posts:
    1
    I did the same thing but in the inspector, but the values are fixed, meaning when I press A I get an output of (-1) and (1) if i press D, i want it to gradually go back to zero, how to do that ?
     
  6. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    828
    There's no support for this ATM. Stateful processors are on the list for >1.0.

    What gamepad are you using? When letting go of the stick, the value should go back to (0,0). If it doesn't, that would be a bug. What value are you seeing in the input debugger?
     
    SomeGuy22 likes this.
  7. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    637
    I'm using an XBOX controller and what I see in the input debugger is the line here:

    I looked at one of your demos in the projects available (in the PackageManager) and I saw that you guys checked to see if the value was too low like in the code below to avoid that issue.

    Code (CSharp):
    1.     public void OnMove(InputAction.CallbackContext context)
    2.     {
    3.         MoveAxis = context.ReadValue<Vector2>();
    4.         if (MoveAxis.sqrMagnitude < 0.01f)
    5.             MoveAxis = Vector2.zero;
    6.     }

    In the standard input system there's a "Dead" value. This might be the reason why it's different from your new input system.
    upload_2019-10-10_17-39-8.png
     
  8. gamefish

    gamefish

    Joined:
    Jul 5, 2013
    Posts:
    11
    I got my Input.GetAxis("Horizontal") working by using 1D Axis, just click the plus button and add 1D Axis Composit, hope this helps:
    Screen Shot 2019-10-21 at 7.50.17 PM.png