Idle and Walk animations overrides Sword Attack animation Godot 4.0

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

Not sure where or what should I add to make the animation play fully without being interrupted by the idle animation. Can someone show me an example where in my code I should fix this?

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down",)
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)
	
if input_dir == Vector2.ZERO:
	$AnimationTree.get("parameters/playback").travel("Idle")
	
else:
	$AnimationTree.get("parameters/playback").travel("Walk")
	$AnimationTree.set("parameters/Idle/blend_position", input_dir)
	$AnimationTree.set("parameters/Walk/blend_position", input_dir)
	
if Input.is_action_just_pressed("ui_sword"):
	$AnimationTree.get("parameters/playback").travel("Sword")
	$AnimationTree.set("parameters/Sword/blend_position", input_dir)
	
move_and_slide()
:bust_in_silhouette: Reply From: exuin

check if you’re playing the sword animation before you play the idle animation

you can set up a state machine for this