Advertisement
johnnygoodguy2000

BossHealthManager.cs

May 25th, 2024 (edited)
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BossHealthManager : MonoBehaviour
  6. {
  7.     public int enemyHealth; // How much health the enemy has
  8.     public GameObject deathParticle; // Effect when enemy dies
  9.     public int pointsForKillingEnemy; // Points to add when the enemy is killed
  10.     public GameObject bossPrefab; // Prefab for the boss
  11.     public float minSize; // Minimum size for the boss to stop dividing
  12.     public int initialHealth; // Initial health for clones
  13.  
  14.     void Start()
  15.     {
  16.         if (enemyHealth <= 0)
  17.         {
  18.             enemyHealth = initialHealth; // Set initial health if not already set
  19.         }
  20.     }
  21.  
  22.     void Update()
  23.     {
  24.         if (enemyHealth <= 0) // If the enemy health is less than or equal to 0
  25.         {
  26.             Debug.Log("Enemy health is zero or less. Triggering division.");
  27.             Instantiate(deathParticle, transform.position, transform.rotation); // Spawn the death effect
  28.             ScoreManager.AddPoints(pointsForKillingEnemy); // Add points to score
  29.  
  30.             if (transform.localScale.x > minSize && transform.localScale.y > minSize) // Check if the boss size is greater than the minimum size
  31.             {
  32.                 float newScaleX = transform.localScale.x * 0.5f; // Calculate the new size for the clones
  33.                 float newScaleY = transform.localScale.y * 0.5f;
  34.  
  35.                 Debug.Log("Creating clones with new scale: " + newScaleX + ", " + newScaleY);
  36.  
  37.                 // Instantiate clone1 and set its scale and health
  38.                 GameObject clone1 = Instantiate(bossPrefab, new Vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z), transform.rotation);
  39.                 Debug.Log("Clone1 position: " + clone1.transform.position);
  40.                 clone1.transform.localScale = new Vector3(newScaleX, newScaleY, transform.localScale.z);
  41.                 Debug.Log("Clone1 scale: " + clone1.transform.localScale);
  42.                 clone1.GetComponent<BossHealthManager>().enemyHealth = initialHealth; // Set health of boss clone1
  43.  
  44.                 // Instantiate clone2 and set its scale and health
  45.                 GameObject clone2 = Instantiate(bossPrefab, new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z), transform.rotation);
  46.                 Debug.Log("Clone2 position: " + clone2.transform.position);
  47.                 clone2.transform.localScale = new Vector3(newScaleX, newScaleY, transform.localScale.z);
  48.                 Debug.Log("Clone2 scale: " + clone2.transform.localScale);
  49.                 clone2.GetComponent<BossHealthManager>().enemyHealth = initialHealth; // Set health of boss clone2
  50.             }
  51.             else
  52.             {
  53.                 Debug.Log("Boss has reached minimum size and will not divide further.");
  54.             }
  55.  
  56.             Destroy(gameObject); // Destroy the original enemy
  57.         }
  58.     }
  59.  
  60.     public void giveDamage(int damageToGive)
  61.     {
  62.         enemyHealth -= damageToGive;
  63.         Debug.Log("Enemy takes damage: " + damageToGive + ". Current health: " + enemyHealth);
  64.         GetComponent<AudioSource>().Play();
  65.     }
  66. }
  67.  
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement