Search Unity

Using InputActions without generating class

Discussion in 'Input System' started by Hertzole, Oct 13, 2019.

  1. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    422
    Hi

    I've been eyeballing this for a really long time but never got into it. Finally decided to give it a go implementing it into my open-source player controller. That means I want it to be a bit more open, allowing users to change things if needed. I feel like generating classes is a no-go then because of how it directly changes things in code. This also becomes a bigger issue when the player controller is delivered through the package manager.

    So, is there a way for me to just use any input action and go by strings instead? I tried setting one up but failed. It never responds to any input. So am I doing something wrong or is it just not possible? I do also have the PlayerInput component but that didn't change anything. (Is there also a way to not use the PlayerInput component and still have input work?)
    This is my test code:
    Code (CSharp):
    1. [SerializeField]
    2. private InputActionAsset input = null;
    3.  
    4. private void Start()
    5. {
    6.     UpdateActions();
    7. }
    8.  
    9. private void OnEnable()
    10. {
    11.     input.Enable();
    12. }
    13.  
    14. private void OnDisable()
    15. {
    16.     input.Disable();
    17. }
    18.  
    19. private void Update()
    20. {
    21.     Debug.Log(input.FindAction("Move").ReadValue<Vector2>());
    22. }
    Thanks for your time
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Not sure what UpdateActions does, but judging from the rest of the code and assuming there is an InputActionAsset with a proper setup referenced through "input", I'd expect this to work.

    What does the input debugger say? Are the actions showing up as enabled and come out with the expected controls bound to them?

    How is the "Move" action configured and what's bound to it?
     
  3. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    422
    I actually got it to work a while later and I forgot to update this post, my apologies. The code above actually was correct but for some reason, it just refused to work. After a few restarts and fiddling with the input backend, it worked. But I also noticed that it didn't work if I had the PlayerInput component attached, so I guess another goal of mine was met.