using UnityEngine;
public class DamageDealer : MonoBehaviour
{
public float damageAmount = 3f; // Set the damage amount to 3 for the bullet
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")) // Check if colliding with the player
{
PlayerHealth playerHealth = other.GetComponent<PlayerHealth>();
if (playerHealth != null) // Ensure the PlayerHealth component exists
{
playerHealth.TakeDamage(damageAmount); // Deal damage to player
}
}
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public float health = 100f; // Initial health
public float maxHealth = 100f; // Maximum health
public Image healthBar; // Reference to the health bar UI
void Start()
{
health = maxHealth; // Set the current health to max at the start
UpdateHealthBar();
}
public void TakeDamage(float damageAmount)
{
health -= damageAmount; // Reduce health by the damage amount
health = Mathf.Clamp(health, 0, maxHealth); // Ensure health doesn't go below zero
UpdateHealthBar();
if (health <= 0)
{
Die(); // Call death handling if health reaches zero
}
}
private void UpdateHealthBar()
{
if (healthBar != null) // Ensure the health bar is assigned
{
healthBar.fillAmount = health / maxHealth;
}
else
{
Debug.LogWarning("Health bar is not assigned in the Inspector!");
}
}
private void Die()
{
Debug.Log("Player has died.");
// Handle player death, like reloading the scene or triggering a game over screen
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // Reloads the scene
}
}
using UnityEngine;
using UnityEngine.UI;
public class Damage : MonoBehaviour
{
public Image healthBar;
public float currentHealth;
public float maxHealth;
private bool FindPlayer = false; // Declare FindPlayer as a boolean
void Update()
{
healthBar.fillAmount = currentHealth / maxHealth;
}
void GiveDamage()
{
if (FindPlayer) // Check if FindPlayer is true
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
if (player)
{
float damageAmount = 10f; // Set your desired damage amount here
player.GetComponent<PlayerHealth>().TakeDamage(damageAmount); // Pass the damage amount
}
}
}
private void OnTriggerEnter(Collider collision)
{
FindPlayer = true; // Set FindPlayer to true when collision occurs
}
private void OnTriggerExit(Collider collision)
{
FindPlayer = false; // Reset FindPlayer to false when leaving trigger
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerHealthController : MonoBehaviour
{
public float health = 100f; // Initial health value
public float maxHealth = 100f;
public Image healthBar; // Reference to the UI element used to display the health bar
void Start()
{
maxHealth = health; // Set the maximum health
}
void Update()
{
if (healthBar != null) // Check if the healthBar is assigned
{
UpdateHealthBar();
if (health <= 0) // Check if the health has reached 0 or below
{
Die(); // Call the die function
}
}
else
{
Debug.LogError("healthBar is not assigned in the Inspector!");
}
}
private void UpdateHealthBar()
{
healthBar.fillAmount = Mathf.Clamp(health / maxHealth, 0, 1);
}
public void TakeDamage(float damageAmount)
{
health -= damageAmount;
if (health < 0)
{
health = 0; // Ensure health doesn’t go below zero
}
}
private void Die()
{
Debug.Log("Player has died.");
// Add any death handling logic here, such as a game over screen or scene reload
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // Reload the current scene
}
}