Idle and Walk animations overrides Attack animation

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

export var speed = 2.5
export var acceleration = 25
export var gravity = 35
export var jump_strength = 5
export var sensitivity = 0.1

onready var head = $Head
onready var hand = $AnimatedSprite
onready var interactRay = $Head/InteractRayCast
onready var light = $OmniLight
onready var sword = $Head/sword
onready var swordAnim = $SwordAnimationPlayer

var look_rot = Vector3.ZERO
var move_dir = Vector3.ZERO
var velocity = Vector3.ZERO
var max_angle = 0
var min_angle = 0
var haveLamp = null
var haveSword = null
var onHand = null

func _ready():
	light.visible = false
	sword.visible = false
	haveLamp = false
	haveSword = false
	onHand = null

func _physics_process(delta):
	head.rotation_degrees.x = look_rot.x
	rotation_degrees.y = look_rot.y
	if not is_on_floor():
		velocity.y -= gravity * delta
	elif Input.is_action_just_pressed("jump"):
		velocity.y = jump_strength
	move_dir = Vector3(
		Input.get_action_strength("right") - Input.get_action_strength("left")
		Input.get_action_strength("backward") - Input.get_action_strength("forward")
	).normalized().rotated(Vector3.UP, rotation.y)
	velocity.x = lerp(velocity.x, move_dir.x * speed, acceleration * delta)
	velocity.z = lerp(velocity.z, move_dir.z * speed, acceleration * delta)
	velocity = move_and_slide(velocity, Vector3.UP)

func _input(event):
	if event is InputEventMouseMotion:
		look_rot.y -= (event.relative.x * sensitivity)
		look_rot.x -= (event.relative.y * sensitivity)
		look_rot.x = clamp(look_rot.x, min_angle, max_angle)

func characterAnimations():
	if haveLamp == true and onHand == "lamp":
		hand.visible = true
		if Input.is_action_pressed("backward") or Input.is_action_pressed("forward") or Input.is_action_pressed("left") or Input.is_action_pressed("right"):"Walk")
		hand.visible = false
	if haveSword == true and onHand == "sword":
		sword.visible = true
		if Input.is_action_pressed("backward") or Input.is_action_pressed("forward") or Input.is_action_pressed("left") or Input.is_action_pressed("right"):
			if swordAnim.current_animation != "Attack-1":"Walk")
				if Input.is_action_pressed("forward"):
					swordAnim.playback_speed = -1
					swordAnim.playback_speed = 1
			if swordAnim.current_animation != "Attack-1":"Idle")
		sword.visible = false
	if haveSword == true and onHand == "sword":
		if Input.is_action_just_pressed("attack"):
			swordAnim.current_animation = "Attack-1"
	if Input.is_action_just_pressed("scroll"):
		if onHand == "sword":
			if haveLamp == true:
				onHand = "lamp"
		elif onHand == "lamp":
			if haveSword == true:
				onHand = "sword"

func _on_Lamp_lampTaken():
	light.visible = true
	haveLamp = true
	onHand = "lamp"

func _on_Sword_pickedUpSword():
	haveSword = true
	onHand = "sword"
	sword.visible = true
:bust_in_silhouette: Reply From: Gluon

I would suggest either look into a state machine (which given the complexity of your code around animations here would probably be the best idea) or else just use a boolean guard.

If you link the attack button to a function which changes a boolean to true to say that the character is attacking and then have an if check so that the code which starts the walking animation only works when that boolean is false you wont get the override happening. After that you can link the animation finished signal to a function to change the boolean to false again so the walking animation will work again.