Animation not playing while others can?

Godot Version

Godot version4.5.1

Question

its my first try on makeing a game after some learning and i cant seem to work this out. i have set my damage button for now to see how my animation will look. But when i click still "Idle" animation plays. I can also play other animations.

here is the entire code of the character. note that is a mix of chat gpt and youtube help. Thank you

extends CharacterBody2D
class_name Player

@export var HEALTH : float = 100
@export var speed : float = 80.0
@export var sprint_mult : float = 1.5
@export var jump_vel : float = -300.0
@export var gravity : float = 900.0
@export var coyote_sec : float = 0.12
@export var jump_buffer_sec: float = 0.08
@export var accel : float = 0.18
@export var friction : float = 0.18

var _coyote_timer : float = 0.0
var _jump_buffer : float = 0.0
var _facing := 1.0
var can_move := true

@onready var anim : AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) → void:

—-- horizontal + sprint (ONLY place we set velocity.x) ------

var input := Input.get_axis(“move left”, “move right”)
var cur_speed := speed * (sprint_mult if Input.is_action_pressed(“runnig”) else 1.0)
var accel_rate := accel if input != 0.0 else friction
velocity.x = lerp(velocity.x, input * cur_speed, accel_rate)

# —-- gravity --------------------------------------------------
if not is_on_floor():
	velocity.y += gravity * delta
	_coyote_timer = max(_coyote_timer - delta, 0.0)
else:
	_coyote_timer = coyote_sec

# —-- jump / coyote / buffer -----------------------------------
_jump_buffer = max(_jump_buffer - delta, 0.0)
if Input.is_action_just_pressed("move up"):
	_jump_buffer = jump_buffer_sec
if _jump_buffer > 0.0 and (_coyote_timer > 0.0 or is_on_floor()):
	_jump_buffer = 0.0
	_coyote_timer = 0.0
	velocity.y = jump_vel
if Input.is_action_just_released("move up") and velocity.y < 0.0:
	velocity.y *= 0.5

move_and_slide()


if abs(velocity.x) > 10.0:
	_facing = sign(velocity.x)
	$AnimatedSprite2D.flip_h = _facing < 0

# ---- animation ----

if Input.is_action_pressed("damage"):
	anim.play("hit")
	velocity.x = 0.0
	if Input.is_action_pressed("attacking"):
		anim.play("attack")  
		velocity.x = 0.0

if Input.is_action_pressed("attacking"):
	anim.play("attack")  
	velocity.x = 0.0

elif not is_on_floor():
	anim.play("idle")
elif abs(velocity.x) > 10.0:
	anim.play("walking")
else:
	anim.play("idle")

It’s a bit hard to understand from your message on what you need and what is wrong..

But did you connect your Button’s button_pressed signal to a function that can play the animation you need?

You have an if-elif-elif-else-statement at the end, that will always play one of the “attack”, “idle” or “walking” animations. This immediately overwrites the “hit” animation.

In the current state it would probably be enough to integrate the “hit” animation into that same if-statement (instead of using a separate one):

if Input.is_action_pressed("attacking"):
	anim.play("attack")  
	velocity.x = 0.0

elif Input.is_action_pressed("damage"):
	anim.play("hit")
	velocity.x = 0.0

elif not is_on_floor():
	anim.play("idle")
elif abs(velocity.x) > 10.0:
	anim.play("walking")
else:
	anim.play("idle")

Later, when the animation will be caused by taking damage or something, you will need some logic to prevent other animations for a certain time. This could be done by checking if the “hit” animation is currently playing (if it isn’t looping) or by using a timer.

1 Like