My input system doesn't register a button being held down

I’m trying to create movement with my character, but instead of continously walking on pressing WASD my character takes a step forward and stops quickly. When I used Debug.Log, the console only registered twice per button press (once when pressed and once after letting go). I am using currently using version 2022.3.11f1 (editor).

I have been trying to get this to work but I just don’t know what I’m doing wrong. Thank you for your time!

Code for my player…

    private PlayerInputAction playerInputAction;
    private Rigidbody playerRigidbody;

    [SerializeField] private float speed = 10f;

    private void Awake()
    {
        playerRigidbody = GetComponent<Rigidbody>();
        playerInputAction = new PlayerInputAction();
    }

    public void OnMovement(InputValue inputValue)
    {
        Vector2 moveDirection = inputValue.Get<Vector2>();
        playerRigidbody.velocity = new Vector3(moveDirection.x, 0, moveDirection.y) * speed * Time.deltaTime;
    }

And he is an image of my inspector (for the player…

1 Answer

1

The SendMessage option of the PlayerInput will send messages based on how your Input is set up, here’s a better explanation, In the case of a Vector2 action type: by default it will send a message every time the direction changes, aka when you press/release the direction keys.

the easiest way to make your script work without having to change any PlayerInputAction settings is to save the moveDirection Vector2 outside the OnMovement function and update the velocity on Update()

    private PlayerInputAction playerInputAction;
    private Rigidbody playerRigidbody;
    private Vector2 moveDirection;
    [SerializeField] private float speed = 10f;

    private void Awake()
    {
        playerRigidbody = GetComponent<Rigidbody>();
        playerInputAction = new PlayerInputAction();
    }
   private void Update()
   {
        playerRigidbody.velocity = new Vector3(moveDirection.x, 0, moveDirection.y) * speed * Time.deltaTime;
   }


    public void OnMovement(InputValue inputValue)
    {
        moveDirection = inputValue.Get<Vector2>();
    }

Thank you! I just tried this and it worked. I'm very thankful for your time and helpful advice.