using System.Collections;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    public static UIManager Instance { get; private set; }

    [SerializeField] private Image damageIndicator;
    [SerializeField] private Camera playerCamera;
    private void Awake()
    {
        if (!Instance)
        {
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }

        // Ensure damageIndicator is not null; otherwise, log an error
        if (!damageIndicator)
        {
            Debug.LogError("Damage Indicator reference is not set in the UIManager.");
        }
        if (damageIndicator == null)
        {
            Debug.LogError("[UIManager] Damage indicator is not assigned.");
        }
        else
        {
            Debug.Log("[UIManager] Damage indicator is successfully assigned.");
        }

        damageIndicator.gameObject.SetActive(false);
    }

    public void ShowDamageIndicator(Vector3 damageDirection)
    {
        if (damageIndicator != null && playerCamera != null)
        {
            // Calculate the angle between the camera’s forward direction and the damage direction in local space
            Vector3 forward = playerCamera.transform.forward;
            //forward.y = 0; // Optional: Flatten the forward vector if you’re only interested in horizontal plane
            Vector3 localDamageDirection = playerCamera.transform.InverseTransformDirection(damageDirection.normalized);
            localDamageDirection.y = 0; // Optional: Flatten the damage direction as well

            float angle = Mathf.Atan2(localDamageDirection.x, localDamageDirection.z) * Mathf.Rad2Deg;

            // Define the distance from the center of the screen to position the indicator
            float indicatorDistance = 400f; // Adjust this value based on your UI layout

            // Calculate the position on the canvas circle based on the angle
            float xPosition = indicatorDistance * Mathf.Sin(angle * Mathf.Deg2Rad);
            float yPosition = indicatorDistance * Mathf.Cos(angle * Mathf.Deg2Rad);

            // Update the position of the damage indicator using anchoredPosition for UI elements
            damageIndicator.rectTransform.anchoredPosition = new Vector2(xPosition, yPosition);

            // Rotate the indicator to point towards the center (opposite direction of the damage)
            damageIndicator.rectTransform.localRotation = Quaternion.Euler(0, 0, angle);



            // Make the indicator visible
            damageIndicator.gameObject.SetActive(true);

            // Hide the indicator after a short delay
            StartCoroutine(HideDamageIndicatorAfterDelay(1.0f));
        }
        else
        {
            Debug.LogError("Player camera or damage indicator reference missing in UIManager.");
        }
    }

    // Coroutine to deactivate the indicator after a delay
    private IEnumerator HideDamageIndicatorAfterDelay(float delay)
    {
        yield return new WaitForSeconds(delay);
        if (damageIndicator != null)
        {
            damageIndicator.gameObject.SetActive(false);
        }
    }

    // … Other UI management methods
}


what does this code do?