¿How do i substract health when colliding with an object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Arielrandal

Sorry if i didnt explain myself good in the title, but i have an area called bullet with a mesh instance and collision shape, and i can shoot those, i made an little kinematic body called enemy with a collision shape and mesh instance with a var called health that when it reaches 0 or lower it queue_free(), the question is: ¿how can i make the “enemy” detect the bullet and then substract an amount of health? Thanks for any answer.

:bust_in_silhouette: Reply From: cascas

There are many ways to do this but this is the easiest way I can think of.
Add an area node to your bullet and then add the collision shapes of the bullet to that area node. The area node has a the body_entered signal, so once connected when the bullet collides with your player it calls a function with the kinematic body as parameter inside the bullet which you can use to decrease the players health.

:bust_in_silhouette: Reply From: wyattb

Another way would be to fire a signal in Area2D that the enemy Kinebody listens to:

Something like this:

in kinematic gd add this

onready var bullet: Area2D=$"../Area2D"
func _ready():
	var _err=bullet.connect("gotcha",self,"_gotcha")
	if _err:

func _gotcha(howbad):

In Area2d.gd under the body_entered signal add this

signal gotcha
func _on_Area2D_body_entered(body: Node) -> void:
	pass # Replace with function body.
:bust_in_silhouette: Reply From: theMX89

connect the area with its self with “body_entered(body:Node)”

then write something like this into the func:

var health -= 1 <------------- the health you wanna subtract