- Home /
Problem with character Max Health and Healing items pick up.
Hello everyone, I made my first platform game and I was wondering if any of you could lend me a hand to fix some issues.
In the game the player Healthbar maximum capacity it's 1f and it is shown in the Scene with a Scrollbar.
It mostly works as it should, I can get damage from the enemies (bullets and contact) and I can heal myself 0,25f of health by picking up some healing items on the stages, the problem is that if I have full health the healing item will overheal me after going throught it (the healing item is destroyed afterwards). My idea was that whenever I have full health, my player won't be able to pick up the healing item and it will keep his health when going throught it, not destroying the item itself in the process. I'm currently using these two scripts:
This one for the healing item
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RecibirVida : MonoBehaviour
{
public float cura = 0.25f;
// Start is called before the first frame update
void Start()
{
}
void OnTriggerEnter(Collider other)
{
other.gameObject.GetComponent<HealthBar>().TakeLife(cura);
Debug.Log("daño");
Destroy(gameObject);
}
}
And this one for the player health.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthBar : MonoBehaviour
{
public float MaxHealth = 1f;
public float CurrentHealth;
public Scrollbar Barra;
bool damaged;
bool Cura;
bool isDead;
CharacterController2D playerMovement;
// Start is called before the first frame update
void Start()
{
CurrentHealth = MaxHealth;
// SetHelathBar();
}
public void TakeLife(float amount)
{
Cura = true;
CurrentHealth += amount;
Barra.size = CurrentHealth;
}
public void TakeDamage(float amount)
{
damaged = true;
CurrentHealth -= amount;
Barra.size = CurrentHealth;
if (CurrentHealth <= 0 && !isDead)
{
//SceneManager.LoadScene("SampleScene");
CurrentHealth = 0;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
//SetHealthBar();
}
public void SetHealthBar()
{
float myHealth = CurrentHealth / MaxHealth;
Barra.transform.localScale = new Vector3(Mathf.Clamp(myHealth, 0f, 1f), Barra.transform.localScale.y, Barra.transform.localScale.z);
}
}
Forgot to mention that some words are written in spanish, allow me to translate them: cura=heal, daño=damage, barra=bar (the name of the player health scrollbar)
And that's pretty much it, thanks beforehand for the help and my apologies if I posted it on the wrong section of the forum, first-timer here.
Answer by jackmw94 · Nov 09, 2020 at 11:51 PM
If there is some condition to whether or not TakeLife works then it'll have to return a flag saying whether or not it's completed successfully:
public bool TakeLife(float amount)
{
float finalHealth = CurrentHealth + amount;
if (finalHealth > 1)
{
return false;
}
Cura = true;
CurrentHealth = finalHealth;
Barra.size = CurrentHealth;
return true;
}
Then we can use that to determine whether or not we consume the health item:
void OnTriggerEnter(Collider other)
{
bool completed = other.gameObject.GetComponent<HealthBar>().TakeLife(cura);
if (completed)
{
Debug.Log("daño");
Destroy(gameObject);
}
else
{
Debug.Log("Could not heal, current health too high");
}
}
If you change your mind and want to be able to pickup the item and limit health to one, you can use a 'clamp' function to set your health to stay between two values:
public bool TakeLife(float amount)
{
Cura = true;
CurrentHealth += finalHealth;
CurrentHealth = Mathf.Clamp(CurrentHealth, 0, 1);
Barra.size = CurrentHealth;
return true;
}
Also, as a side note: you can use Image components as health bars: if you assign a 'source image' to it then you'll see an option called 'image type' appear. By setting this to 'filled' you can then control how much of the image is filled via its 'fillAmount'. This will handle staying between 0 and 1 for you, you get the option of different fill method and you can give it your own image!
Your answer
Follow this Question
Related Questions
Occasional taking control of Character Controller 1 Answer
Box Collider Trigger not working 1 Answer
Dialogue character emotions system 1 Answer
How can I stop killing myself ? 3 Answers