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.