Problem with walk script

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

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?


	if Input.is_action_pressed("Back"):
		velocity.y = -speed
		#$PlayerSprite.flip_h = false
	elif Input.is_action_just_released("Back"):
		var facing_back = true
		if facing_back == true:
			if Input.is_action_just_pressed("LoadAmmo"):
			if Input.is_action_just_pressed("SwordSwing"):
	elif Input.is_action_pressed("Forward"):
		velocity.y = speed
	elif Input.is_action_just_released("Forward"):
		var facing_forward = true
		if facing_forward == true:
			if Input.is_action_just_pressed("LoadAmmo"):
	elif Input.is_action_pressed("Left"):
		velocity.x = -speed
	elif Input.is_action_just_released("Left"):
		var facing_left = true
		if facing_left == true:
			if Input.is_action_just_pressed("LoadAmmo"):
	elif Input.is_action_pressed("Right"):
		velocity.x = +speed
	elif Input.is_action_just_released("Right"):
		var facing_right = true
		if facing_right == true:
			if Input.is_action_just_pressed("LoadAmmo"):


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):
: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:

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

Added script | 2020-09-27 14:02