• 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 Player 32

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

4 ways to fix bluetooth connectivity on Windows 11
Tips

4 ways to fix bluetooth connectivity on Windows 11

August 8, 2025
How to know the computer is tracked and processed by Keylogger
Tips

How to know the computer is tracked and processed by Keylogger

August 7, 2025
Opal: Create applications who do not need to write code
Tips

Opal: Create applications who do not need to write code

August 3, 2025
How to activate a new Start menu on Windows 11
Tips

How to activate a new Start menu on Windows 11

July 29, 2025
Intellgpt: AI tool for osint and data science
Tips

Intellgpt: AI tool for osint and data science

July 28, 2025
How to create Google Ai Pro 12 months free with Indian student account
Tips

How to create Google Ai Pro 12 months free with Indian student account

July 27, 2025
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
Inline Feedbacks
View all comments

Recent News

How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
Instructions on how to fix Screen Time Limited Reached on RoBlox

Instructions on how to fix Screen Time Limited Reached on RoBlox

August 13, 2025
How to install GPT-suns on who do not need the Internet

How to install GPT-suns on who do not need the Internet

August 12, 2025
How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
Instructions on how to fix Screen Time Limited Reached on RoBlox

Instructions on how to fix Screen Time Limited Reached on RoBlox

August 13, 2025
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

How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

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

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

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