- Home /
Multiple Key Presses Not Registering (New Input System)
Hello! I've been working on the movement for my game using the new Input System version 1.3.0 and I've run into an issue regarding multiple buttons being pressed at once.
Within my code, whenever an arrow key is pressed, the movement function is called, the movement function is repeatedly called if you hold down a direction, however the problem I've run into is, for example, when I hold the LeftArrow key and then hold down the UpArrow key the game will ignore the UpArrow key press and continue moving left. If I let go of the LeftArrow key while still holding the UpArrow the cursor will still continue to move left and won't stop until the UpArrow is released. This issue is also present when using a controller instead of keyboard. I'm wondering if this is an input system issue or a code issue, here's my movement code for reference. Any help would be really appreciated, thank you!!
public void RequestMove(InputAction.CallbackContext ctx)
{
// Prints the key pressed for testing purposes
if(ctx.started)
{
print(ctx.control);
}
if(ctx.performed)
{
buttonHeldCoroutine = ButtonHeld(ctx.ReadValue<Vector2>());
StartCoroutine(buttonHeldCoroutine);
if(!cursorMoving)
{
movementRequests.Push(ctx.ReadValue<Vector2>());
StartCoroutine(StartMovement(10f));
}
}
if(ctx.canceled)
{
ButtonReleased();
}
}
private void ButtonReleased()
{
buttonHeld = false;
movementRequests.Clear();
StopCoroutine(buttonHeldCoroutine);
}
private IEnumerator ButtonHeld(Vector2 direction)
{
yield return new WaitForSeconds(0.2f);
buttonHeld = true;
while(buttonHeld)
{
movementRequests.Push(direction);
StartCoroutine(StartMovement(10f));
yield return new WaitForSeconds(0.1f);
}
}
private IEnumerator StartMovement(float moveSpeed)
{
SoundManager.instance.PlayFX(2);
Vector2 poppedRequest = (Vector2)movementRequests.Peek();
currentPosition += (Vector2)movementRequests.Pop();
if(currentPosition.x < leftMost || currentPosition.x > rightMost){
currentPosition -= poppedRequest;
yield break;
}
if(currentPosition.y < bottomMost || currentPosition.y > topMost){
currentPosition -= poppedRequest;
yield break;
}
MoveCamera();
while((Vector2)transform.localPosition != currentPosition)
{
cursorMoving = true;
transform.localPosition = Vector2.MoveTowards(transform.localPosition, currentPosition, moveSpeed * Time.deltaTime);
yield return null;
}
cursorMoving = false;
}
Answer by Quest_for_stuff · Mar 29 at 03:17 AM
I think it's because you probably have WASD 2D Vector in new Input System. Since they all come together, you will need to release all WASD to have the context trigger canceled.