Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

The new input system. ???

Discussion in 'Input System' started by tclancey, May 25, 2020.

  1. tclancey

    tclancey

    Joined:
    May 19, 2017
    Posts:
    140
    So I thought I'd give this a go.

    Confused? Yes I am.

    I've just about got the hang of the input setup screen where you assign keys, but I can't get any input from a controller working at all, I can with the old system, although that's a pain as well.

    I've downloaded a load of "Simple" samples, simple my arse! Why make these samples do so much? I just want a VERY simple sample that shows getting an input. Also the samples I've downloaded all seem to use LowLevel, none of the tubes I've found use LowLevel.

    Could we please, please have the most simple of simplest examples of getting an input from a controller? I don't want graphs, I don't want flashy boxes, I don't want moment, just please put a value in a variable!!

    Rant over.

    I must admit I'm pretty new to unity and c#.

    Does anyone have a sample as simple as this I could have a look at? Please!!

    Many thanks.
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    990
    i had similar problems and am using old system, but in truth i have not tried new system properly. There is someone called 'brackeys' who may be able to help with tutorial.

     
    msibrava, DiogoDB and Ryiah like this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,506
    The following code assumes you've created and attached an input action asset to the script in the Inspector, and that the asset has an action map named "Gameplay" and that it has the actions "Movement" and "Fire".

    Line 25's
    ReadValue<Vector2>()
    is essentially the same as
    GetAxis("Horizontal")
    and
    GetAxis("Vertical")
    .

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     public InputActionAsset playerControls;
    7.  
    8.     private InputAction movement;
    9.     private InputAction fire;
    10.  
    11.     private void Awake()
    12.     {
    13.         var gameplayActionMap = playerControls.FindActionMap("Gameplay");
    14.  
    15.         movement = gameplayActionMap.FindAction("Movement");
    16.         movement.performed += OnMovementChanged;
    17.         movement.canceled += OnMovementChanged;
    18.         movement.Enable();
    19.  
    20.         fire = gameplayActionMap.GetAction("Fire");
    21.         fire.performed += OnFireChanged;
    22.         fire.Enable();
    23.     }
    24.  
    25.     private void OnMovementChanged(InputAction.CallbackContext context)
    26.     {
    27.         var direction = context.ReadValue<Vector2>();
    28.  
    29.         // Code that moves the player based on the direction
    30.     }
    31.  
    32.     private void OnFireChanged(InputAction.CallbackContext context)
    33.     {
    34.         // Code that fires a projectile
    35.     }
    36. }
     
    Last edited: May 25, 2020
    raant, Echo2606, whogas and 2 others like this.
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,919
    Who thought this was a good idea?
     
    Cynicat and Kevathiel like this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,506
    To be fair there is a far easier way to access the control state if you don't care about features like rebinding controls.

    Code (csharp):
    1. if (Keyboard.current.space.wasPerformedThisFrame)
    2. {
    3.     // FIRE
    4. }
    Code (csharp):
    1. if (Gamepad.current.aButton.wasPerformedThisFrame)
    2. {
    3.     // FIRE
    4. }
     
    reelie and adnanzmn97 like this.
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    990
    is the asset package 'rewired' any better? does anyone know.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,506
    I personally found rewired to be more difficult than the new input system.
     
    deus0 likes this.
  8. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    990
    am i wrong in thinking there is nothing wrong with old system, or am i getting myself into trouble down the line?
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,506
    There are no benefits to learning the old system at this point in time. Everything that you were able to do with it can be done with the new input system, and unlike the new input system the old one has no advanced functionality for when you need more it. Want rebinding on the old system? You'll have to build your own rebinding framework on top of it.
     
  10. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    2,387
    I've been using it since 5.6 and was able to do everything I wanted, but I only do windows and linux, dunno about consoles.
     
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,919
    They made a property for every key? This is nuts.

    Actually this seems to be awful through and through. Looking at Gamepad configuration:
    https://docs.unity3d.com/Packages/c...ngine.InputSystem.LowLevel.GamepadButton.html

    Someone thought that gamepad has fixed number of buttons.
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,506
    Ready for some more? There are at least four major ways of accessing input data. My first post showed the most complex method. Unity themselves show one of the intermediate methods in a blog post (first link) with the second intermediate method being the option for the input action asset to generate a C# class (second link).

    https://blogs.unity3d.com/2019/10/14/introducing-the-new-input-system/
    https://docs.unity3d.com/Packages/c....html#auto-generating-script-code-for-actions

    Remember the basic approach I posted earlier?
    Code (csharp):
    1. Keyboard.current.space.wasPressedThisFrame
    Here's a variant using an enum.
    Code (csharp):
    1. Keyboard.current[Key.Space].wasPressedThisFrame
    Here's a variant using a string.
    Code (csharp):
    1. ((KeyControl)Keyboard.current["space"]).wasPressedThisFrame
     
    deus0 and Gekigengar like this.
  13. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    5,599
    It's not that you won't be able to access the rest of them, they just created helpers for the common ones.
     
    angrypenguin likes this.
  14. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,919
    My opinion so far:
    "So we decided to address that and took inspiration from simplicity of japanese writing system."

    I didn't dive in very far, but on surface this seems to be horrible and worse than EventSystem for GUI.

    Few scenarios off the top of my head:

    * Let's say the user is using korean keyboard and I want to detect keypress of "ㅂ". Now what?
    * The user is utilizing unbranded usb clone of Sega Genesis gamepad. Which button is which?
    * My gamepad has 4 analog sticks and 28 buttons. How do I access them?

    I understand the need for low level API and polling, and abstraction for input device makes sense. But, I don't dig the layer at the very top with limited enums.
     
    Cynicat, Silly_Rollo, Ryiah and 2 others like this.
  15. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,490
    You can download the free Rewired trial version and get a good overview of how Rewired works first hand without spending any money.

    https://guavaman.com/projects/rewired/trial.html

    One of the features I like about Rewired is the huge number of controllers that are supported. The most important part of the number of controllers supported is that the developer has hands on experience with 98% of the controllers. That makes it so much easier when you ask a support question and get a informed response on the controller.

    https://guavaman.com/projects/rewired/docs/SupportedControllers.html

    The best thing you can do is evaluate both solutions and see what works best for you.
     
  16. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    2,910
    You can do all of that with the new input system for free. No trial - just free.

    "unsupported" controllers will still be accessible by axis and buttons, just a bit more hassle.
     
    Xepherys and Peter77 like this.
  17. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    7,279
    "A bit more hassle" is a bit of an understatement, because you eventually will likely need to purchase those controllers (especially ones that aren't xinput derived, especially if you're not only targeting Steam with its internal controller compatibility helpers) to test them in the first place. You can not rely on the idea (nor should you bank on the possibility at all) that you can collect this data from the userbase. There is a monetary and technical cost that comes with having to manually add controller support outside of the most baseline.
     
    Last edited: May 26, 2020
  18. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    13,459
    So... what do you all expect them to do with "unsupported" controllers? Clearly the more they can support the better, but they probably can't get 100% coverage and still need to do something for the rest.

    Assuming that functionality is present to allow runtime custom mappings then isn't that the solution? Ask the user to map what buttons they want for each action?

    I like Rewired too, and only moved away from it while cutting down on 3rd party dependencies. I had a pretty big project ported over to use the new Input System instead in less than a work day, having never looked at it before. So far my main gripe is with the documentation. The system is straightforward enough to use, but figuring out which bits are relevant to you is a bit of a pain the first time through, because they provide multiple ways to do things and no clear "approach X is best for cases where Y".
     
  19. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    7,279
    Honestly? There's not a lot of them to worry about.



    Xbox 360 and One support is just Xinput. PS4 is handled through the Steam controller system on that front and the most common drivers for it and PS3 both effectively work as Xinput emulators. That's 92% immediately covered even if you're not targeting Steam as a platform. 2.15%ish of the "other" is the Steam Controller, which is already effectively user mapped.

    We can rule out, generally, Rockband instruments and "everything else."

    The only real concern here is the 2.15%ish using "PC gamepads," which is a broad scale compatibility nightmare because it includes any manner of HID gamepad setups, often with very little standardization and, at last check, are often not at all easy to detect with the new input system.

    Also important to note is that this graph (from 2018) is not exclusive ownership. Many people in the edge case categories likely also own other controllers, as the amount of gamepad using accounts (at time of article) was "over 30,000,000," but there's 27,200,000 using 360 pads.

    That said, the big thing about Rewired is that it'll get you that extra compatibility for essentially free (you still have to buy it and use the automapper), while the new input system is putting that onus on the developer in a more direct way. Rewired also has the benefit of a lot of fantastic documentation alongside its active development. Even with the new input system being free, there are other costs to consider and I firmly believe that Rewired handles those very well.
     
    MadeFromPolygons likes this.
  20. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    13,459
    I'd also be concerned about people with custom designed "accessibility" controllers. I've only seen a couple in the wild, and both happened to be based on Xbox gamepads, but I don't know how commonly that's the case. If relevant that's the kind of thing where an engine vendor handling things centrally can give a direct, positive impact to often overlooked minority groups.
     
  21. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    7,279
    Accessibility controllers are basically one of those things where there are a couple of standards out there, but if you want to support them in any capacity, you're going to have to do in-house testing already because there's a lot of considerations to be made, as even the Microsoft one has expansion capability that may require special consideration. For instance, some people use a breathing tube for button inputs, but they may also use them for axis inputs.

    When you get into accessibility controllers, you're getting into something where you have to go through a lot.
     
  22. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    13,459
    Surely that's handled on their end and we just receive it as input from an arbitrary button or axis though?
     
  23. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,550
    Same way you do in the old system. If we're talking Korean keyboard layout as you find it on Windows, there is no [THAT CHARACTER] key (forum is being very peculiar; won't let me include the actual character...). The key is Q like on the English keyboard. The ㅂ character is IME text input. So that input comes through only in text input.

    To get input from that key, you'd bind to Q. To get it as text input, you'd go to Keyboard.onTextInput.

    You could bind to the key that generates the ㅂ character using ("<Keyboard>/#{ㅂ}") but there is no key in that layout that does that, so the binding wouldn't actually catch anything.

    By the nature of the beast, we can only guarantee that mapping for gamepads that we specifically support or where the underlying API we use guarantees that mapping for us. Unlike in the old system, we do make that guarantee, though, for devices we surface as Gamepads.

    At this point, the set of explicitly supported gamepads and joysticks is still rather restricted. It'll grow over time. Though I suspect that it'll remain true that the best solution for having a fallback to cover arbitrary HID gamepad/joystick/whatever is still user rebinds.

    The enums in the example are for key codes. On a PC keyboard, there is a more or less fixed set of those keys. The enum acts as a fast identifier for these standard keys which are identified by location (i.e. Key.A is the identifier for the *physical* key in-between Caps Lock and the S key regardless of which keyboard layout is employed). Keyboard devices, however, are not restricted to having keys only from this set. If you have a keyboard with 1024 keys, you can create a device for it.

    If it's not explicitly supported, the buttons and axes will be surfaced as generic controls as described in the HID descriptor of the device. You'll have button1 (comes out as "trigger"), button2, etc. and axes like rx, ry, etc. x and y will be turned into a stick. Hatswitches will be represented as dpads.

    Code (CSharp):
    1. // Look them up by name.
    2. joystick["button2"]
    3. joystick["rx"]
    4.  
    5. // Grab all buttons.
    6. joystick.allControls.OfType<ButtonControl>()
    7.  
    8. // Or only at toplevel.
    9. joystick.children.OfType<ButtonControl>()
    10.  
    11. // Bind to them from an action
    12. new InputAction(binding: "<HID::MyDevice>/rx"); // Or "<Joystick>/rx" or just "*/rx"
    13.  
    Some way to go here. Became more painfully obvious as we went. The system very much grew bottom up and with an eye towards along the way not railroading users into a "one option or no option" kind of deal. But we (me specifically) were pretty late in realizing that at the very toplevel, we need a single, clearly illuminated, no fuzz path. Which, at the very least, led to plenty confusion around what can and what cannot be done easily and how (and also to some things that *should* be easy but aren't yet).

    Anyway, we're chipping away at it. But it is clear to us that we're still some way from the "pick it up and it's pretty obvious" level we want to be at.
     
    Last edited: May 26, 2020
  24. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    2,910
    Thanks for the in-depth reply, greatly appreciated and I am sure appreciated by everyone else in the thread
     
  25. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    198
    I’m not really sure what all the ire is about. So far the new Input System seems far and away superior to the old system, and more straight-forward than Rewired.

    The documentation is pretty poor, but if you’ve been using Unity for more than half a minute, you should expect nothing more (documentation is a constant gripe of mine, but also writing and maintaining documentation sucks).

    Given the ease of using actions and binding/rebinding keys to those actions, given virtually any input device, anything can be bound to anything by the end user without much though (so long as you create a UI for user bindings, which you should).
     
  26. mysticvalleycsa

    mysticvalleycsa

    Joined:
    Sep 22, 2020
    Posts:
    11
    i wish I had known this before I spent the past three days setting up a character controller, and then while following a lighting tutorial discovered build settings.. smdh
     
unityunity