The player has to be hit 3 times (health <= 0) before the game over screen appears. The problem is that it keeps counting -1 -2 -3… How do I fix this?
these are my scripts that I used.
PlayerStats
public class PlayerStats : MonoBehaviour {
public Image healthBar;
public float cur_health = 3f;
public static int Cur_health { get; internal set; }
private void Start()
{
cur_health = 3f;
SetHealthBar();
}
public void TakeDamage(float amount)
{
cur_health -= amount;
SetHealthBar();
}
public void SetHealthBar()
{
float my_health = cur_health;
healthBar.transform.localScale = new Vector3(Mathf.Clamp(my_health, 0f, 1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
}
}
enemy damage
public class FallingBlocksDamage : MonoBehaviour {
float damage = 1f;
private void OnTriggerEnter(Collider other)
{
other.gameObject.GetComponent<PlayerStats>().TakeDamage(damage);
}
game over manager
public class GameOver1 : MonoBehaviour {
public static GameOver instance;
public GameObject gameOverScreen;
public Text secondsSurvivedUI;
public bool gameOver = false;
void Start()
{
FindObjectOfType<move1> ().OnPlayerDeath += OnGameOver;
}
void Update()
{
if (gameOver)
{
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("main scene");
}
}
}
void OnGameOver() {
gameOverScreen.SetActive(true);
secondsSurvivedUI.text = Mathf.RoundToInt(Time.timeSinceLevelLoad).ToString();
gameOver = true;
}
}
pls help me
,I Want my player to die and my game over screen to pop up. The player has to be hit 3 times before the screen pops up but when my health is lower the 0 it keeps counting -1 -2 -3… how do I fix this?
these are my scripts
PlayerStats
public float cur_health = 3f;
public Image healthBar;
public static int Cur_health { get; internal set; }
private void Start()
{
cur_health = 3f;
SetHealthBar();
}
public void TakeDamage(float amount)
{
cur_health -= amount;
SetHealthBar();
}
public void SetHealthBar()
{
float my_health = cur_health;
healthBar.transform.localScale = new Vector3(Mathf.Clamp(my_health, 0f, 1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
}
enemy damage script
{
float damage = 1f;
private void OnTriggerEnter(Collider other)
{
other.gameObject.GetComponent<PlayerStats>().TakeDamage(damage);
}
}
game over manager
public static GameOver instance;
public GameObject gameOverScreen;
public Text secondsSurvivedUI;
public bool gameOver = false;
void Start()
{
FindObjectOfType<move1> ().OnPlayerDeath += OnGameOver;
}
void Update()
{
if (gameOver)
{
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("main scene");
}
}
}
void OnGameOver() {
gameOverScreen.SetActive(true);
secondsSurvivedUI.text = Mathf.RoundToInt(Time.timeSinceLevelLoad).ToString();
gameOver = true;
}
}
How do I fix this?