Голосование за лучший ответ
Азпзпзппзппзз
Мыслитель
(9882)
7 месяцев назад
Вот что мне ответил чат гпт
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Health : MonoBehaviour
{
public bool canDie = true;
public float startingHealth = 100.0f;
public float maxHealth = 100.0f;
public float currentHealth;
public GameObject gameOverUI;
public Text healthText;
public Text deathText;
public Button respawnButton;
public Button mainMenuButton;
public bool replaceWhenDead = false;
public GameObject deadReplacement;
public bool makeExplosion = false;
public GameObject explosion;
public bool isPlayer = false;
private bool dead = false;
private float originalStartingHealth;
private void Start()
{
originalStartingHealth = startingHealth;
currentHealth = startingHealth;
UpdateHealthText();
// Ensure this object persists through scene changes
DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded; // Subscribe to the sceneLoaded event
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// Reset the health when a new scene is loaded
ResetHealth();
}
private void OnDestroy()
{
// Unsubscribe from the sceneLoaded event
SceneManager.sceneLoaded -= OnSceneLoaded;
}
public void ChangeHealth(float amount)
{
currentHealth += amount;
if (currentHealth <= 0 && !dead && canDie)
Die();
else if (currentHealth > maxHealth)
currentHealth = maxHealth;
UpdateHealthText();
}
public void Die()
{
dead = true;
if (replaceWhenDead && deadReplacement != null)
Instantiate(deadReplacement, transform.position, transform.rotation);
if (makeExplosion && explosion != null)
Instantiate(explosion, transform.position, transform.rotation);
if (isPlayer)
RestartScene();
Destroy(gameObject);
}
public void RestartScene()
{
ResetHealth();
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void ResetHealth()
{
startingHealth = originalStartingHealth;
currentHealth = startingHealth;
UpdateHealthText();
}
private void UpdateHealthText()
{
if (healthText != null)
{
healthText.text = Mathf.RoundToInt(currentHealth).ToString();
}
}
}
hj vfy
Ученик
(118)
7 месяцев назад
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Health : MonoBehaviour
{
public bool canDie = true;
public float startingHealth = 100.0f;
public float maxHealth = 100.0f;
public float currentHealth;
public GameObject gameOverUI;
public Text healthText;
public Text deathText;
public Button respawnButton;
public Button mainMenuButton;
public bool replaceWhenDead = false;
public GameObject deadReplacement;
public bool makeExplosion = false;
public GameObject explosion;
public bool isPlayer = false;
private bool dead = false;
private float originalStartingHealth;
private void Awake()
{
FindHealthText();
}
private void Start()
{
originalStartingHealth = startingHealth; // Сохраняем начальное значение здоровья
currentHealth = startingHealth;
UpdateHealthText();
}
private void FindHealthText()
{
// Находим объект healthText по тегу или другим способом и сохраняем ссылку на него
healthText = GameObject.FindGameObjectWithTag("HealthText").GetComponent<Text>();
}
public void ChangeHealth(float amount)
{
currentHealth += amount;
if (currentHealth <= 0 && !dead && canDie)
Die();
else if (currentHealth > maxHealth)
currentHealth = maxHealth;
UpdateHealthText();
}
public void Die()
{
dead = true;
if (replaceWhenDead)
Instantiate(deadReplacement, transform.position, transform.rotation);
if (makeExplosion)
Instantiate(explosion, transform.position, transform.rotation);
if (isPlayer)
RestartScene();
Destroy(gameObject);
}
public void RestartScene()
{
ResetHealth(); // Сбрасываем здоровье
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void ResetHealth()
{
startingHealth = originalStartingHealth; // Восстанавливаем начальное значение здоровья
currentHealth = startingHealth;
UpdateHealthText();
}
private void UpdateHealthText()
{
if (healthText != null)
{
healthText.text = Mathf.RoundToInt(currentHealth).ToString();
}
}
}
попробуй этот код
Vhbb Vhvvgg
Ученик
(141)
7 месяцев назад
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Health : MonoBehaviour
{
public bool canDie = true;
public float startingHealth = 100.0f;
public float maxHealth = 100.0f;
public float currentHealth;
public GameObject gameOverUI;
public Text healthText;
public Text deathText;
public Button respawnButton;
public Button mainMenuButton;
public bool replaceWhenDead = false;
public GameObject deadReplacement;
public bool makeExplosion = false;
public GameObject explosion;
public bool isPlayer = false;
private bool dead = false;
private float originalStartingHealth;
private void Start()
{
originalStartingHealth = startingHealth; // Сохраняем начальное значение здоровья
currentHealth = startingHealth;
UpdateHealthText();
}
public void ChangeHealth(float amount)
{
currentHealth += amount;
if (currentHealth <= 0 && !dead && canDie)
Die();
else if (currentHealth > maxHealth)
currentHealth = maxHealth;
UpdateHealthText();
}
public void Die()
{
dead = true;
if (replaceWhenDead)
Instantiate(deadReplacement, transform.position, transform.rotation);
if (makeExplosion)
Instantiate(explosion, transform.position, transform.rotation);
if (isPlayer)
Destroy(gameObject);
}
public void RestartScene()
{
ResetHealth(); // Сбрасываем здоровье
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void ResetHealth()
{
startingHealth = originalStartingHealth; // Восстанавливаем начальное значение здоровья
currentHealth = startingHealth;
UpdateHealthText();
}
private void UpdateHealthText()
{
if (healthText != null)
{
healthText.text = Mathf.RoundToInt(currentHealth).ToString();
}
}
}
когда персонаж получает урон, хп уменьшается на индикаторе
когда хп персонажа 0, то сцена перезапускается
проблема в том что после перезапуска сцены, когда игрок получает урон, хп перестает уменьшатся на индикаторе