Minimap System

Render texture minimap with player icon, enemy blips, objective markers, and zoom control.

File
MinimapSystem.cs
Unity
2022.3+
Level
intermediate
Size
3.2 KB · 177 lines
Added
2026-03-15
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 10 serialized fields with their defaults.

Inspector
Script MinimapSystem
Camera
Minimap Camera None (Camera)
Player Transform None (Transform)
Follow
Height 40
Rotate With Player
Zoom
Default Zoom 30
Min Zoom 15
Max Zoom 60
Zoom Speed 5
Icons
Icon Prefab None (GameObject)
Icon Scale 1

When to use it

Essential for open-world RPGs, RTS games, and exploration games where players need spatial awareness. Great for showing enemy positions, quest objectives, and points of interest. Pair with Top-Down Camera for a cohesive RTS control scheme.

Setup

  1. Create a secondary Camera set to Orthographic, looking down (rotation 90,0,0)
  2. Set it to render to a RenderTexture (create one: 256x256)
  3. Display the RenderTexture in a RawImage UI element (circular mask optional)
  4. Attach MinimapSystem, assign the camera and player transform
  5. Register icons: MinimapSystem.RegisterIcon(enemy.transform, Color.red)
  6. Unregister on destroy: MinimapSystem.UnregisterIcon(transform)
  7. Create a 'Minimap' layer and set camera culling to only show minimap objects

Features

  • Render texture-based minimap with dedicated orthographic camera
  • Automatic player following with optional rotation-match
  • Dynamic icon registration and unregistration for any tracked object
  • Configurable zoom in/out with smooth interpolation
  • Automatic cleanup of icons for destroyed objects
  • Singleton pattern for easy global access from any script

Inspector fields

Every field Minimap System exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Minimap CameraminimapCameraCameraNoneCamera
Player TransformplayerTransformTransformNoneCamera
Heightheightfloat40Follow
Rotate With PlayerrotateWithPlayerboolfalseFollow
Default ZoomdefaultZoomfloat30Zoom
Min ZoomminZoomfloat15Zoom
Max ZoommaxZoomfloat60Zoom
Zoom SpeedzoomSpeedfloat5Zoom
Icon PrefabiconPrefabGameObjectNoneIcons
Icon ScaleiconScalefloat1Icons

Public API

What other scripts can call on MinimapSystem.

MemberReturnsDescription
static RegisterIcon(Transform target, Color color, float scale = 1f)voidRegister a transform to show on the minimap.
static UnregisterIcon(Transform target)voidRemove a tracked icon from the minimap.
ZoomIn()voidZoom in the minimap.
ZoomOut()voidZoom out the minimap.
Initialize(Transform trackedTarget, Color color, float scale)void
static Instance propertyMinimapSystem

Common mistakes

You must create a dedicated 'Minimap' layer and set the minimap camera to only render that layer — otherwise it will render the full scene. The minimap camera needs a RenderTexture assigned as its Target Texture, and a RawImage UI element must display that texture. Icon prefabs need a Renderer component for the color to apply; SpriteRenderers work too.

Source code

MinimapSystem.cs, 177 lines, MIT licensed. Paste it into a new C# file of the same name.

MinimapSystem.cs
C#
using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Render texture minimap. Requires a secondary orthographic camera
/// rendered to a RenderTexture displayed in a RawImage UI element.
/// </summary>
public class MinimapSystem : MonoBehaviour
{
    public static MinimapSystem Instance { get; private set; }

    [Header("Camera")]
    [SerializeField] private Camera minimapCamera;
    [SerializeField] private Transform playerTransform;

    [Header("Follow")]
    [SerializeField] private float height = 40f;
    [SerializeField] private bool rotateWithPlayer = false;

    [Header("Zoom")]
    [SerializeField] private float defaultZoom = 30f;
    [SerializeField] private float minZoom = 15f;
    [SerializeField] private float maxZoom = 60f;
    [SerializeField] private float zoomSpeed = 5f;

