How to do it...

To create a radar to show the relative positions of the objects, follow these steps:

  1. Create a new 3D project, with a textured Terrain. Import the Environment standard asset package contents, by choosing menu: Assets | Import Package | Environment.
    1. Create a terrain by navigating to the Create | 3D Object | Terrain menu.
    2. Size the Terrain 20 x 20, positioned at (-10, 0, -10) - so that its center is at (0, 0, 0):

    1. Texture paint your Terrain with the SandAlbedo option, as shown in the screenshot. You need to select the Paintbrush tool in the Terrain component, then click the Edit Textures button, and select the SandAlbedo texture from the imported Environment assets:

  1. Import the provided folder Images.
  2. Create a 3D Cube GameObject at Position (2, 0.5, 2). Create a Tag Cube and tag this GameObject with this new tag. Texture this GameObject with the red image called icon32_square_yellow, by dragging image icon32_square_yellow from the Project panel over this GameObject in the Hierarchy panel.
  3. Duplicate the cube GameObject, and move this new cube to Position (6, 0.5, 2).
  4. Create a 3D Sphere GameObject at Position (0, 0.5, 4). Create a Tag with the name Sphere and tag this GameObject with this new tag. Texture this GameObject with the red image called icon32_square_yellow.
  5. Import the Characters standard asset package into your project.
  6. From the Standard Assets folder in the Project panel, drag the prefab ThirdPersonController into the Scene and position it at (0, 1, 0).
  7. Tag this ThirdPersonController GameObject as Player (selecting this built-in tag means that the camera we'll add will automatically track this player object, without us having to manually set the target for the camera).
  8. Remove the Main Camera GameObject.
  9. Import the Cameras standard asset package into your project.
  10. From the Standard Assets folder in the Project panel, drag the prefab Multi-PurposeCameraRig into the Scene.
  11. In the Hierarchy panel, add a UI | RawImage GameObject to the scene named RawImage-radar.
  1. Ensure that the RawImage-radar GameObject is selected in the Hierarchy panel. From the Images folder in the Project panel, drag the radarBackground image into the Raw Image (Script) public property Texture.
  2. In Rect Transform, position RawImage-radar at the top-left using the Anchor Presets item. Then set both width and height to 200 pixels.
  3. Create a new UI RawImage named RawImage-blip. Assign it the yellowCircleBlackBorder Texture image file from the Project panel. In the Project panel, create a new empty prefab asset file named blip-sphere, and drag the RawImage-blip GameObject into this prefab to store all its properties.
  4. Set the texture of GameObject RawImage-blip to redSquareBlackBorder from the Project panel. In the Project panel, create a new empty prefab asset file named blip-cube, and drag the RawImage-blip GameObject into this prefab to store all its properties.
  5. Delete the RawImage-blip GameObject from the Hierarchy.
  6. Create a C# script class called Radar, containing the following code, and add an instance as a scripted component to the RawImage-radar GameObject:
using UnityEngine; 
using UnityEngine.UI; 

public class Radar : MonoBehaviour { 
   public float insideRadarDistance = 20; 
   public float blipSizePercentage = 5; 
   public GameObject rawImageBlipCube; 
   public GameObject rawImageBlipSphere; 
   private RawImage rawImageRadarBackground; 
   private Transform playerTransform; 
   private float radarWidth; 
   private float radarHeight; 
   private float blipHeight; 
   private float blipWidth; 

   void Start() { 
         rawImageRadarBackground = GetComponent<RawImage>(); 
         playerTransform = GameObject.FindGameObjectWithTag("Player").transform; 
         radarWidth = rawImageRadarBackground.rectTransform.rect.width; 
         radarHeight = rawImageRadarBackground.rectTransform.rect.height; 
         blipHeight = radarHeight * blipSizePercentage / 100; 
         blipWidth = radarWidth * blipSizePercentage / 100; 
   } 

   void Update() { 
         RemoveAllBlips(); 
         FindAndDisplayBlipsForTag("Cube", rawImageBlipCube); 
         FindAndDisplayBlipsForTag("Sphere", rawImageBlipSphere); 
   } 

   private void FindAndDisplayBlipsForTag(string tag, GameObject prefabBlip) { 
         Vector3 playerPos = playerTransform.position; 
         GameObject[] targets = GameObject.FindGameObjectsWithTag(tag); 
         foreach (GameObject target in targets) { 
               Vector3 targetPos = target.transform.position; 
               float distanceToTarget = Vector3.Distance(targetPos, playerPos); 
               if ((distanceToTarget <= insideRadarDistance)) 
                CalculateBlipPositionAndDrawBlip (playerPos, targetPos, prefabBlip); 
         } 
   }  

    private void CalculateBlipPositionAndDrawBlip (Vector3 playerPos, Vector3 targetPos, GameObject prefabBlip) { 
         Vector3 normalisedTargetPosition = NormaizedPosition(playerPos, targetPos); 
         Vector2 blipPosition = CalculateBlipPosition(normalisedTargetPosition); 
         DrawBlip(blipPosition, prefabBlip); 
   } 

   private void RemoveAllBlips() { 
         GameObject[] blips = GameObject.FindGameObjectsWithTag("Blip"); 
         foreach (GameObject blip in blips) 
               Destroy(blip); 
   } 

   private Vector3 NormaizedPosition(Vector3 playerPos, Vector3 targetPos) { 
         float normalisedyTargetX = (targetPos.x - playerPos.x) / insideRadarDistance; 
         float normalisedyTargetZ = (targetPos.z - playerPos.z) / insideRadarDistance; 
         return new Vector3(normalisedyTargetX, 0, normalisedyTargetZ); 
   } 

   private Vector2 CalculateBlipPosition(Vector3 targetPos) { 
         float angleToTarget = Mathf.Atan2(targetPos.x, targetPos.z) * Mathf.Rad2Deg; 
         float anglePlayer = playerTransform.eulerAngles.y; 
         float angleRadarDegrees = angleToTarget - anglePlayer - 90; 
         float normalizedDistanceToTarget = targetPos.magnitude; 
         float angleRadians = angleRadarDegrees * Mathf.Deg2Rad; 
         float blipX = normalizedDistanceToTarget * Mathf.Cos(angleRadians); 
         float blipY = normalizedDistanceToTarget * Mathf.Sin(angleRadians); 
         blipX *= radarWidth / 2; 
         blipY *= radarHeight / 2; 
         blipX += radarWidth / 2; 
         blipY += radarHeight / 2; 
         return new Vector2(blipX, blipY); 
   } 

   private void DrawBlip(Vector2 pos, GameObject blipPrefab) { 
         GameObject blipGO = (GameObject)Instantiate(blipPrefab); 
         blipGO.transform.SetParent(transform.parent); 
         RectTransform rt = blipGO.GetComponent<RectTransform>(); 
         rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, pos.x, blipWidth); 
         rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, pos.y, blipHeight); 
   } 
} 

Run your game. You will see two red squares and one yellow circle on the radar, showing the relative positions of the red cubes and yellow sphere. If you move too far away, the blips will disappear.