My Health Bar isn't working

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

Below is the code for my TextureRect which is a child of the Enemy. Whenever a body enters the hurt box, my hitpoints variable inconsistently switches between values 50, -50, 100 and 0 causing the Enemy to be remove from the scene tree at random

I think something is wrong with the logic or maybe there an easier way to make a Health Bar

extends TextureRect

func _ready():
  $TextureProgress.value = 100

func set_percent_value_int(value):
  $TextureProgress.value = value


extends KinematicBody2D        #The Enemy

export(int) var hitpoints = 100
var max_hitpoints = 100


func _on_Hurtbox_body_entered(body):

 hitpoints -= 50
 $TextureRect.set_percent_value_int(float(hitpoints)/max_hitpoints * 100)
  
 if hitpoints <= 0:
    queue_free()

 print (hitpoints)
:bust_in_silhouette: Reply From: steffan99

Maybe its just the collision mask of your hurtbox. Make sure its only able to collide with one Collision Layer

the mask of the hurtbox and the mask of the projectile that hits them are the same

javrocks | 2021-10-16 19:40

Try turning off Hurtbox collision layer and just turn on the collision mask. Other physicsbodies might be triggering Hurtbox collision signal.

steffan99 | 2021-10-16 20:51

I turned it off but it still isn’t working

For the Enemy hurtbox should monitoring and monitorable be set to true

And should the projectile have its own Area 2D because i just have a collision shape attached to a Kinematicbody

javrocks | 2021-10-16 21:33