Problem Playing "hit" Animation?

Godot Version

4.2.2

Question

Hello all,

I am having a problem that I am hoping that some of you might be able to help with. It is a problem that I am completely out of my depth with. It is adding an animation to the Brackey’s Godot 4 tutorial.

What I have is a 2D platformer where if you jump onto a spike and later get hit by an enemy then the player game loop _physics_process plays an animation, but I only want it to play the animations once, I made the animation to not loop:

The spike scene initiates the contact and sends the variable onSpike to the player with a true value.

That works. What I cannot get at all is how to play the animation and how to only make it play once. The code I have used is this:

#-- on spike----------------------------
if(onSpike == true):
	hero_sprite.play("hit");
	if (hero_sprite.animation=="hit" and hero_sprite.is_playing() ):
		return;
	else:
		onSpike = false;
		print("... onSpike over");
#------------------------------------------

I can hear you laughing from here :stuck_out_tongue:. There is a definite logic error here, but, I cannot see it. What I see is:

  1. if onSpike equals true
  2. play the sprite animation “hit”
  3. if that animation is playing - don’t do nuthing, return, let it finish
  4. else its over set onspike to false over and done with
  5. onSpike is false continue on nothing to see here, move along

but, the hit animation continues ad infinitum and caught in an endless loop no doubt caused by the return and never drops down to: onspike=false. Needless to say I need some help. As always any help is greatly appreciated.

Thankyou and Regards.

why do you need it to be in _physics_process function that need to check every frame?

yes, this is correct, then just play “hit” animation when the player received this signal. and you can even decrease health and give invulnerability for x seconds, depending how you code it. the invulnerability timer cooldown could using the delta from _physics_process, but if it’s to just play an animation and decrease a health, you dont need to make it like a state so that you let it handle in physics_process

1 Like

I already have coded it to take off the health, the hit animation state, which I was absolutely dreading due to a lack of knowledge, was the next stage.

As for playing the animation, any animation, I thought all that needed to be handled by the physics_process :hushed:. I will have to rejig my code to take it out and see what happens. Will take some time, so first thing tomorrow.

Thankyou for replying.

dont need, you can even play it on tween tween_callback, just need to call the right animation track name

1 Like

Yep, it worked you are quickly becoming the GOAT of the forum.

Thankyou so much for all your help!

1 Like

that’s quite a compliment, i’m still learning too

1 Like

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