Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Phantomist |
How do I make my player take damage?
Hey. I’m currently working on my finals and have run into an issue. In my 2D platformer I want my character to take damage from a beam to light. I’ve made the beam of light a Area2D Node, but I’m not quite sure how to make it deal damage to my player character.
Here’s what my player code looks like
extends KinematicBody2D
signal health_updated(health)
signal killed()
var velocity = Vector2(0,0)
const SPEED = 375
const GRAVITY = 30
const JUMPFORCE = -1000
export (float) var max_health = 100
onready var health = max_health setget _set_health
# warning-ignore:unused_argument
func _physics_process(delta):
if Input.is\_action\_pressed("right"):
velocity.x = SPEED
$[AnimatedSprite.play](https://AnimatedSprite.play)("Walk")
$AnimatedSprite.flip\_h = false
elif Input.is\_action\_pressed("left"):
velocity.x = -SPEED
$[AnimatedSprite.play](https://AnimatedSprite.play)("Walk")
$AnimatedSprite.flip\_h = true
else:
$[AnimatedSprite.play](https://AnimatedSprite.play)("Idle")
if not is\_on\_floor():
$[AnimatedSprite.play](https://AnimatedSprite.play)("Jump")
velocity.y = velocity.y + GRAVITY
if Input.is\_action\_just\_pressed("jump") and is\_on\_floor():
velocity.y = JUMPFORCE
velocity = move\_and\_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.2)
func damage(amount):
\_set\_health(health - amount)
$Timer.start()
func kill():\
pass
func _set_health(value):
var prev\_health = health
health + clamp(value, 0, max\_health)
if health != prev\_health:
emit\_signal("health\_updated", health)
if health == 0:
kill()
emit\_signal("killed")
If it’s a mess (which it probably is), it’s because I’ve used multiple Youtube tutorials for this project so far.
How do I make the Area2D Node deal damage?