我正忙着创建一个健康系统,并在屏幕上创建了一个游戏来显示当前的健康状况是0/玩家死亡。但是屏幕上的游戏没有显示出来(我对if
不太了解)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.InputSystem;
public class HealthSystem : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
public GameObject gameOver;
public bool damage;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
public void GameOver()
{
if (currentHealth == 0)
{
gameOver.SetActive(true);
}
}
public void RestartButton()
{
SceneManager.LoadScene("Playground");
}
public void OnDamage(InputValue value)
{
DamageInput(value.isPressed);
TakeDamage(10);
}
public void DamageInput(bool newDamageState)
{
damage = newDamageState;
}
}
发布于 2022-07-23 15:31:54
在代码中没有调用GameOver()
。每次减少currentHealth()
时,检查是否达到零,并执行必要的操作。为此,在您的GameOver()
方法中调用TakeDamage()
void TakeDamage(int damage)
{
currentHealth -= damage;
GameOver();
healthBar.SetHealth(currentHealth);
}
另外,检查currentHealth
是否小于或等于0
,而不仅仅是0
public void GameOver()
{
if (currentHealth <= 0)
{
gameOver.SetActive(true);
}
}
https://stackoverflow.com/questions/73091819
复制相似问题