Body stops moving when the button is not pressed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By godot frajer

Hi, I’m a begginer in programming and I’m working on a fun, little project to gain experience.
My problem is that I’m trying to make a body that moves constantly but it only moves when I press an input.

extends KinematicBody2D

export (int) var speed = 0
export (float) var rotate_speed = 1.5
export (int) var max_speed = 200

var movement = Vector2(0, 0)
var rotate = 0

func get_input():
	# movement
	rotate = 0
	movement = Vector2()
	if Input.is_action_pressed("go_right"):
		rotate += 1
	if Input.is_action_pressed("go_left"):
		rotate -= 1
	# speeding up with "go_forward" and slowing down with "go_backward"
	if Input.is_action_pressed("go_forward"):
		movement += Vector2(0, -speed).rotated(rotation)
		speed += 1
	if Input.is_action_pressed("go_backward"):
		speed -=1
	if speed > max_speed:
		speed = max_speed
	if speed < 0:
		speed = 0

func _physics_process(delta):
	#some stuff that I don't understand even in the slightest
	rotation += rotate * rotate_speed * delta
	movement = move_and_slide(movement)

Here’s the entire code. I have a suspicion that it might be because forward movement should be under physics process function but even then I have no idea how to make it work. I hope I explained it well enough. All help would be appreciated.

:bust_in_silhouette: Reply From: njamster

The _physics_process-method is called every (physics-)frame. And, in turn, calls your get_input-method to redefine the value of movement based on the player input. But at the start of that function you override it’s value regardless of the inputs:

func get_input():
    # ...
    movement = Vector2()   # movement is set to (0, 0)
    # ...

This will be its final value unless the player is pressing go_forward or go_backward, i.e. if no keys are pressed the body will stop moving as movement == (0, 0).