How do I play a death animation after a collision? (Brackey's Godot Beginner Tutorial)

Godot Version

4.22

Question

Hello,

I’m entirely new to Godot and following Brackey’s “How to make a Video Game - Godot Beginner Tutorial”.
How to make a Video Game - Godot Beginner Tutorial

Instead of having the player body fall through a platform, I want to insert a death animation which I have already inserted the sprite frames for in the AnimatedSprite2D child node under the Player scene. The idle, run and jump animations are accessed through the Player scene script, but the killzone that Brackey introduces is a different scene. I tried replacing the line of code that causes the player to fall through blocks when dying with:

body.get_node.(“AnimatedSprite2D”).play(“death”)

This doesn’t work the same way as the line of code used in the player scene script:

animated_sprite_2d.play(“idle”)

Otherwise, I’m not sure how I’d access the same function used in the Killzone scene in the Player scene with an if statement to trigger the death animation, or how to access the sprite animations from the Killzone scene.

I’m not sure how to go about this. Still very new to all this, so any help is really appreciated!
Thank you.

Killzone scene script:
Screenshot 2024-07-23 212152

Player scene script:
image
image

in the player scene script, add a function called

func die():
       animated_sprite_2d.play("death")

in the killzone script:

func on_body_entered(body):
      if body.has_method("die"):
             body.die()

Another solution would be to have an area2d attached to the player and detect when the killzone body enters that area2d.

1 Like

This made my player stuck in the air, unable to move! Do I need to put this in a specific place? I’m kinda new to coding.

so this die() function needs to be outside of your physics process loop, I had the same issue, but all you need to do is move the function to be above your process function

I also have the same issue but none of these solutions have worked. Anyone else that can help? Maybe by doing what is in Brackey’s Godot Beginner Tutorial and then going from there to creating death animation. Would appreciate if someone tried that.

I used this tutorial as well, and came across the same issue. In the killzone script, use the method that snipercup mentioned. In the player script though, I added a variable called “dying”, and had that set to false. For all animations on that script, I had them only run if “dying” is set to false. Then when the function die shown by snipercup is called, it sets “dying” to true, which allows a dying animation to run. Hope this helps people!

I ended up cheating and loading a death scene…

Blankglitch would you be able to provide the code you put in like snipercup did? I know what you mean but I have no idea how to execute that in code lol

You mean the code in the player script? Do I understand correctly that you want to use the boolean value dying in combination with the function I posted before?


# player script
#player.gd

var dying: bool = false

func die():
      dying = true

func update_animation():
       if dying == true:
              animated_sprite_2d.play("death")

Now when you call player.die() it will set the dying variable to true
The update_animation() function is just to give an idea about how to use the boolean.

If you share your script then I can give some more specific advice.

Thanks for the help @snipercup , but I’ve already managed to fix it using this guys video. https://www.youtube.com/watch?v=g-3yMb-0X8M
The death animation works and the level does restart, but I found an issue where if I jump on top of the enemy and die then my character will float and play the animation above ground. How would I fix this?
Heres a video of the issue: Imgur: The magic of the Internet

1 Like