I had a RTS Camera script running fine, with 1 small issue. When I press the key for movement (WASD), it would move once, stop for a moment, and then keep going smooth….
I was using _Input
public override void _Input(InputEvent @event)
{
if (Input.IsMouseButtonPressed(MouseButton.Middle))
{
if (@event is InputEventMouseMotion mouse)
{
translate = new Vector3(mouse.Relative.X, 0f, mouse.Relative.Y) / mouse_speed;
}
}
else if (@event is InputEventKey key)
{
// W, A, S, D for movement
if (Input.IsKeyPressed(Key.W) ) // W
translate += Vector3.Forward;
if (Input.IsKeyPressed(Key.S)) // S
translate += Vector3.Back;
if (Input.IsKeyPressed(Key.A)) // A
translate += Vector3.Left;
if (Input.IsKeyPressed(Key.D)) // D
translate += Vector3.Right;
translate = translate.Normalized() * translation_speed;
}
}
So I went online to find a solution for it, and I saw some examples of people using the Keys in the _process instead… That, for some reason I don’t know, worked and it’s smooth without the delay.
But the same examples put the mouse in _UnhandledInput
I don’t fully grasp this… why putting the mouse in _unhandledInput instead of _Input? Why using _Input was delaying the response?
I don’t recommend this approach. The reason your character stops at first then keeps moving is because you’re essentially giving control over to your OS when it comes to input handling.
The best way I can describe it is this: Open up Notepad, and press a key and hold it down. You will see your OS type the letter, and after a second or so, it’ll start repeating that letter over and over again.
That’s exactly what’s happening here as well.
I recommend moving your input code to Process instead, and use the InputMap.
If there’s a GUI element on screen, if you put game controller logic into the _input function, the GUI elements will not block the input from reaching that function, which may be undesired. _unhandled_input will not be called for inputs that are blocked by GUI, however.
Though the reason people don’t use either function and use _process instead is that _process is called consistently every frame instead of only whenever the player does an input.