MiniMap in Unity3D

Basically we all think that we are hard core gamers. We always tries to win the battle at any cost. The RPGs is the most popular category of the games. In this type of games, player is the brave warrior willing to complete his mission. All these types of games commonly have a Mini-Map, to show the player’s current position with respect to his enemies.

Today as Unity3D is most popular game engine in the market, this blog will help you to create a mini-map using Unity3D.

A Mini-Map is often placed at a screen corner in computer games to aid players in orienting themselves within the game world.

Depending on the game theme mini-map can contain different objects such as the player character, surrounding terrain, enemies, bonus prizes, and important locations or items that are highlighted on mini-map.

MiniMap_Position

First of all, you must decide what will be the contents of your mini-map, possibly there can be two things :

  1. The whole terrain (i.e. The whole game play area).
  2. Some part of the area surrounding the player character.

Here I have explained about the type two because it is quite tricky and also a better approach.

I am assuming that you have installed the Unity3D correctly, and aware about creating the new project and its structure. While creating the project select at least the following two packages :

  1. Character Controller.unityPackage. This package includes inbuilt First Person Controller, so that we can navigate using the standard input control arrow keys.
  2. Terrain Assets.unityPackage. This package is used to create a terrain area , add trees, grass etc.

Create_Project

 

    1. Create a Unity3D project  say “MyMiniMapProject”.
    2. Add the directional light by GameObject –> Create Other –> Directional Light to add some light in the scene.
    3. Create a terrain from GameObject –> Create Other –> Terrain. Set its width, height and length. Add some tress to the terrain.
    4. Drag the First Person Controller from project view to hierarchy view. Delete the ‘Main Camera’ as First Person Controller has its own Main Camera.

 

Hierarchy_Project_View

    1. To avoid the FPC fall down from Terrain, add a Capsule Collider to the First Person Controller.

Capsule_Colliders

    1. Run your project, now your player can move around the terrain with the arrow keys.
    2. This is where fun begins while creating the mini-map,  add a camera to the scene from GameObject –> Create Other –> Camera. Name it as “MiniMapCamera”.
    3. Rotate it 90 degree along X axis so that it can show the area from the top view. Align the camera above the player(FPC). Change the projection of MiniMapCamera from Perspective to Orthographic in the inspector, as we don’t want any depth in mini-map view.

 

MiniMapCameraAdjustment

    1. Set the MiniMapCamera ‘Depth’ property to 1. It is the most important property because lower depth are rendered before cameras with higher depth. Remove the FlareLayer, GUILayer, Audio Listener components.
    2. Adjust the viewport rect to position the mini-map in the game view. Try to adjust X, Y and width, height of the MiniMapCamera.

 

MiniMapCameraSetting

    1. Run the project, you can see the mini-map visible in the game view. As of now MiniMapCamera is not moving with player(FPC).

 

MiniMap_Position

    1. Now to make the MiniMapCamera follow with the player, we have to attach a script to the MiniMapCamera. Following is a script written in C#.

Code: FollowCamera.cs

 
using UnityEngine;
using System.Collections;
/**
* This Script is used for following the MiniMap camera with the First Person Controller.
* @Need : FirstPersonControllerObject GameObject.
*/
public class FollowCamera : MonoBehaviour {
// FPC GameObject pointer.
public GameObject FirstPersonControllerObject = null;

// Update is called once per frame.
   void Update () {
   // Assign the FirstPersonControllerObject's X and Z to MiniMapCamera's transform.
   // Keeping the Y same as MiniMapCamera's Y, Because we don't want to change the Y.
    this.transform.position = new Vector3(FirstPersonControllerObject
.transform.position.x,this.transform.position.y,FirstPersonControllerObject.
transform.position.z);
   }
}
    1. Add the script to the MiniMapCamera and drag the First Person Controller in the place of GameObject.

Attach_Script

  1. Now the run the project, Hooray your MiniMapCamera can move with your First Person Controller.

MiniMap_Position

Happy Coding..!!!! 🙂