Hello there! I am working on a player, and I am working on a health bar, though its not working. Can someone help me out for a way to make this work?
CODE:
extends CharacterBody2D
var health = MAX_HEALTH
var SPEED = 450.0
const JUMP_VELOCITY = -600.0
const MAX_HEALTH = 12
func _ready():
add_to_group(“Player”)
func damageone() → void:
health -= 1
if health <= 0:
get_tree().reload_current_scene()
func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("run"):
SPEED = 670
if Input.is_action_just_released("run"):
SPEED = 450
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 22)
move_and_slide()
func _on_hitbox_area_area_entered(area: Area2D) → void:
if area.is_in_group(“One Hurt”):
damageone()
if health < 0:
get_tree().reload_current_scene()
Well I’m trying to make my health go down by one when interacting with an enemies hitbox on your hitbox. Instead it just touches you a couple times and randomly resets the level, instead of the value being 0.
Ok so I’ve come up with a new system by giving the health (progress bar) on the player it’s own script. But it still didn’t work, how could I fix this?
CODE:
extends ProgressBar
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
max_value = 12
min_value = 0
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
if value == 0:
get_tree().reload_current_scene()
func _on_hitbox_area_area_entered(area: Area2D) -> void:
if area.is_in_group("One hurt"):
value -= 1