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
You add area2D to your enemy Scene and rename it Hitbox.
And set CollisionShape2D and place it where the attack frame is in the attack animation and remember to turn it off.
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
Create area2D and rename it as Hurtbox in Scene Player
Set the hurtbox’s collisionshape to be the same as the original node’s Collisionshape.
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.
Then you go to AnimatedSprite -> animation dies and turn off Loop otherwise when running dead animation it will be repeated.
This is the final result.
summary
That’s it, the part of destroying enemies and characters is done! See you in the next part.