- Home /
Input System gets inputs only once then stops.
So yeah, I started learning the new input system today. I've tried to troubleshoot and follow tutorials exactly already but I can't seem to figure it out.
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public Rigidbody rb;
Player_Input controls;
float moveSpeed = 0.25f;
Vector2 moveDirection;
// Start is called before the first frame update
void Awake()
{
controls = new Player_Input();
controls.Gameplay.Movement.performed += context => moveDirection = context.ReadValue<Vector2>();
controls.Gameplay.Movement.canceled += context => moveDirection = Vector2.zero;
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
}
public void player_Move()
{
Vector3 tmpVector3 = new Vector3(moveDirection.x,0f,moveDirection.y);
rb.MovePosition(transform.position + tmpVector3);
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
So when the engine is running, the gameobject moves once, then stops no matter how long you hold down the button. When I had a controller added, it did the same thing. It would move if you changed direction(once per analog direction change) but keeping any direction held would result in a standstill. It acts like it's "GetKeyDown" rather than "GetKey." I've tried different inputs, movement types, keys, action and control types, etc.
The official doc had some information but I thought it was kinda broad and didn't help really. I've followed a few different tutorials exactly. A lot of the Information that I've found so far has been kinda......meh. Or rather, they just completely leave out some stuff entirely or doesn't explain what they are doing or why they are doing it that way.
Anyway, am I just a dummy and doing something wrong or is this a weird bug?
Answer by _Frost · May 02, 2021 at 02:54 AM
I figured it out, so I'm leaving this here in case anyone else stumbles into this. So the Input system kind of does not fetch your keybinds from your input controller during runtime. Like it does, but only for each new key press. It doesn't check if you are holding it still. So.. after you make your function that gets called by message or event, put the function in update as well.
void Update()
{
OnMovement();
}
public void OnMovement()
{
Vector3 tmpVector3 = new Vector3(moveDirection.x,0f,moveDirection.y) * moveSpeed;
rb.MovePosition(transform.position + tmpVector3);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613033139im_/https://answers.unity.com/themes/thub/images/avi.jpg)