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

In this part, I will show you how to kill enemies when players attack. And in the next part, I will show you how to let the enemy destroy the Player.

Destroy the enemy

About first, I will have to create an Area2D in the Scene Player and this Area2D will always be off and only on when attacking. I created one more called hurtbox in the enemy Scene and if the Player’s hitbox is turned on and collides with the enemy’s hurtbox, then the Player has hit the enemy and then the enemy’s health will be deducted and < 0 it will disappear.

Hitbox

Add

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

I will create 1 more Area2D in Scene player and rename it Hitbox and add CollisionShape2D

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 52

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 53

Then adjust the Shape to be equal to where it was in the attack animation.

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 54[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 55[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 56

Then I will connect the frame change signal to the script. I’m connecting this here because I want to know if I’m in the attack animation and it reaches the frame where the hitbox needs to be turned on, I will turn it on.

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 57

Frame 3 is the frame where I want to enable hitbox.

Script

extends KinematicBody2D

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

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)


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 
        


Above is the full code.

I declare a new variable as: var satthuong = 10 Its effect, see the name.

In the function frame change the paragraph if animation.animation == "tancong": I check if it’s an attack animation and if animation.frame == 3:is if frame is equal to 3 I will turn on hitbox and vice versa.

Hurtbox

Add

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 58

You add the Area2D and rename it as Hurtbox + them CollisionShape2D

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 59

Regarding CollisionShape2D, it is equal to KinematicBody’s ColllisionShape2D.

[Tạo 2D Platformer Game với Godot]  Part 12: Killing enemies 60[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 61[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 62

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 63

Then connect the signal for it, here I connect the signal area_entered and not the body entered because Hitbox it is area2d, not the body.

And connect to check if hitbox comes in then do something….

Script

extends KinematicBody2D


var tocdo = 80
var trongluc = 10
var chuyendong = Vector2()
var huong_dichuyen = 1
onready var animation = $AnimatedSprite
var dangtancong = false
var mau = 20
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":
		mau -= area.get_parent().satthuong


Above will be the full code of this part.

I created a new variable named quick and this is the enemy’s blood variable.

Current if area.get_parent().name == "Player": of area_entered is that I will check the parent name of area2D is Player, then quickly -= Player’s satthuong.

And in the Physics function I will check if the health is <= 0 then run the animation dead, and in the animation finish function I will check if the animation ends and die -> queue_free() (this is the function that helps you remove the node from the tree ).

[Tạo 2D Platformer Game với Godot]  Part 12: Destroy the Enemy 64

This is the final result.

Leave a Reply