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. Dismiss Notice

Resolved Input.GetKeyDown vs Input Actions

Discussion in 'Scripting' started by Vandereck, May 1, 2023.

  1. Vandereck

    Vandereck

    Joined:
    Oct 16, 2022
    Posts:
    12
    Hi Everyone,

    I'm starting to learn about the new Input system with Input actions and I wanted to check what are the difference between this and writing an Input.GetKeyDown()? Is there any benefits in using one of the other?

    Many thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    It's two different systems. Input.GetKeyDown() is the old system.

    Personally I am still using the old system for ALL of my games because it just works.

    The only exception is for my VR game where I had no choice since the oculus kit requires you to use the new system, at least AFAICT
     
    Vandereck and Unifikation like this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    So, at first glance, all the YouTube videos will gush about it, and show you the first 45 minutes worth of setting up an ActionMap and call it a day. That really can't give anyone a good understanding of the system or its benefits. And it definitely doesn't highlight all the little nagging issues that come from having such a new and overengineered spaghetti of beta code that is still being worked out.

    I had to do a deep dive into writing a bit of code at the low end of the Input System to start to appreciate the structure a bit more.

    At the very lowest level, it's almost identical. And you can use them in almost identical ways, with just some funny names.

    Code (CSharp):
    1. #if ENABLE_INPUT_SYSTEM
    2.     if (Keyboard.current.cKey.wasPressedThisFrame)
    3. #else
    4.     if (Input.GetKeyDown(KeyCode.C))
    5. #endif
    6.  
    The ".current" there gives a hint at one big strength of the InputSystem. Multiple controllers may be active. Doing Couch Coop or want someone to be able to switch from Gamepad to Keyboard seamlessly? The new system wraps every button, potentiometer, joystick and gyro into InputControl, and an InputDevice is just a collection of InputControl elements.

    Some controls are really ambiguous, when you get down to it. The DPad of a Gamepad is registered both under a 2D Joystick-like control and also a set of four buttons simultaneously, so you can respond to either one depending on what would make your code make more sense.

    The ActionMap is just a structured collection of bindings. The Input System is saying "okay, for every current device, for every binding in the current action map, save that state and notify any/all code that has been subscribed to the events." I do wish there was a better way of aggregating ActionMaps so you don't have to repeat your bindings for global keys in all of your specialized maps like walking, driving, swimming, flying, but the idea that all of the bindings are in these groups helps make things more flexible to users' preferences and accessible for handicap play.

    And saving and loading and letting users re-map their bindings is all pretty much a single line of code.

    At the highest level, there is still a lot of rough edges and things I still find overburdensome and annoying. The PlayerInput class is finicky and has changed a lot in the last several iterations of public releases. The good news is, if you don't like it, you don't need to use it. You can work with ActionMaps directly, or you can work with InputControls directly.
     
    spiney199, Ryiah and Unifikation like this.
  4. Vandereck

    Vandereck

    Joined:
    Oct 16, 2022
    Posts:
    12
    Hi Halley,

    Thank you very much for the detailed answer, at this I only tried when Importing a small game to Android following a course on Udemy and for simple tasks like moving and simple attacks I did see any difference in using the Input System, however as soon as I get on more intermediate level I will give it a go, thank you.
     
  5. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    I really like new system because it only listen when you hit button and not in update method and also you set your binding for specific action e.g. Jump and you map it for all input controllers. Gamepad,Keyboard or whatever and then in your script you just work with the action not with a specific key. That is much better then old system.
     
    Vandereck likes this.
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
  7. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    If you are using the keyboard there is no difference. But now let say you need to also check that the gamepad South (A) button has been pressed. With Input.GetKeyDown() no deal, you will need to use Input.GetButton(). With the new input system you can bind your key and your gamepad button to the same InputAction and then check it with either:

    Code (csharp):
    1.  
    2. if (controls.myAction.wasStartedThisFrame) // started
    3.  
    4. if (controls.myAction.wasPerformedThisFrame) // continuing
    5.  
    6. if (controls.myAction.wasReleasedThisFrame) // stopped
    7.  
    Or you can have calls like that:

    Code (csharp):
    1.  
    2.         controls.myAction.started += myAction;
    3.  
    4.         private myAction(CallbackContext context)
    5.         {
    6.           //...
    7.         }
    8.  
    I recommend watching samyam's videos on Youtube if you want to learn more about the new input system, she covered it pretty well.
     
  8. Vandereck

    Vandereck

    Joined:
    Oct 16, 2022
    Posts:
    12
    @_geo__ Thank you for sharing the link, I will read it through.

    @dlorre Thanks for explaining, seems a better system for a multi platform.