Search Unity

Couple of newbie problems

Discussion in 'Input System' started by flatterino, Mar 27, 2019.

  1. flatterino

    flatterino

    Joined:
    Jan 22, 2018
    Posts:
    17
    Hello,

    I have two problems when it comes to testing out the new input system.

    I have a super simple WASD control scheme set up like so:

    upload_2019-3-27_20-6-15.png

    This is my code ("Input" is the auto-generated class):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class newInputSystemTests : MonoBehaviour
    6. {
    7.  
    8.     public Input input;
    9.  
    10.     void Awake()
    11.     {
    12.         input.Player.Move.performed += ctx => LogOnConsole(ctx.ReadValue<Vector2>());
    13.     }
    14.  
    15.     void LogOnConsole(Vector2 value)
    16.     {
    17.         Debug.Log("keyboard: " + value);
    18.     }
    19.  
    20. }
    Every keypress apparently is logging twice (on press, not on press and again on release), and also an exception happens in between, every time:

    upload_2019-3-27_20-8-54.png

    Any ideas? Thanks for taking the time, I'm using this for the first time today and I'm taking it step by step.
     
  2. Rallix

    Rallix

    Joined:
    Mar 17, 2016
    Posts:
    139
    I think you must enable the controls first to be able to use them.
    I. e. add something like this:

    Code (csharp):
    1. void OnEnable() => input?.Player.Enable();
    2. void OnDisable() => input?.Player.Disable();
    I'm pretty new to this, too, but I'm not sure whether "Key" action type is the right one to use here. I'd probably leave it on "Any" and see if anything changes.
    Also for movement, you'll probably want to use Continuous action (= trigger all the time while you hold a button) instead of Hold (= trigger once if you hold a button long enough).

    If it doesn't help, you could also try the "Generate Interfaces" approach and have something like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewInputSystemTests : MonoBehaviour, IPlayerActions
    4. {
    5.     [SerializeField] Input input = default;
    6.  
    7.     void Awake()
    8.     {
    9.         input?.Player.SetCallbacks(this);    
    10.     }
    11.  
    12.     void OnEnable() => controls?.Player.Enable();
    13.     void OnDisable() => controls?.Player.Disable();
    14.  
    15.     public void OnMovement(InputAction.CallbackContext context)
    16.     {    
    17.         if (context.performed)
    18.         {        
    19.             Debug.Log($"Keyboard: {context.ReadValue<Vector2>()}");
    20.         }
    21.     }
    22. }
     
    Last edited: Mar 27, 2019
    SystemId and flatterino like this.
  3. flatterino

    flatterino

    Joined:
    Jan 22, 2018
    Posts:
    17
    I should have pointed out that I enable the controls elsewhere. In fact, you can see them working: they do log the correct XY Vector2 in response to a keypress.

    I've solved this, but frankly I am completely unsure how it happened and I think this could be a bug. All I've done was remove the default Interaction for the action (it had a Hold interaction, added by default) and check and uncheck the "Continuous" checkmark.

    It works correctly now: no duplicate input, no exception between the duplicate inputs. Very strange.

    Thanks for the contributions, @Rallix.
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The duplicate triggering was the original default behavior that has since been changed. Basically, actions used to trigger on any single value change if there was no interaction added to a binding. So, one trigger on button down, one on button up.

    The default behavior has since been changed to trigger 'performed' only when a control starts being actuated (e.g. button goes down) and trigger 'cancelled' when a control is being released. The old behavior can be restored by toggling "Pass-Through" on.