
Hi all,
Last couple days I've been writing a little tutorial on how to create the enemy radar that I made for my game a couple of days ago. If you are interested, the full tutorial can be found on my own blog over here:
https://timcoster.com/2020/03/25/unity-enemy-radar-tutorial/
Or if you're a super awesome pro like me and you don't need a tutorial, here is the super short quick and dirty version:
1. Create a GUI Canvas and a RawImage.
2. Use the first texture for the RawImage:
3. Attach the script posted below to the RawImage:
4. Create two RawImages with the second radar blip texture and color them red and green:
5. Turn the red and green blips into prefabs.
6. Create a red and green capsule and tag them Enemy and NPC.
7. Populate the EnemyRadar script in the Inspector. Use the camera for the player property and the prefabs for the blips.
8. Play

//using System.Collections;
//using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class EnemyRadar : MonoBehaviour {
public float radarDistance = 20, blipSizePercentage = 15;
public bool usePlayerDirection = true;
public Transform player;
public GameObject blipRedPrefab,blipGreenPrefab;
public string redBlipTag = "Enemy", greenBlipTag = "NPC";
private float radarWidth, radarHeight, blipWidth, blipHeight;
void Start() {
radarWidth = GetComponent<RectTransform>().rect.width;
radarHeight = GetComponent<RectTransform>().rect.height;
blipHeight = radarHeight * blipSizePercentage/100;
blipWidth = radarWidth * blipSizePercentage/100;
}
void Update() {
RemoveAllBlips();
DisplayBlips(redBlipTag, blipRedPrefab);
DisplayBlips(greenBlipTag, blipGreenPrefab);
}
private void DisplayBlips(string tag, GameObject prefabBlip) {
Vector3 playerPos = player.position;
GameObject[] targets = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject target in targets) {
Vector3 targetPos = target.transform.position;
float distanceToTarget = Vector3.Distance(targetPos, playerPos);
if(distanceToTarget <= radarDistance) {
Vector3 normalisedTargetPosition = NormalisedPosition(playerPos, targetPos);
Vector2 blipPosition = CalculateBlipPosition(normalisedTargetPosition);
DrawBlip(blipPosition, prefabBlip);
}
}
}
private void RemoveAllBlips() {
GameObject[] blips = GameObject.FindGameObjectsWithTag("RadarBlip");
foreach (GameObject blip in blips)
Destroy(blip);
}
private Vector3 NormalisedPosition(Vector3 playerPos, Vector3 targetPos) {
float normalisedTargetX = (targetPos.x - playerPos.x)/radarDistance;
float normalisedTargetZ = (targetPos.z - playerPos.z)/radarDistance;
return new Vector3(normalisedTargetX, 0, normalisedTargetZ);
}
private Vector2 CalculateBlipPosition(Vector3 targetPos) {
// find the angle from the player to the target.
float angleToTarget = Mathf.Atan2(targetPos.x,targetPos.z) * Mathf.Rad2Deg;
// The direction the player is facing.
float anglePlayer = usePlayerDirection? player.eulerAngles.y : 0;
// Subtract the player angle, to get the relative angle to the object. Subtract 90
// so 0 degrees (the same direction as the player) is Up.
float angleRadarDegrees = angleToTarget - anglePlayer - 90;
// Calculate the xy position given the angle and the distance.
float normalisedDistanceToTarget = targetPos.magnitude;
float angleRadians = angleRadarDegrees * Mathf.Deg2Rad;
float blipX = normalisedDistanceToTarget * Mathf.Cos(angleRadians);
float blipY = normalisedDistanceToTarget * Mathf.Sin(angleRadians);
// Scale the blip position according to the radar size.
blipX *= radarWidth*.5f;
blipY *= radarHeight*.5f;
// Offset the blip position relative to the radar center
blipX += (radarWidth*.5f) - blipWidth*.5f;
blipY += (radarHeight*.5f) - blipHeight*.5f;
return new Vector2(blipX, blipY);
}
private void DrawBlip(Vector2 pos, GameObject blipPrefab) {
GameObject blip = (GameObject) Instantiate(blipPrefab);
blip.transform.SetParent(transform);
RectTransform rt = blip.GetComponent<RectTransform>();
rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left,pos.x, blipWidth);
rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top,pos.y, blipHeight);
}
}