Search Unity

Bug New Input System - Solo keypress not detected until after a gamepad button has been pressed

Discussion in 'Input System' started by spambo, Jan 15, 2023.

  1. spambo

    spambo

    Joined:
    Sep 30, 2022
    Posts:
    1
    I'm new to game dev and I've been creating a simple movement script for my character using the new input system and I have run into a strange bug that I can't seem to fix. When the game is running, if I press a solo key to move e.g. W, the game does not appear to register the input, however, if 2 keys are pressed it does and if one key is let go but the other is kept pressed, the now solo key works as it should until it is released.

    Using an Xbox controller works perfectly but the strangest bit is that if an input has been detected from the controller (it doesn't matter if the input is registered to an action or not), solo pressing any movement key on the keyboard then works as it should and moves the character without any issues.

    Does anyone have any idea why this might be happening?

    Just an FYI, I have double-checked and all the keys are bound to their corresponding UP/DOWN/LEFT/RIGHT move action and it is set to Value and Vector2. Additionally, the issue of solo keypresses not registering persists when the keyboard is the only input device connected.

    This is my code:
    (I've cut out the bits that aren't to do with movement so excuse the unused variables)


    Code (CSharp):
    1.  
    2.  
    3.  
    4.  
    5. using System;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8. using Unity.VisualScripting;
    9. using UnityEngine;
    10. using UnityEngine.InputSystem;
    11.  
    12. public class PlayerController : MonoBehaviour
    13. {
    14.     [SerializeField] private Rigidbody _rb;
    15.     [SerializeField] private float _moveSpeed = 5;
    16.     //[SerializeField] private float _turnSpeed = 360;
    17.     private Vector2 _inputRaw;
    18.     private Vector3 _inputVector;
    19.     [SerializeField] private float _jumpForce = 100f;
    20.  
    21.     public PlayerInputActions playerInputActions;
    22.  
    23.     public Vector3 boxSize;
    24.     public float maxDistance;
    25.     public LayerMask layerMask;
    26.  
    27.     private void Awake()
    28.     {
    29.         playerInputActions = new PlayerInputActions();
    30.         playerInputActions.Player.Enable();
    31.        
    32.  
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.        
    38.  
    39.     }
    40.  
    41.     private void FixedUpdate()
    42.     {
    43.         ApplyHorizontalMovement();
    44.        
    45.     }
    46.  
    47.     void OnMove()
    48.     {
    49.         //reads Move values
    50.         _inputRaw = playerInputActions.Player.Move.ReadValue<Vector2>();
    51.        //converts 2D to 3D space
    52.         _inputVector = new Vector3(_inputRaw.x, 0f, _inputRaw.y);
    53.  
    54.  
    55.  
    56.     }
    57.  
    58.     void ApplyHorizontalMovement()
    59.     {
    60.        
    61.         //converts transformation to isometric axis
    62.         var moveIso = _inputVector.ToIso();
    63.        
    64.         if ((moveIso * _moveSpeed * Time.deltaTime).magnitude > 0)
    65.         {
    66.  
    67.            
    68.             _rb.MovePosition(transform.position + moveIso * _moveSpeed * Time.deltaTime);
    69.            
    70.             //rotates player towards move direction
    71.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveIso), 0.2F);
    72.            
    73.         }
    74.  
    75.       }
    76.  
    77. }
    78.