Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

so how do I get Input.GetKeyDown("A") ?

Discussion in 'Input System' started by laurentlavigne, Jul 27, 2020.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,434
    couldn't figure out the new system.
     
  2. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    232
    for one, I use https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Actions.html#action-types Button.

    Also see https://docs.unity3d.com/Packages/c...nityEngine_InputSystem_InputActionType_Button

    As I use https://docs.unity3d.com/Packages/c...ions.html#embedding-actions-in-monobehaviours, I set Button on the GO:

    Using Push Events, this might be enough. https://docs.unity3d.com/Packages/c...manual/Components.html#notification-behaviors You don't need a
    PlayerInput
    though, you can simply add event listeners to your MonoBehaviour-based Action, if you went my route: https://docs.unity3d.com/Packages/c...1.0/manual/Actions.html#responding-to-actions

    However, I use polling: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Actions.html#polling-actions, I converted my Update() GetKeyDown() to

    Code (CSharp):
    1. if (iA_rotate.ReadValue<float>() == 1)
    2.                             {
    3.                                 if (wasNotYetRead) {
    4.                                     wasNotYetRead = false;
    5.                                     // do stuff
    6.                                 }
    7.                             }
    8.                             else
    9.                             {
    10.                                 wasNotYetRead = true;
    11.                             }
    using a shielding flag.
     

    Attached Files:

  3. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    232
    an Action in the new Input System corresponds to an Axis in the old Input Manager. In that IM, the Axis bound to up to 4 keyboard keys and took input from any Gamepad and mouse simultaneously. In the IS, you can (and must) bind every device key/button/stick you like to get data from explicitly to the Action yourself.

    The value range is [0,1] for the most part (key not pressed = 0, key pressed = 1), [-1, 1] for sticks I guess (haven't verified this).

    To emulate sensitivity and gravity of an Axis of the IM, use sth. like this:

    Code (CSharp):
    1. accumulatedRollLeft += (rollleft.ReadValue<float>() * 2f - 1f) * Time.deltaTime;
    2.             accumulatedRollLeft = Mathf.Clamp01(accumulatedRollLeft);
    This emulates sensitivity and gravity of 1. The arithmetic maps [0,1] to [-1,1], ensuring key not pressed returns the value to 0. Clamping prevents overflow.
     
  4. kagamijun8810

    kagamijun8810

    Joined:
    Oct 6, 2020
    Posts:
    4
    How can i implement this, given the fact that i don't use the classic method of referencing the asset and doing the action.started += 'callback function'.. but instead i use the PlayerInput component's Events > Player Callbacks, (the ones that take a parameter of type CallbackContext)
    here's the part of script of my concern:
    Code (CSharp):
    1.  
    2. private PlayerMotor Motor;
    3.  
    4. public void OnMovement_H(InputAction.CallbackContext context) {
    5.         float _direction = context.ReadValue<float>();
    6.         Motor.Aim_H(_direction);
    7.     }
    8.  
    The action that triggers the OnMovement_H method's Action Type is set to Button and it has one 1D Axis Composite
     
  5. uani

    uani

    Joined:
    Sep 6, 2013
    Posts:
    232
    is _direction either 0 or 1 currently?

    if yes, replace rollleft in my code by context from yours and store accumulatedRollLeft as a class float initialised as 0.
     
  6. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,434
    register delegate, onevents... seems awfully complicated for quick work, how is it in actual production?
     
  7. Ofx360

    Ofx360

    Joined:
    Apr 30, 2013
    Posts:
    155
    UnityEngine.InputSystem.Keyboard.current.akey.wasPressedThisFrame

    Im not at the computer at the moment, but I believe thats the full path. Thats the easy way to quickly setup input without using the Actions or anything.

    Can also get to gamepad, etc through similar ways.
     
    laurentlavigne likes this.
  8. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,434
    nice!
    upload_2021-1-1_12-38-13.png

    now how do I get the vector2 from this thing?
    like that
    upload_2021-1-1_13-12-45.png

    answer:
    var input = UnityEngine.InputSystem.Gamepad.current.leftStick.x.EvaluateMagnitude();

    a mouthful. There is a very good blog on this:
    https://blog.eyas.sh/2020/10/unity-for-engineers-pt3-input-system/
     
    Last edited: Jan 1, 2021
    idbrii and Ofx360 like this.
  9. Ofx360

    Ofx360

    Joined:
    Apr 30, 2013
    Posts:
    155
    Ahh nice! I didnt know about that. Like your link describes, I typically use ‘ReadValue’:
    Code (CSharp):
    1. Using UnityEngine.InputSystem;
    2.  
    3. (...)
    4.  
    5. var input = Gamepad.current.leftStick.ReadValue();
     
    idbrii likes this.
  10. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,434
    EvaluateMagnitude is abs of ReadValue?
    what's the purpose of either?