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

    public void Movement(InputAction.CallbackContext context) {
        inputVector = context.ReadValue<Vector2>();
        Debug.Log(inputVector);
    }

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?

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 if(!context.performed) return; to only do something when it is the performed phase.

Thanks for your help.I use the followed code to check the phase

    public void Movement(InputAction.CallbackContext context) {
        Debug.Log(context.phase);
        inputVector = context.ReadValue<Vector2>();
        Debug.Log(inputVector);
    }

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.

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.

using UnityEngine;

namespace PhantomDragonStudio.Input
{
    [System.Serializable]
    public class MovementProcessor
    {
        [Range(0.1f, 5f)][SerializeField] private float movementSensitivity;
        [Range(0f, 5f)][SerializeField] private float movementSpeedModifier;
        [SerializeField] private InputMap _inputMap;
        public Vector2 RawInput { get; private set; }
        public bool ShiftModifier { get; private set; }
        public float ModifedPanSpeed => movementSpeedModifier;

            public void Initialize()
        {
            if (_inputMap == null)
                _inputMap = new InputMap();

            _inputMap.Player.Move.Enable();
            _inputMap.Player.ShiftModifier.Enable();
            _inputMap.Player.ShiftModifier.performed += ctx => ShiftModifier = true;
            _inputMap.Player.ShiftModifier.canceled += ctx => ShiftModifier = false;
            _inputMap.Player.Move.performed += ctx => RawInput = ctx.ReadValue<Vector2>().normalized * movementSensitivity;
            _inputMap.Player.Move.canceled += ctx => RawInput = Vector2.zero;
        }
    }
}

Oh I did put a hold interaction on it and forgot to remove…silly me. It works correctly now.
I really appreciate your help.

1 Like