- Home /
My HealthBar is not regenerating when i'm pickingup potion
So basically i've got HP bar on my hero that decrease health when he collides with enemy but when i'm trying to pick up my heal potion it's heals me in numbers that i got in Component but the HP bar slider is not moving. Can some1 pls help i stuck with this problem 4 days already
This is my Player script:
public class KilluaControl : MonoBehaviour
{
public float KilluaHealth;
public float KilluaMaxHealth = 1000;
public KilluaHealthBar HealthBar;
private Rigidbody2D rb;
private ManaHandler MB;
private Animator anim;
public float moveSpeed;
private bool attack;
private bool kick;
private bool shoot;
float driX = 0f;
private bool facingRight = true;
private Vector3 localScale;
private void Start (){
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
localScale = transform.localScale;
moveSpeed = 15f;
MB = GetComponent<ManaHandler>();
KilluaHealth = KilluaMaxHealth;
HealthBar.SetHealth(KilluaHealth, KilluaMaxHealth);
}
private void Update ()
{
{
driX = Input.GetAxis("Horizontal");
transform.position += new Vector3(driX, 0, 0) * Time.deltaTime * moveSpeed;
if (!Mathf.Approximately(0, driX))
transform.rotation = driX < 0 ? Quaternion.Euler(0,180, 0) : Quaternion.identity;
if(Input.GetButtonDown("Jump") && rb.velocity.y == 0)
rb.AddForce(Vector2.up * 1000f);
if(Mathf.Abs(driX) > 0 && rb.velocity.y == 0)
anim.SetBool("isRunning", true);
else
anim.SetBool("isRunning", false);
if (rb.velocity.y == 0)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", false);
}
if (rb.velocity.y > 0)
anim.SetBool("isJumping", true);
if (rb.velocity.y < 0)
{
anim.SetBool("isJumping", false);
anim.SetBool("isFalling", true);
}
}
{
HanldeInput();
}
}
private void HandleAttacks()
{
if(attack && !this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attacking"))
{
anim.SetTrigger("isAttacking1");
rb.velocity = Vector2.zero;
}
}
private void HandleKicks()
{
if(kick && !this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Kicking"))
{
anim.SetTrigger("isKicking");
rb.velocity = Vector2.zero;
}
}
private void HandleShoots()
{
if(shoot && !this.anim.GetCurrentAnimatorStateInfo(0).IsTag("Shooting"))
{
anim.SetTrigger("isShooting");
rb.velocity = Vector2.zero;
}
}
private void HanldeInput()
{
if (Input.GetMouseButtonDown(0))
{
attack = true;
}
if (Input.GetMouseButtonDown(4))
{
kick = true;
}
if (Input.GetMouseButtonDown(1))
{
//shoot = false;
shoot = true;
}
}
private void FixedUpdate()
{
HandleAttacks();
HandleKicks();
HandleShoots();
ResetValues();
}
public void AddLife()
{
if(KilluaHealth < KilluaMaxHealth && KilluaHealth <=0 == false)
{
HealthBar.gameObject.SetActive(true);
KilluaHealth += 50;
}
}
private void OnTriggerEnter2D(Collider2D coll)
{
if (coll.CompareTag("Chocolate"))
{
AddLife();
Destroy(coll.gameObject);
}
}
public void TakeHit(float damage)
{
KilluaHealth -= damage;
HealthBar.SetHealth(KilluaHealth, KilluaMaxHealth);
if(KilluaHealth <= 0)
{
HealthBar.gameObject.SetActive(false);
anim.SetTrigger("Death");
moveSpeed = 0;
GetComponent<AdvancedMovment>().enabled = false;
}
}
private void ResetValues()
{
attack = false;
kick = false;
shoot = false;
}
}
and this is my health bar script:
public class KilluaHealthBar : MonoBehaviour
{
public Slider Slider;
public Color Low;
public Color High;
public Vector3 Offset;
public void SetHealth(float health, float maxHealth)
{
Slider.gameObject.SetActive(health < maxHealth);
Slider.value = health;
Slider.maxValue = maxHealth;
Slider.fillRect.GetComponentInChildren<Image>().color = Color.Lerp(Low, High, Slider.normalizedValue);
}
void Update()
{
Slider.transform.position = Camera.main.WorldToScreenPoint(transform.parent.position + Offset);
}
}
Answer by Terraya · Mar 30, 2021 at 02:27 AM
The problem here is, because you just Update your Health, not the Healthbar,
as i can see you use this function todo so:
HealthBar.SetHealth(KilluaHealth, KilluaMaxHealth);
so you would need to change your function to that:
public void AddLife()
{
if(KilluaHealth < KilluaMaxHealth && KilluaHealth <=0 == false)
{
HealthBar.gameObject.SetActive(true);
KilluaHealth += 50;
HealthBar.SetHealth(KilluaHealth, KilluaMaxHealth);
}
}
I can't describe how thankful i am, ..i knew that is would be that simple but still couldn't figure it out... Thank you again!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i make a Healthbar the same width no matter the health? 1 Answer
AV Pro Video Playback Rewinding Issue -Android 0 Answers
C# Health Bar 1 Answer