    [Header("Icons")]
    [SerializeField] private GameObject iconPrefab;
    [SerializeField] private float iconScale = 1f;

    private float currentZoom;
    private Dictionary<Transform, MinimapIcon> trackedIcons = new Dictionary<Transform, MinimapIcon>();

    private void Awake()
    {
        if (Instance == null)
            Instance = this;
        else
        {
            Destroy(gameObject);
            return;
        }

        currentZoom = defaultZoom;

        if (minimapCamera != null)
            minimapCamera.orthographicSize = currentZoom;
    }

    private void LateUpdate()
    {
        if (minimapCamera == null || playerTransform == null) return;

        // Follow player
        Vector3 camPos = playerTransform.position;
        camPos.y = playerTransform.position.y + height;
        minimapCamera.transform.position = camPos;

        // Rotation
        if (rotateWithPlayer)
        {
            minimapCamera.transform.rotation = Quaternion.Euler(
                90f, playerTransform.eulerAngles.y, 0f
            );
        }
        else
        {
            minimapCamera.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
        }

        // Update zoom
        minimapCamera.orthographicSize = Mathf.Lerp(
            minimapCamera.orthographicSize,
            currentZoom,
            Time.deltaTime * 5f
        );

        // Update tracked icons
        CleanupDestroyedIcons();
    }

    /// <summary>
    /// Register a transform to show on the minimap.
    /// </summary>
    public static void RegisterIcon(Transform target, Color color, float scale = 1f)
    {
        if (Instance == null || Instance.iconPrefab == null) return;
        if (Instance.trackedIcons.ContainsKey(target)) return;

        GameObject icon = Instantiate(Instance.iconPrefab);
        MinimapIcon minimapIcon = icon.GetComponent<MinimapIcon>();

        if (minimapIcon == null)
            minimapIcon = icon.AddComponent<MinimapIcon>();

        minimapIcon.Initialize(target, color, scale * Instance.iconScale);
        Instance.trackedIcons[target] = minimapIcon;
    }

    /// <summary>
    /// Remove a tracked icon from the minimap.
    /// </summary>
    public static void UnregisterIcon(Transform target)
    {
        if (Instance == null) return;
        if (Instance.trackedIcons.TryGetValue(target, out MinimapIcon icon))
        {
            if (icon != null)
                Destroy(icon.gameObject);
            Instance.trackedIcons.Remove(target);
        }
    }

    /// <summary>Zoom in the minimap.</summary>
    public void ZoomIn()
    {
        currentZoom = Mathf.Max(minZoom, currentZoom - zoomSpeed);
    }

    /// <summary>Zoom out the minimap.</summary>
    public void ZoomOut()
    {
        currentZoom = Mathf.Min(maxZoom, currentZoom + zoomSpeed);
    }

    private void CleanupDestroyedIcons()
    {
        List<Transform> toRemove = new List<Transform>();
        foreach (var kvp in trackedIcons)
        {
            if (kvp.Key == null || kvp.Value == null)
                toRemove.Add(kvp.Key);
        }
        foreach (var key in toRemove)
        {
            if (trackedIcons.TryGetValue(key, out MinimapIcon icon))
            {
                if (icon != null) Destroy(icon.gameObject);
            }
            trackedIcons.Remove(key);
        }
    }
}

/// <summary>
/// Minimap icon that follows a world-space target.
/// </summary>
public class MinimapIcon : MonoBehaviour
{
    private Transform target;
    private float heightOffset = 40f;

    public void Initialize(Transform trackedTarget, Color color, float scale)
    {
        target = trackedTarget;
        transform.localScale = Vector3.one * scale;

        Renderer rend = GetComponent<Renderer>();
        if (rend != null)
            rend.material.color = color;

        // Place on minimap layer
        gameObject.layer = LayerMask.NameToLayer("Minimap");
    }

    private void LateUpdate()
    {
        if (target == null)
        {
            Destroy(gameObject);
            return;
        }

        Vector3 pos = target.position;
        pos.y = target.position.y + heightOffset;
        transform.position = pos;
    }
}