How to chain multiple input commands cleanly

Godot Version

godot-4

Question

func _process(_delta):

var direction = Input.get_axis("move_left", "move_right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
if Input.is_action_pressed("move_left") && isattacking == false && movey == false:
	movex = true
	velocity.x = -SPEED
	animator.play("walk");
	animator.flip_h = true
elif Input.is_action_just_released("move_left"):
	movex = false
if Input.is_action_pressed("move_right") && isattacking == false && movey == false: 
	movex = true
	velocity.x = SPEED
	animator.play("walk");
	animator.flip_h = false
elif Input.is_action_just_released("move_right"):
	movex = false
var Hight = Input.get_axis("down", "up")
if Hight:
	velocity.y = Hight * SPEED
else: 
	velocity.y = move_toward(velocity.y, 0, SPEED)
if Input.is_action_pressed("down") && isattacking == false && movex == false:
	movey = true
	velocity.y = SPEED
	animator.play("walk")
elif Input.is_action_just_released("down"):
	movey = false
if Input.is_action_pressed("up") && movex == false:
	movey = true
	velocity.y = -SPEED
	animator.play("walk")
elif Input.is_action_just_released("up"):
	movey = false
if movex == false && movey == false && isattacking == false:
	animator.play("idle")
pass

if Input.is_action_pressed(“swing_right”):
animator.play(“attack_1”);
animator.flip_h = false
isattacking = true;
$attackarea/CollisionShape2D.disabled = false;
if Input.is_action_pressed(“swing_left”):
animator.play(“attack_1”);
animator.flip_h = true
isattacking = true
$attackarea_2/CollisionShape2D.disabled = false;

if Input.is_action_pressed("super"):
	animator.play("attack_3")
	animator.flip_h = false
	isattacking = true
	$attackarea_3/CollisionShape2D.disabled = false;
	
if Input.is_action_just_pressed("Run"):
	SPEED = SPEED * 2.5

if Input.is_action_just_released("Run"):
	SPEED = SPEED / 2.5
	
pass

pass
move_and_slide()





pass # Replace with function body.

func start(pos):
position = pos
show()

pass # Replace with function body.

func _on_animated_sprite_2d_animation_finished():
if $PlayerSprite2D.animation == “attack_1”:
isattacking = false;
$attackarea/CollisionShape2D.disabled = true;
$attackarea_2/CollisionShape2D.disabled = true;
pass # Replace with function body.

pass # Replace with function body.

func _on_animated_sprite_2d_animation_finished3():
if $PlayerSprite2D.animation == “attack_3”:
isattacking = false;
$attackarea_3/CollisionShape2D.disabled = true;
pass # Replace with function bod

alright so this is the attacks and movement code wasd is movement and arrow keys are attacks the problem im having is if you press the d key and press the w key you move down and to the right but if you press the s key you move up and to the right same with the a key the other problem is if you attack and move the animation gets canceled but the attack area stays active how on earth do i fix this

It’s difficult to read the code without code formatting, could you try pasting it again but use the </> button to insert a block. It will look like this

```
type or paste code here
```

Then press ctrl+v to paste between the ticks ```


One thing I’m seeing is this code applies velocity in two separate if statements, one more conventional, then again with attack checks.

# conventional, very similar to template code
var direction = Input.get_axis("move_left", "move_right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

# attempting to handle attacks
if Input.is_action_pressed("move_left") && isattacking == false && movey == false:
	movex = true
	velocity.x = -SPEED
	animator.play("walk");
	animator.flip_h = true
elif Input.is_action_just_released("move_left"):
	movex = false

I would bet reducing this code would solve your issues, as attacks and movement are separate actions they will behave better when separated. But again with proper formatting it will be possible to tell.

im trying to use that button but it doesnt seem to be doing anything

and the reason there are two velocities is cause i want the character to move up down left and right so there is no jump but movement on the y and x axis

ive been showcasing my progress here if it helps you get a better idea of what im working on

You don’t have to use the button, you could copy these three ticks, or press the key (`~) three times, then paste

```
paste here
last three are optional
```

func _process(_delta):
	
	var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	if Input.is_action_pressed("move_left") && isattacking == false && movey == false:
		movex = true
		velocity.x = -SPEED
		animator.play("walk");
		animator.flip_h = true
	elif Input.is_action_just_released("move_left"):
		movex = false
	if Input.is_action_pressed("move_right") && isattacking == false && movey == false: 
		movex = true
		velocity.x = SPEED
		animator.play("walk");
		animator.flip_h = false
	elif Input.is_action_just_released("move_right"):
		movex = false
	var Hight = Input.get_axis("down", "up")
	if Hight:
		velocity.y = Hight * SPEED
	else: 
		velocity.y = move_toward(velocity.y, 0, SPEED)
	if Input.is_action_pressed("down") && isattacking == false && movex == false:
		movey = true
		velocity.y = SPEED
		animator.play("walk")
	elif Input.is_action_just_released("down"):
		movey = false
	if Input.is_action_pressed("up") && movex == false:
		movey = true
		velocity.y = -SPEED
		animator.play("walk")
	elif Input.is_action_just_released("up"):
		movey = false
	if movex == false && movey == false && isattacking == false:
		animator.play("idle")
	pass

so like this

	if Input.is_action_pressed("swing_right"):
			animator.play("attack_1");
			animator.flip_h = false
			isattacking = true;
			$attackarea/CollisionShape2D.disabled = false;
	if Input.is_action_pressed("swing_left"):
			animator.play("attack_1");
			animator.flip_h = true
			isattacking = true
			$attackarea_2/CollisionShape2D.disabled = false;
			
	if Input.is_action_pressed("super"):
		animator.play("attack_3")
		animator.flip_h = false
		isattacking = true
		$attackarea_3/CollisionShape2D.disabled = false;
		
	if Input.is_action_just_pressed("Run"):
		SPEED = SPEED * 2.5

	if Input.is_action_just_released("Run"):
		SPEED = SPEED / 2.5
		
	pass