How to walk and attack key logic animations

Godot Version

Godot v4.2.1.stable - Ubuntu 23.10 23.10 - X11 - Vulkan (Forward+) - integrated Intel(R) Graphics (ADL GT2) () - 12th Gen Intel(R) Core™ i7-1260P (16 Threads)

Question

Trying to figure out how to attack while walking animations. I walk holding down D key, and press the Right arrow key to attack. I would like it start playing the attack animation while still keeping down the D key and upon release of arrow key to go back to walk.

Is there a way to interrupt the held down D key and give priority to arrow key pressed?
For example do I need some flags to ignore the D key and is _is_action_just_pressed() the function to use?

func _process(delta):
	
	# attack/Slash right arrow key
	if Input.is_action_just_pressed("ui_right"):
		$Sprite2D.flip_h=true
		anim.play("attack")  # Slash animation
		horizontal_direction = 1
		velocity =  Vector2(speed * horizontal_direction, 0)
		move_and_slide()	
    
    # walk pressing D
	if Input.is_action_just_pressed("move_right"):
		$Sprite2D.flip_h=true
		anim.play("walk") # Walk animation
		horizontal_direction = 1
		velocity =  Vector2(speed * horizontal_direction, 0)
		move_and_slide()

Use a bool to check if he attacks:

var is_attacking = false

if Input.is_action_just_pressed(“move_right”):
if(is_attacking):
anim.play(“attack”) # Attack animation
elif(!is_attacking):
anim.play(“walk”) # Walk animation

Just a quick example. But this could be an option

1 Like

Your second if-condition that checks move_right will overwrite your animation change made in the ui_right.

Quickest way would be to use elif to check for move_right - or you could simply change the order of the if-conditions.

Later on, when/if you find your self with too many if/elif’s I advice you to check out states design pattern.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.