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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How do I filter keyboard noise when using "Invoke Unity Events" as input behavior?

Discussion in 'Input System' started by MeruSama, Jul 18, 2020.

  1. MeruSama

    MeruSama

    Joined:
    Dec 12, 2017
    Posts:
    3
    Code (CSharp):
    1.     public void Movement(InputAction.CallbackContext context) {
    2.         inputVector = context.ReadValue<Vector2>();
    3.         Debug.Log(inputVector);
    4.     }
    I use code above to recieve player's input, but when I press and hold a certain botton like w, the debug log displays:
    [15:24:00] (0.0, 1.0)
    [15:24:00] (0.0, 0.0)
    [15:24:00] (0.0, 1.0)
    [15:24:00] (0.0, 1.0)
    How do I filter the noise like this?
     
  2. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55
    It's going to print a lot with the log statement inside the event receiving script because of the way the devices fire off events with attached values, unless you write another script to handle it and then tell something else to update only when the value changes.

    You might just want to try something like
    Code (CSharp):
    1. if(!context.performed) return;
    to only do something when it is the performed phase.
     
  3. MeruSama

    MeruSama

    Joined:
    Dec 12, 2017
    Posts:
    3
    Thanks for your help.I use the followed code to check the phase
    Code (CSharp):
    1.     public void Movement(InputAction.CallbackContext context) {
    2.         Debug.Log(context.phase);
    3.         inputVector = context.ReadValue<Vector2>();
    4.         Debug.Log(inputVector);
    5.     }
    And the log goes:
    [22:20:19] Started
    [22:20:19] (0.0, 1.0)
    [22:20:19] Canceled
    [22:20:19] (0.0, 0.0)
    [22:20:19] Started
    [22:20:19] (0.0, 1.0)
    [22:20:19] Performed
    [22:20:19] (0.0, 1.0)
    I use your code at the beginning of the method, and the rest code won't be excuted unless the botton is hold about half second.But I guess I know what to do.
    Again, thanks a lot.
     
  4. FullMe7alJacke7

    FullMe7alJacke7

    Joined:
    Dec 17, 2013
    Posts:
    55

    It depends how you're reading in the input. What update mode is it in? (Project Settings)
    What binding type is it? Does it have any processors on it? To me that sounds like you put a hold processor on it. Even with the line I put in there you shouldn't be waiting a half a second... It should be read within milliseconds either way. This is how I do it in my project and have no issues.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace PhantomDragonStudio.Input
    4. {
    5.     [System.Serializable]
    6.     public class MovementProcessor
    7.     {
    8.         [Range(0.1f, 5f)][SerializeField] private float movementSensitivity;
    9.         [Range(0f, 5f)][SerializeField] private float movementSpeedModifier;
    10.         [SerializeField] private InputMap _inputMap;
    11.         public Vector2 RawInput { get; private set; }
    12.         public bool ShiftModifier { get; private set; }
    13.         public float ModifedPanSpeed => movementSpeedModifier;
    14.  
    15.             public void Initialize()
    16.         {
    17.             if (_inputMap == null)
    18.                 _inputMap = new InputMap();
    19.  
    20.             _inputMap.Player.Move.Enable();
    21.             _inputMap.Player.ShiftModifier.Enable();
    22.             _inputMap.Player.ShiftModifier.performed += ctx => ShiftModifier = true;
    23.             _inputMap.Player.ShiftModifier.canceled += ctx => ShiftModifier = false;
    24.             _inputMap.Player.Move.performed += ctx => RawInput = ctx.ReadValue<Vector2>().normalized * movementSensitivity;
    25.             _inputMap.Player.Move.canceled += ctx => RawInput = Vector2.zero;
    26.         }
    27.     }
    28. }
     
  5. MeruSama

    MeruSama

    Joined:
    Dec 12, 2017
    Posts:
    3
    Oh I did put a hold interaction on it and forgot to remove...silly me. It works correctly now.
    I really appreciate your help.
     
    FullMe7alJacke7 likes this.