Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

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

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

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    968
    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. :(
     
    Chiefkat and Bencarbon like this.
  2. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    23
    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
     
    EZaca, RastyBasic and Chiefkat like this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    968
    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:
    968
    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:
    2
    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 ?
     
    salah-rashad, Tryptex and Bencarbon like this.
  6. Rene-Damm

    Rene-Damm

    Unity Technologies

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

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    968
    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:
    14
    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
     
  9. camposcreativestudios

    camposcreativestudios

    Joined:
    Jun 2, 2020
    Posts:
    2
    Is there any update on dampening the input value? I'm currently using v 1.0.
     
  10. Balaa

    Balaa

    Joined:
    May 3, 2016
    Posts:
    1
    Hey, i find a solution for this. If anyone is still looking for. Basically if you put your "inputSystem.movement.ReadValue<x>()" inside "Awake()" or "Start()" you will actually invoke the callback just one time when the system detects the input and never updating that value until it comes back to "input.movement.cancelled" and the player make another move so, the solution is to put your "input.movement.ReadValue<x>()" in Update(). Then everytime it updates it will get the current updated value of the input.
     
    katanakacper likes this.
  11. FileThirteen

    FileThirteen

    Joined:
    Oct 23, 2012
    Posts:
    27
    Alternatively could you tell us the math equation that is used in the old Input Manager that takes sensitivity and gravity into account?
     
  12. KUNGERMOoN

    KUNGERMOoN

    Joined:
    Apr 29, 2020
    Posts:
    4
  13. kris8360

    kris8360

    Joined:
    Jan 11, 2013
    Posts:
    34
    Hi, I have the same problem too, I managed to change every other input in my game to the new input system, except for the movement Input.GetAxis Horizontal/Vertical. Did you manage to find a solution?

    My main problem is, that all I get is digital input with the WASD keys, even if I set the control type to Analog.
    With the old GetAxis, I got a smooth acceleration, it took a couple of frames to go from 0 to 1. Now its instant, and when I run in circles it looks stupid.

    Even with a PS4 controller's analog stick, the old Input.GetAxis is a 1000 times smoother, when I run around in circles with the new input system it's really choppy. (update mode set to Dynamic Update)
     
  14. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    214
    Polyternity and SomeGuy22 like this.
  15. kris8360

    kris8360

    Joined:
    Jan 11, 2013
    Posts:
    34
    awesome, thank you!
     
  16. samhain323

    samhain323

    Joined:
    Aug 4, 2020
    Posts:
    4
    How were you accessing the value in the sript. Can't seem to figure it out. The vector version I use var v = value.Get<Vector2>(); but I can't figure out how to access the values when using the 1D axis
     
  17. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,811
    APSchmidt and Laaglandje like this.
  18. Dynablaster

    Dynablaster

    Joined:
    Feb 28, 2014
    Posts:
    9
    I never post but I have to this time...

    This is what pisses me off with Unity. I was like oh they have a new input system that looks cool on the surface and then you realize its missing some core functionality... So many systems like this get updated and then they lose some of key features. Why put something out there that doesnt even cover all what the old system could do? Because that should be goal of an update, first cover everything that the old system could do and wrap it in the new fancy system that you are developing and then add features. Yes you said its coming but why didnt you wait till you had processors with this functionality this way just puts people off...
     
    SMHall, altair2020, M4R5 and 4 others like this.
  19. Flarup

    Flarup

    Joined:
    Jan 7, 2010
    Posts:
    164
    I am also looking into replicating the "old" Input.GetAxis method using the new input system. And as other people also have written, I would love to use WASD as fallback controls, when no Xbox/PS controller is connected to the system.

    I'm using a "1D Axis" for this, and I'm using the C# callbacks. One thing I noticed is that I only get the onActionTriggered callback when the state of the key CHANGES. I would love to get a callback EVERY frame the key is held down, so I can keep moving the player as long as the key is held down - or as long as the joystick is held to one of the sides.

    How do I configure the new input system to give me a callback every frame?


    Kind regards,
    Uffe
     
  20. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    214
    https://docs.unity3d.com/Packages/c.../api/UnityEngine.InputSystem.InputAction.html

    states

    "
    When input is received on controls bound to an action, the action will trigger callbacks in response. These callbacks are started, performed, and canceled. [...]

    [...] The default behavior is that when a control is actuated (that is, moving away from its resting position), started is called and then performed. Subsequently, whenever the a control further changes value to anything other than its default value, performed will be called again. Finally, when the control moves back to its default value (i.e. resting position), canceled is called.

    [...] In these callbacks, you will receive a InputAction.CallbackContext with information about how the action got triggered. For example, you can use ReadValue<TValue>() to read the value from the binding that triggered or use interaction to find the interaction that is in progress.

    action.started += context => Debug.Log($"{context.action} started");
    action.performed += context => Debug.Log($"{context.action} performed");
    action.canceled += context => Debug.Log($"{context.action} canceled");
    "

    inside the Remarks at the top towards the end,
     
    Flarup likes this.
  21. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    214
    event "performed" in Input System 1.1.0-preview3 sounds like it can be what you are seeking.
     
  22. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    214
  23. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    214
  24. Flarup

    Flarup

    Joined:
    Jan 7, 2010
    Posts:
    164
    I have just tested this, using the latest preview version of the new input system.

    And unfortunately, I only get "changes" when I get the "performed" callback. So as soon as I have moved the joystick all the way to one side, then I get no more "performed" callbacks until I move the joystick again.

    @Rene-Damm Is this working as designed or a bug? If this is the design, how would I get joystick moved callback every frame?
     
  25. smatthews1999

    smatthews1999

    Joined:
    Jan 14, 2018
    Posts:
    9
    I don't know if people are still struggling with this .. but I was. Here's how I solved basic WASD character movement.

    upload_2021-4-30_10-2-32.png

    and then processed with the following code...

    Code (CSharp):
    1.  
    2. private void Awake()
    3.     {
    4.         input = new PlayerInput();
    5.  
    6.         input.CharacterControls.Movement.performed += ctx =>
    7.         {
    8.             var v2 = ctx.ReadValue<Vector2>();
    9.             movement = new Vector3(v2.x, 0,v2.y);
    10.         };
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         transform.Translate(movement * Time.deltaTime);
    16.     }
    In the input 'interactions' it's important to use 'Press and Release' so the 'zero' vector will be recorded and the player will stop moving.
     
  26. Skewworks

    Skewworks

    Joined:
    Jul 13, 2017
    Posts:
    1
    If you want to simple read the value at any point you can do so by finding the action and invoking a read directly. You don't have to wait for an event. In the example below it gets the action at start and reads on every update.

    Code (CSharp):
    1.         public string actionName = "SomeAction";
    2.         private InputAction action;
    3.  
    4.         private void Awake()
    5.         {
    6.             playerInput = GetComponent<PlayerInput>();
    7.             action = playerInput.actions.FindAction(actionName);
    8.         }
    9.  
    10.         private void Update()
    11.         {
    12.             Debug.Log("Action Value: " + action.ReadValue<float>());
    13.         }