Radar.Spawn(transform, lifetime) clones the "Target Template", names it "Target" and attaches a RadarTarget that self-destructs after lifetime. RadarTarget aims each frame toward camera->target (full 3D LookRotation), drives the point renderer's _BaseColor from a forward-bearing gradient (ahead/side/behind depth cue lost in the flat disc projection) via a MaterialPropertyBlock, and scales the point renderer transform by camera->target distance. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
57 lines
2.2 KiB
C#
57 lines
2.2 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 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 <see cref="RadarTarget"/> for the per-blip behaviour.
|
|
/// </summary>
|
|
[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<Camera>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spawns a Target blip that each frame orients toward <paramref name="target"/> (from the reference
|
|
/// camera) and destroys itself after <paramref name="lifetime"/> seconds. Returns the spawned blip, or
|
|
/// null if it could not be created.
|
|
/// </summary>
|
|
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<RadarTarget>();
|
|
blip.Init(target, referenceCamera, lifetime);
|
|
return blip;
|
|
}
|
|
}
|