using Convai.Scripts;
using Convai.Scripts.Utils;
using UnityEngine;
///
/// Positions the VR transcript UI relative to the active ConvaiNPC based on appearance changes.
///
public class VRTranscriptUIPositionSetter : MonoBehaviour
{
// Offset for positioning the UI relative to the ConvaiNPC
[SerializeField] private Vector3 _offset;
///
/// Subscribes to events when the script is enabled.
///
private void OnEnable()
{
ConvaiNPCManager.Instance.OnActiveNPCChanged += ConvaiNPCManager_OnActiveNPCChanged;
}
///
/// Unsubscribes from events to prevent issues when the script is disabled.
///
private void OnDisable()
{
ConvaiNPCManager.Instance.OnActiveNPCChanged -= ConvaiNPCManager_OnActiveNPCChanged;
}
///
/// Updates the position of the current UI when the active ConvaiNPC changes.
///
/// The newly active ConvaiNPC.
private void ConvaiNPCManager_OnActiveNPCChanged(ConvaiNPC convaiNPC)
{
if (convaiNPC == null)
{
return;
}
UpdateCurrentUIPosition(convaiNPC);
}
///
/// Updates the position of the current UI based on the location and offset of the active ConvaiNPC.
///
/// The active ConvaiNPC.
private void UpdateCurrentUIPosition(ConvaiNPC convaiNPC)
{
Transform npcTransform = convaiNPC.transform;
Vector3 targetOffset = _offset.x * npcTransform.right + _offset.y * npcTransform.up + _offset.z * npcTransform.forward;
Debug.Log("NEW POSITION: "+ targetOffset);
transform.position = npcTransform.position + targetOffset;
}
}