using UnityEngine;

public class Billboard : MonoBehaviour
{
    [Tooltip("The camera the UI element should face.")]
    private Camera targetCamera;

    [Tooltip("The axis about which the UI element should rotate.")]
    public Vector3 axis = Vector3.up;

    [Tooltip("Whether to align the UI element's up direction with the camera's up direction.")]
    public bool alignWithCameraUp = true;

    private void Start()
    {
        targetCamera = Camera.main; 
    }

    private void LateUpdate()
    {
        // Ensure the target camera is not null
        if (targetCamera != null)
        {
            // Get the direction from the UI element to the camera
            Vector3 directionToCamera = targetCamera.transform.position - transform.position;

            // If aligning with camera up direction, adjust axis
            Vector3 finalAxis = alignWithCameraUp ? targetCamera.transform.up : axis;

            // Ensure the UI element faces the camera without rolling
            transform.LookAt(transform.position - directionToCamera, finalAxis);
        }
    }
}
