how does the bullet hit the enemy is the enemy take the damage and the bullet is gone?

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

I already have the bullet and enemy system, but not the damage function, this one from below and the bullet:

extends CharacterBody2D
var speed = 850
var direction = 1

func _ready():
velocity.x = speed * direction

func _physics_process(delta):
if is_on_wall():

func _on_visible_on_screen_notifier_2d_screen_exited():

this is from the enemy

extends CharacterBody2D

enum {

var state = WALK
@onready var anim = $AnimatedSprite2D
@onready var ray2D = $RayCast2D
var health = 3

var is_moving_left = true

const SPEED = 50.0
#const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
	velocity.y += gravity * delta
match state:
		anim.animation = "walk"
		velocity.x = SPEED if is_moving_left else -SPEED


func detect_turn_around():
if not ray2D.is_colliding() and is_on_floor():
	is_moving_left = !is_moving_left
	scale.x = -scale.x
:bust_in_silhouette: Reply From: Nickyroo

You would have in you’re enemy Script a Variable like “Health”, or “HP” (Hit Points)

Then you would have a Raycast or Collision or however you would want to fire the Bullet, Get the Enemy’s Collision. Upon Colliding, you would Obtaining that Enemy’s Script Through you’re Collision and its following path… And finally tweaking the Enemy’s Health

for Instance, if you’re collision is a Child of that Object containing the Script… You would type something like:

var HitEnemyScript = BulletRaycast.get_collider().get_parent()
HitEnemyScript.Health -= 8