using UnityEngine;
///
/// Sits on the "Radar" overlay object (a disc rendered by the Radar Camera in front of the main camera).
/// Spawns "Target" blips - clones of the child "Target Template" - each of which continuously points from
/// the reference camera toward a world-space transform (visualising the bearing camera -> target) and then
/// destroys itself after a given lifetime. See for the per-blip behaviour.
///
[DisallowMultipleComponent]
public sealed class Radar : MonoBehaviour
{
[Tooltip("Template each spawned Target is cloned from. Auto-resolves to the child \"Target Template\".")]
[SerializeField] private Transform targetTemplate;
[Tooltip("Camera the direction is measured FROM (camera -> target). Auto-resolves to the Camera this radar sits under.")]
[SerializeField] private Camera referenceCamera;
private void Awake()
{
if (!targetTemplate)
{
var t = transform.Find("Target Template");
if (t) targetTemplate = t;
}
if (!referenceCamera) referenceCamera = GetComponentInParent();
}
///
/// Spawns a Target blip that each frame orients toward (from the reference
/// camera) and destroys itself after seconds. Returns the spawned blip, or
/// null if it could not be created.
///
public RadarTarget Spawn(Transform target, float lifetime)
{
if (!target)
{
Debug.LogWarning("[Radar] Spawn called without a target transform.", this);
return null;
}
if (!targetTemplate)
{
Debug.LogWarning("[Radar] No Target Template assigned or found.", this);
return null;
}
Transform clone = Instantiate(targetTemplate, targetTemplate.parent);
clone.name = "Target";
clone.localPosition = targetTemplate.localPosition;
clone.localScale = targetTemplate.localScale;
clone.gameObject.SetActive(true);
var blip = clone.GetComponent();
blip.Init(target, referenceCamera, lifetime);
return blip;
}
}