+1 vote

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.

Godot version 3.3.2
in Engine by (37 points)

3 Answers

+2 votes

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.

by (252 points)
+2 votes

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:
        print("Error:",_err)

func _gotcha(howbad):
    print(howbad)

In Area2d.gd under the body_entered signal add this

signal gotcha
func _on_Area2D_body_entered(body: Node) -> void:
    emit_signal("gotcha","deadcenter")
    pass # Replace with function body.
by (719 points)
+1 vote

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

by (179 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.