Boss interacts with health bar

Here's the complete code that integrates the functionality of the boss grabbing and refreshing the health bar at 50% health, complete with comments to explain each part.
 avatar
unknown
csharp
16 days ago
3.1 kB
0
Indexable
using System.Collections;
using UnityEngine;
using UnityEngine.UI;  // For working with UI elements

public class BossBattle : MonoBehaviour
{
    public float maxHealth = 100f;          // The boss's maximum health
    private float bossHealth;              // The boss's current health
    private bool hasInteracted = false;    // Tracks whether the grab-and-refresh action has been triggered

    public Transform bossHand;             // The boss's hand (assign in Inspector)
    public RectTransform healthBarUI;      // The health bar UI element (assign in Inspector)
    public Transform originalCanvasTransform; // Original parent of the health bar in the Canvas
    private Vector3 originalPosition;      // Original position of the health bar
    private Animator animator;             // Reference to the boss's Animator component
    public Slider healthBarSlider;         // The slider representing the health bar (assign in Inspector)

    void Start()
    {
        // Initialize health
        bossHealth = maxHealth;

        // Store the original position of the health bar
        originalPosition = healthBarUI.position;

        // Get the Animator component attached to the boss
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // Check if the boss's health is below 50% and trigger the interaction
        if (bossHealth <= maxHealth / 2 && !hasInteracted)
        {
            hasInteracted = true;
            StartCoroutine(GrabAndRefreshHealthBar());
        }

        // Debug key to reduce health for testing (optional)
        if (Input.GetKeyDown(KeyCode.Space))
        {
            TakeDamage(10);
        }
    }

    public void TakeDamage(float damage)
    {
        // Reduce the boss's health
        bossHealth -= damage;

        // Update the health bar slider
        UpdateHealthBarUI();

        // Clamp health to prevent it from going below 0
        bossHealth = Mathf.Max(bossHealth, 0);
    }

    private void UpdateHealthBarUI()
    {
        // Update the health bar to reflect the current health
        healthBarSlider.value = bossHealth / maxHealth;
    }

    private IEnumerator GrabAndRefreshHealthBar()
    {
        // Detach the health bar from the Canvas and attach it to the boss's hand
        healthBarUI.SetParent(bossHand);
        healthBarUI.localPosition = Vector3.zero; // Position it at the hand

        // Play the grab animation
        animator.SetTrigger("GrabHealthBar");

        // Wait for the grab animation to finish (adjust time as needed)
        yield return new WaitForSeconds(1.0f);

        // Refresh the boss's health
        bossHealth = maxHealth;
        UpdateHealthBarUI();

        // Optional: Add a delay for the recharge animation
        yield return new WaitForSeconds(1.0f);

        // Return the health bar to its original position and parent
        healthBarUI.SetParent(originalCanvasTransform);
        healthBarUI.position = originalPosition;
    }
}
Leave a Comment