- Home /
Input System: Input Action "started" Callback doesn't work properly on controller triggers?
Hello everyone,
I've encountered a problem with the Input System's reliability when using a controller's trigger buttons (known as LT resp. RT on XBOX controllers).
If I don't press the trigger buttons fast and strong enough (that is, down to the maximum within 1 frame), the InputAction's "started" callback function isn't being invoked. This causes a bug in my script, because I want to run different lines of code depending on whether or not the button was just pressed in the current frame. The "performed" callback function however is being invoked just as expected. So according to Unity, the InputAction is being performed but at the same time it hasn't even started yet...
The script works with keyboard keys and contoller face buttons, just not with controller trigger buttons (and yes, I tried multiple different controllers). However, I'd really like to keep the keybindings fully remappable. So can someone please explain if this is an intended behaviour? If so, any ideas for a workaround?
I attached an exemplary script down below:
public class DemoScript : MonoBehaviour
{
private Input
_input;
private bool
_inputCrouch,
_inputCrouchStart,
_inputCrouchCancel;
private void OnEnable()
{
_input.Enable();
}
private void OnDisable()
{
_input.Disable();
}
private void Awake()
{
//set up input system
_input = new Input();
_input.ActionMap.Crouch.started += ctx => _inputCrouchStart = ctx.ReadValueAsButton();
_input.ActionMap.Crouch.performed += ctx => _inputCrouch = ctx.ReadValueAsButton();
_input.ActionMap.Crouch.canceled += ctx => _inputCrouch = ctx.ReadValueAsButton();
_input.ActionMap.Crouch.canceled += ctx => _inputCrouchCancel = !ctx.ReadValueAsButton();
}
private void Update()
{
//not true if I just press the trigger halfway down
//not true if I slowly press it until max
//only true if I quickly press down to max
if (_inputCrouchStart)
{
Debug.Log("inputCrouchStart is True!");
}
//true if I just press the trigger halfway down
//and true if I slowly press it until max
//and also true if I quickly press down to max
if (_inputCrouch)
{
Debug.Log("inputCrouch is True!");
}
//invalidate both started and canceled inputs at the end of the update method
_inputCrouchStart = false;
_inputCrouchCancel = false;
}
}
Answer by EricEntertaiment · Jun 22, 2021 at 04:01 PM
To whom it may concern, it was pointed out to me that ReadValueAsButton() won't return true if the button (trigger in this case) hasn't been pressed at least halfway within the corresponding frame, at least per default.
There are multiple ways to deal with that, the quickest being to simply axe the ctx.ReadValueAsButton() and directly do this:
_inputCrouchStart = true;