• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

[Tạo 2D Platformer Game với Godot] Part 13: Destroy the Player

AnonyViet by AnonyViet
January 27, 2023
in Tips
0

In this part, I will guide you to destroy the Player when the enemy attacks and the Player will run out of health.

Join the channel Telegram of the AnonyViet 👉 Link 👈

Kill the player

This part is similar to the previous part, but in this part, I will add hitbox in enemies and hurtbox in Player.

Hitbox

Add

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 18

You add area2D to your enemy Scene and rename it Hitbox.[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 19

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 20

And set CollisionShape2D and place it where the attack frame is in the attack animation and remember to turn it off.

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 21 [Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 22 [Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 23

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 24

Then also connect the signal as in the previous section.

Script

extends KinematicBody2D

# Di chuyn
var tocdo = 80
var trongluc = 10
var chuyendong = Vector2()
var huong_dichuyen = 1
# Animation
onready var animation = $AnimatedSprite
var dangtancong = false
# Chi so
var mau = 20
var satthuong = 5

func _physics_process(delta):
    if dangtancong == false:
        chuyendong.x = tocdo * huong_dichuyen
        chuyendong.y += trongluc
        chuyendong = move_and_slide(chuyendong,Vector2.UP)
        animation.play("dichuyen")
        
        if $RayCast2D.is_colliding() == false:
            huong_dichuyen = huong_dichuyen * -1
            scale.x = scale.y * huong_dichuyen
        
        if is_on_wall() and $XacDinhPlayer.is_colliding() == false:
            huong_dichuyen = huong_dichuyen * -1
            scale.x = scale.y * huong_dichuyen
    
    var coll = $XacDinhPlayer.get_collider()
    if coll != null and coll.name == "Player" and dangtancong == false:
        dangtancong = true
        animation.play("tancong")
    if mau <= 0:
        animation.play("chet")


func _on_AnimatedSprite_animation_finished():
    if animation.animation == "tancong":
        dangtancong = false
    if animation.animation == "chet":
        queue_free()


func _on_Hurtbox_area_entered(area):
    if area.get_parent().name == "Player" and area.get_parent().dangtancong == true:
        mau -= area.get_parent().satthuong



func _on_AnimatedSprite_frame_changed():
    if animation.animation == "tancong":
        if animation.frame == 3:
            $Hitbox/CollisionShape2D.disabled = false
        else:
            $Hitbox/CollisionShape2D.disabled = true

The code part, it also adds the same as in the previous part.

I add the satthuong variable, then check if it is an attack frame, turn on the hitbox and vice versa.

Paragraph area.get_parent().dangtancong == true is what I use to determine that the hitbox that collides with the enemy’s hurtbox is not duplicated by itself.

Hurtbox

Add

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 25

Create area2D and rename it as Hurtbox in Scene Player

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 26Set the hurtbox’s collisionshape to be the same as the original node’s Collisionshape.

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 27 [Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 28 [Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 29 [Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 30

Done then add the signal as before.

Script

extends KinematicBody2D

var tocdo = 200
var trongluc = 10
var chuyendong = Vector2()
var huong_dichuyen = 1
onready var animation = $AnimatedSprite
var dangtancong = false

var mau = 20
var satthuong = 10

func _physics_process(delta):
    
    if !dangtancong:
        huong_dichuyen = Input.get_axis("ui_left","ui_right")
        if huong_dichuyen != 0:
            chuyendong.x = lerp(chuyendong.x,huong_dichuyen * tocdo,0.5)
            animation.play("chay")
            scale.x = scale.y * huong_dichuyen
        else:
            chuyendong.x = lerp(chuyendong.x,0,0.5)
            animation.play("dungyen")

        if is_on_floor():
            if Input.is_action_just_pressed("ui_accept") :
                chuyendong.y = -250
        else:
            animation.play("nhay")
    if Input.is_key_pressed(KEY_J) and is_on_floor():
        dangtancong = true
        chuyendong.x = 0
        animation.play("tancong")
    
    chuyendong.y += trongluc 
    chuyendong.normalized()
    chuyendong = move_and_slide(chuyendong,Vector2.UP)
    
    if mau <= 0:
        set_physics_process(false)
        animation.play("chet")
        $CollisionShape2D.disabled = true
func _on_AnimatedSprite_animation_finished():
    if animation.animation == "tancong":
        dangtancong = false
func _on_AnimatedSprite_frame_changed():
    if animation.animation == "tancong":
        if animation.frame == 3:
            $Hitbox/CollisionShape2D.disabled = false
        else:
            $Hitbox/CollisionShape2D.disabled = true 
        



func _on_Hurtbox_area_entered(area):
    if area.name == "Hitbox" and area.get_parent().dangtancong == true:
        mau -= area.get_parent().satthuong
    pass # Replace with function body.

The code, I think through the previous part and a little bit above that you can understand these codes.

Here, most are the same, but there is only 1 place that you should pay attention to.

Notice the line if mau <= 0: This will be the important line in this Section and in Player. Below that, I will not run the queue_free() function because most Platformer games when the Player dies, they will return to a saved point or return to the menu without deleting anything.

Current set_physics_process(false) is that I disable the physics_process function.

$CollisionShape2D.disabled = true is that I turn off the Player’s CollisionShape2D because when the Player dies without turning it off, it will still determine that the player is alive and continue to attack.

[Tạo 2D Platformer Game với Godot]  Part 13: Destroy Player 31

Then you go to AnimatedSprite -> animation dies and turn off Loop otherwise when running dead animation it will be repeated.[Tạo 2D Platformer Game với Godot] Part 13: Destroy the Player

This is the final result.

summary

That’s it, the part of destroying enemies and characters is done! See you in the next part.

The article achieved: 5/5 – (100 votes)

Tags: DestroyGameGodotPartPlatformerPlayertạovới
Previous Post

12 Cách để Hacker có thể Hack điện thoại của bạn

Next Post

Instructions for registering Domain .ooo for free

AnonyViet

AnonyViet

Related Posts

Windows 7/8.1/10 users will be upgraded to Windows 11 for free
Tips

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving ChatGPT Plus for 1 month for free in 2026
Tips

Instructions for receiving ChatGPT Plus for 1 month for free in 2026

July 28, 2026
Some reasons I don’t like Windows 11
Tips

Some reasons I don’t like Windows 11

July 27, 2026
Instructions on how to get free Facebook white ticks
Tips

Instructions on how to get free Facebook white ticks

July 27, 2026
Collection of Windows 11 wallpapers for computers and phones
Tips

Collection of Windows 11 wallpapers for computers and phones

July 26, 2026
Don’t install leaked Windows 11
Tips

Don’t install leaked Windows 11

July 25, 2026
Next Post
Instructions for registering Domain .ooo for free

Instructions for registering Domain .ooo for free

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted

Recent News

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving the free Certified LLM Security Expert certificate

Instructions for receiving the free Certified LLM Security Expert certificate

July 30, 2026
MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

July 29, 2026
What is quantum resistant cryptography? Why must the Internet change?

What is quantum resistant cryptography? Why must the Internet change?

July 29, 2026
Windows 7/8.1/10 users will be upgraded to Windows 11 for free

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving the free Certified LLM Security Expert certificate

Instructions for receiving the free Certified LLM Security Expert certificate

July 30, 2026
MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

MSI laptops are not just for gamers: Which line is suitable for students, office workers and creative people?

July 29, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

Windows 7/8.1/10 users will be upgraded to Windows 11 for free

July 30, 2026
Instructions for receiving the free Certified LLM Security Expert certificate

Instructions for receiving the free Certified LLM Security Expert certificate

July 30, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply