"walking animation" doesnt play when moving character in godot

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

this is the player script :

extends KinematicBody

var velocity = Vector3()
var speed = 4
var AttackPoints = 2


enum State {
	IDLE,
	WALKING,
	ATTACKING
}

var state = State.IDLE

onready var anim_player = $AnimationPlayer

func _physics_process(delta):
	velocity = Vector3()

	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_up"):
		velocity.z -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.z += 1
		
	if state == State.ATTACKING:
		velocity = Vector3()

	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		move_and_slide(velocity)
		state = State.WALKING
	else:
		state = State.IDLE
		
	if Input.is_action_pressed("attack"):
		state = State.ATTACKING
		update_state()

func update_state():
	if state == State.IDLE:
		$AnimatedSprite3D.play("idle")
	elif state == State.WALKING:
		$AnimatedSprite3D.play("walking")
	elif state == State.ATTACKING:
		if Input.is_action_just_pressed("attack") && AttackPoints == 2:
			$AttackResetTimer.start()
			$AnimatedSprite3D.play("Attack_1")
			AttackPoints = AttackPoints - 1
		
		elif Input.is_action_just_pressed("attack") && AttackPoints == 1:
			$AttackResetTimer.start()
			$AnimatedSprite3D.play("Attack_2")
			AttackPoints = AttackPoints - 1
			
			
		
			
func _on_AnimatedSprite3D_animation_finished():
	if $AnimatedSprite3D.animation == "Attack_1":
		$AnimatedSprite3D.play("idle")
	elif $AnimatedSprite3D.animation == "Attack_2":
		$AnimatedSprite3D.play("idle")
	
		
func _on_AttackResetTimer_timeout():
	AttackPoints = 2

the "walking " animation doesnt play. Im assuming i need to ad a boolean like if_moving somewhere but ive been trying for ages and its not working any suggestions and help would be grateful thank you

Edited to fix code formatting…

jgodfrey | 2023-02-12 16:21

:bust_in_silhouette: Reply From: LeslieS

It doesn’t appear that you update after changing the state to walking.
It looks like update_state() is only called when you are attacking.