Godot Version
Godot 4.5.1
Question
So I can’t figure it out why this is happening, I’ve been following a tutorial on yt and when it came to do the progress bar as health for the player I’ve encountered this problem.
Below is my player script where I account for damage and updating the progress bar, also a photo of the progress bar properties. When I click to shows progress bar % it seems to be updating the value correctly, but not vissually
I tried everything and nothing seems to work, also I’m new to coding and would appreciate any advice on the code. Thanks
Player script:
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var attack: Sprite2D = $Attack
@onready var animation_player: AnimationPlayer = $AnimationPlayer
var last_facing: String = "down" # "up", "down", "left", "right"
var hp = 100.0
signal hp_zero
func _physics_process(delta: float) -> void:
#get input for movement
var direction = Input.get_vector("move_left","move_right","move_up","move_down")
velocity = direction * 200
move_and_slide()
#attack
if Input.is_action_just_pressed("attack"):
_do_attack()
#play animations for each side that it moves
if velocity.length() > 0.0:
if Input.is_action_pressed("move_down"):
animated_sprite_2d.play("front_walk")
last_facing = "down"
if Input.is_action_pressed("move_up"):
animated_sprite_2d.play("back_walk")
last_facing = "up"
if Input.is_action_pressed("move_left"):
animated_sprite_2d.flip_h = true
animated_sprite_2d.play("side_walk")
last_facing = "left"
if Input.is_action_pressed("move_right"):
animated_sprite_2d.flip_h = false
animated_sprite_2d.play("side_walk")
last_facing = "right"
#if player is not moving, play idle animations
else:
match last_facing:
"down":
animated_sprite_2d.play("front_idle")
"up":
animated_sprite_2d.play("back_idle")
"left":
animated_sprite_2d.play("side_idle")
"right":
animated_sprite_2d.flip_h = false
animated_sprite_2d.play("side_idle")
const DMG_RATE = 5.0
var overlapping_enem = %MyHurtBox.get_overlapping_bodies()
if overlapping_enem.size() > 0 :
hp -= DMG_RATE * overlapping_enem.size() * delta
%ProgressBar.value = hp
if hp <= 0.0 :
animated_sprite_2d.play("death")
await animated_sprite_2d.animation_finished
hp_zero.emit()
func _do_attack():
# Hide the regular sprite and show the attack sprite
animated_sprite_2d.hide()
attack.show()
if velocity.length() > 0.0:
match last_facing:
"down":
animation_player.play("front_attack")
"up":
animation_player.play("back_attack")
"left":
animation_player.play("left_attack")
"right":
animation_player.play("right_attack")
else:
animation_player.play("front_attack")
await animation_player.animation_finished
attack.hide()
animated_sprite_2d.show()

