Problems with multiple animations working with the player

Godot Version

godot4

Question

I am having some problems with animating the player it has two animations walking animation and a digging animation yet when the player presses right-click to dig the animation only plays the first frame, and even weirder it will play the whole thing but only if the player holds down the walking button. Has anybody encountered a problem like that I’ll list my code below to try and help as best I can. Let me know if you need anything else, my apologies animation isn’t my strong suit.

extends CharacterBody2D

var speed = 75
var currentLocation = Vector2()

@onready var gold_in_my_bag = $InventoryUI/ColorRect/Label3

@export var stream: AudioStream

@onready var animated_sprite = $AnimatedSprite2D
@onready var player_canvas_layer = $JustPickedUp
@onready var inventory_ui = $InventoryUI

@onready var tilemap = $"../TileMap" 

@onready var items = $"../Items"

var groundlayer = 0
var collisionlayer = 1

var tilepos = null

var can_dig_custom_data = "can_dig"

var isdigging = false
var ismoving = false


func _ready():
	global.set_player_reference(self)
	player_canvas_layer.visible = false

func get_input():
	var input_direction = Vector2()
	if true == true:
		if Input.is_action_pressed("ui_left"):
			input_direction.x -= 1
		if Input.is_action_pressed("ui_right"):
			input_direction.x += 1
		if Input.is_action_pressed("ui_up"):
			input_direction.y -= 1
		if Input.is_action_pressed("ui_down"):
			input_direction.y += 1
		velocity = input_direction.normalized() * speed
	else:
		pass
	if Input.is_action_just_pressed("dig"):
		isdigging = true
		
		var mousepos = get_global_mouse_position()
		
		tilepos = tilemap.local_to_map(mousepos)
		
		var source_id = 0
		
		var Atlas : Vector2i = Vector2i(6, 1)
		
		var tileData : TileData = tilemap.get_cell_tile_data(groundlayer, tilepos)
		
		if tileData:
			var can_dig = tileData.get_custom_data(can_dig_custom_data)
			if can_dig:
				spawn_item(global.spawnableitems[randi() % global.spawnableitems.size()], mousepos)
				tilemap.set_cell(groundlayer, tilepos, source_id, Atlas)
				animated_sprite.play("Digging")
			else:
				print("can't dig here")
		else:
			print("no tile data")
	isdigging = false
	

func _physics_process(delta):
	get_input()
	move_and_slide()
	update_animations2()
	gold_in_my_bag.text = str(global.gold)
	
func update_animations2():
	var lastanimation = ""
	var digging = false
	
	if velocity.length() > 0:  # Check if the character is moving
		if Input.is_action_pressed("ui_up"):
			if Input.is_action_pressed("ui_left"):
				animated_sprite.play("Up_Left")  # Play the diagonal up-left animation
				lastanimation = "Idle_Up_Left"
			elif Input.is_action_pressed("ui_right"):
				animated_sprite.play("Up_Right")  # Play the diagonal up-right animation
				lastanimation = "Idle_Up_Right"
		elif Input.is_action_pressed("ui_down"):
			if Input.is_action_pressed("ui_left"):
				animated_sprite.play("Down_Left")  # Play the diagonal down-left animation
				lastanimation = "Idle_Down_Left"
			elif Input.is_action_pressed("ui_right"):
				animated_sprite.play("Down_Right")  # Play the diagonal down-right animation
				lastanimation = "Idle_Down_Right"
	else:
		animated_sprite.stop()
		animated_sprite.play(lastanimation)  # Stop the animation if the character is not moving


func _input(event):
	if event.is_action_pressed("ui_inventory"):
		inventory_ui.visible = !inventory_ui.visible
		AudioManager.play_sound(stream)



func spawn_item(data, pos):
	var item_scene = load("res://Scenes/Inventory_Item.tscn")
	var item_instance = item_scene.instantiate()
	item_instance.initiate_items(data["item_type"], data["item_name"], data["item_rarity"], data["item_worth"], data["item_texture"])
	item_instance.global_position = pos
	items.add_child(item_instance)

It looks to me that when the player stops walking velocity == 0.
When velocity == 0 this code fires:

if velocity.length() > 0:   
 ...
else:
		animated_sprite.stop()
		animated_sprite.play(lastanimation)

So long as the player is walking the velocity > 0 and the else clause doesn’t fire.

That helped solve the problem by putting an elif statement before the else I was able to allow the digging animation to play without getting stopped. Thanks so much for the help man. Have an amazing rest of your day! :slight_smile:

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