I want to make the boss in my game attack with a laser, but I don’t know how to make the player take damage. Please help.
var laser_inatack_range = false
if body.has_method("laser"):
laser_inatack_range = true```
func laser_attack():
if laser_inatack_range == true:
health = health -20
print(health)
emit_signal("healthChanged",health)
damage_sound.play()
$Camera2D.shake(0.2)
The best way depends on how you are drawing the laser. Is it a Sprite2D that you are rotating? Is it a Line2D whose points are updated for each rotation?
i use area2d with sprite2d and animate in animation player
in the script that i made area 2d had to touch the player and cause damage to him.but something went wrong:(
When asking for help, always post your code (enclose with three backticks (```) before and after the code) and relevant error messages. People don’t know anything about your project, so the best we can do at the moment is guess at what the problem is. Which tends not to be fruitful.
Are you using get_overlapping_bodies in the laser? With that method you can find out if the laser overlaps anything inheriting from PhysicsBody2D, which I imagine your player character does. If you do use that method, does the laser beam’s Area2D happen to be very thin?
Every time some object that inherits from PhysicsBody2D makes contact with the laser, _on_body_entered is called. You can edit _on_body_entered to do anything you please with that body.
If this is your player’s script:
class_name Player extends CharacterBody2D
var health : int = 100
...
func take_damage(amount : int)->void:
health -= amount
You could change the content of _on_body_entered to:
func _on_body_entered(body : Node2D)->void:
if body is Player:
var player = body as Player
player.take_damage(10)