Problem with walk script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Amateur.game.dev.

I created a walk script. Whenever my character is walking and i try to change direction, the character walks diagonally, before i release the button. How would I stop this from happening?

Edit:

#back
	if Input.is_action_pressed("Back"):
		velocity.y = -speed
		#$PlayerSprite.flip_h = false
		$icon.play("GoldWBack")
	elif Input.is_action_just_released("Back"):
		$icon.play("IdleBack")
		var facing_back = true
		if facing_back == true:
			if Input.is_action_just_pressed("LoadAmmo"):
				$icon.play("LoadArrowB")
			if Input.is_action_just_pressed("SwordSwing"):
				$icon.play("GSwordB")
	
	
	#forward
	elif Input.is_action_pressed("Forward"):
		velocity.y = speed
		$icon.play("GoldWForward")
	elif Input.is_action_just_released("Forward"):
		$icon.play("IdleForward")
		var facing_forward = true
		if facing_forward == true:
			if Input.is_action_just_pressed("LoadAmmo"):
				$icon.play("LoadArrowF")
	
	#left
	elif Input.is_action_pressed("Left"):
		velocity.x = -speed
		$icon.play("GoldWLeft")
	elif Input.is_action_just_released("Left"):
		$icon.play("IdleLeft")
		var facing_left = true
		if facing_left == true:
			if Input.is_action_just_pressed("LoadAmmo"):
				$icon.play("LoadArrowL")
	
	
	#right
	elif Input.is_action_pressed("Right"):
		velocity.x = +speed
		$icon.play("GoldWRight")
	elif Input.is_action_just_released("Right"):
		$icon.play("IdleRight")
		var facing_right = true
		if facing_right == true:
			if Input.is_action_just_pressed("LoadAmmo"):
				$icon.play("LoadArrowR")

SOLVED!!!

Basically, if you are only using 4 directions in your game then you should have this script layout (especially the _process). I recommend creating your own inputs.

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    elif Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    elif Input.is_action_pressed("ui_down"):
        velocity.y += 1
    elif Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    movement = velocity.normalized() * speed

func _process(delta):
    get_input()
    move_and_slide(movement)
:bust_in_silhouette: Reply From: rakkarage

Just a guess but maybe check for button up with not event.pressed so it does not happen 'before i release the button`

func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton and not event.pressed:
		print("ok")

Otherwise please provide more information or some example code please? You character only walks when button down? How does the turning work?

Added script

Amateur.game.dev. | 2020-09-27 14:02